Use PyMapping_GetOptionalItemString where necessary with Python 3.13
With Python 3.13, some lookup methods like PyMapping_GetItemString and PyObject_GetAttrString became more strict. They are now always throwing an exception in case the attribute is not found.
To make these optional lookups work again, the GetOptional family of functions needs to be used.
See:
- https://docs.python.org/3/c-api/object.html#c.PyObject_GetOptionalAttrString
- https://docs.python.org/3/c-api/mapping.html#c.PyMapping_GetOptionalItemString
This is the upstream version of the following ROOT commit:
- https://github.com/root-project/root/commit/0a823d99ee
Related to the following cppyy issue, although it might be better to close that issue by fixing the underlying problem:
- https://github.com/wlav/cppyy/issues/273
Interesting, since even with py13, https://github.com/wlav/cppyy/issues/273 doesn't fail for me.
Yes, I've read that. Maybe my reproducer in the issue is not complete. I'll investigate this further.
As for the PR, I'm much rather use a more descriptive name and put a compatibility function in CPyCppyy.h. I'll do that later: I'm going to remove all Python2 stuff, finally. My last place to test it was an old Mac, but that one has other issues now, so the most recent release didn't have code verified on it.
This graph shows that py3.7 is the oldest I need to care about.
I'm not sure at this point if the PR is covered by a unit test in ROOT or not.
Let's see: https://github.com/root-project/root/pull/19401
The crossinheritance tests fail on Python 3.14 debug without this PR. Unfortunately, not with a clear error message...
2025-12-03T22:31:24.0936637Z ============================= test session starts ==============================
2025-12-03T22:31:24.0987517Z platform linux -- Python 3.14.0, pytest-8.4.2, pluggy-1.6.0
2025-12-03T22:31:24.1007008Z rootdir: /github/home/ROOT-CI/src/bindings/pyroot/cppyy/cppyy
2025-12-03T22:31:24.1007512Z configfile: pyproject.toml
2025-12-03T22:31:24.1008194Z collected 40 items
2025-12-03T22:31:24.1008343Z
2025-12-03T22:31:24.1008588Z ../../../../../../src/bindings/pyroot/cppyy/cppyy/test/test_crossinheritance.py x [ 2%]
2025-12-03T22:31:24.1009072Z .............x.....sss.sss..s.x.x....CMake Error at /github/home/ROOT-CI/src/cmake/modules/RootTestDriver.cmake:232 (message):
2025-12-03T22:31:24.1009441Z error code: Subprocess aborted