Skip to content

Commit d5a74fa

Browse files
committed
[Python] Only use Python Limited C API in ROOT Pythonizations library
This follows up on 4e0c7bf, swapping out the remaining use of CPython API functions that were not inside the limited API. In the cases where we're sill using private headers from cppyy, the parts of these headers that don't compile with the limited API are excluded via the `Py_LIMITED_API` preprocessor macro.
1 parent e8f1237 commit d5a74fa

14 files changed

Lines changed: 76 additions & 46 deletions

bindings/pyroot/cppyy/CPyCppyy/src/CPPInstance.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//////////////////////////////////////////////////////////////////////////////
1111

1212
// Bindings
13+
#ifndef Py_LIMITED_API
1314
#include "CPPScope.h"
15+
#endif
1416
#include "Cppyy.h"
1517
#include "CallContext.h" // for Parameter
1618

@@ -116,12 +118,14 @@ inline void* CPPInstance::GetObject()
116118
}
117119

118120
//----------------------------------------------------------------------------
121+
#ifndef Py_LIMITED_API
119122
inline Cppyy::TCppType_t CPPInstance::ObjectIsA(bool check_smart) const
120123
{
121124
// Retrieve the C++ type identifier (or raw type if smart).
122125
if (check_smart || !IsSmart()) return ((CPPClass*)Py_TYPE(this))->fCppType;
123126
return GetSmartIsA();
124127
}
128+
#endif
125129

126130

127131
//- object proxy type and type verification ----------------------------------
@@ -132,6 +136,7 @@ extern __declspec(dllimport) PyTypeObject CPPInstance_Type;
132136
extern PyTypeObject CPPInstance_Type;
133137
#endif
134138

139+
#ifndef Py_LIMITED_API
135140
template<typename T>
136141
inline bool CPPInstance_Check(T* object)
137142
{
@@ -141,6 +146,7 @@ inline bool CPPInstance_Check(T* object)
141146
(Py_TYPE(object)->tp_new == CPPInstance_Type.tp_new || \
142147
PyObject_TypeCheck(object, &CPPInstance_Type));
143148
}
149+
#endif
144150

145151
template<typename T>
146152
inline bool CPPInstance_CheckExact(T* object)

bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ PyObject* Instance_FromVoidPtr(
3535
#include <utility>
3636
#include <vector>
3737

38+
#if PY_VERSION_HEX < 0x030b0000
39+
namespace CPyCppyy {
40+
extern dict_lookup_func gDictLookupOrg;
41+
dict_lookup_func gDictLookupOrg = nullptr;
42+
} // namespace CPyCppyy
43+
#endif
3844

3945
// Note: as of py3.11, dictionary objects no longer carry a function pointer for
4046
// the lookup, so it can no longer be shimmed and "from cppyy.interactive import *"

bindings/pyroot/cppyy/CPyCppyy/src/Dimensions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef CPYCPPYY_DIMENSIONS_H
22
#define CPYCPPYY_DIMENSIONS_H
33

4+
// Bindings
5+
#include "CPyCppyy/CommonDefs.h"
6+
47
// Standard
58
#include <algorithm>
69
#include <initializer_list>

bindings/pyroot/cppyy/CPyCppyy/src/Utility.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
//- data _____________________________________________________________________
2626
#if PY_VERSION_HEX < 0x030b0000
27-
dict_lookup_func CPyCppyy::gDictLookupOrg = 0;
2827
bool CPyCppyy::gDictLookupActive = false;
2928
#endif
3029

bindings/pyroot/cppyy/CPyCppyy/src/Utility.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace CPyCppyy {
1313
class PyCallable;
1414

1515
#if PY_VERSION_HEX < 0x030b0000
16-
extern dict_lookup_func gDictLookupOrg;
1716
extern bool gDictLookupActive;
1817
#endif
1918

@@ -46,7 +45,11 @@ inline PyObject* CT2CppName(PyObject* pytc, const char* cpd, bool allow_voidp)
4645
const std::string& name = CT2CppNameS(pytc, allow_voidp);
4746
if (!name.empty()) {
4847
if (name == "const char*") cpd = "";
49-
return CPyCppyy_PyText_FromString((std::string{name}+cpd).c_str());
48+
#if PY_VERSION_HEX < 0x03000000
49+
return PyString_FromString((std::string{name}+cpd).c_str());
50+
#else
51+
return PyUnicode_FromString((std::string{name}+cpd).c_str());
52+
#endif
5053
}
5154
return nullptr;
5255
}

bindings/pyroot/pythonizations/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ set(libname ROOTPythonizations)
3131

3232
add_library(${libname} SHARED ${cpp_sources})
3333

34+
# Use what is in the limited API since Python 3.11, if we build with a lower
35+
# Python version, we don't bother using the limited API.
36+
if (Python3_VERSION VERSION_GREATER_EQUAL "3.11")
37+
# On Windows we can't use the stable ABI yet: it requires linking against a
38+
# different libpython, so as long as we don't build all translation units in
39+
# the ROOT Pythonization library with the stable ABI we should not use it.
40+
if(NOT MSVC)
41+
target_compile_options(${libname} PRIVATE -DPy_LIMITED_API=0x030B0000)
42+
endif()
43+
endif()
44+
3445
# Set the suffix to '.so' and the prefix to 'lib'
3546
set_target_properties(${libname} PROPERTIES ${ROOT_LIBRARY_PROPERTIES_NO_VERSION})
3647
target_link_libraries(${libname} PUBLIC Core Tree CPyCppyy)

bindings/pyroot/pythonizations/src/CPPInstancePyz.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* For the list of contributors see $ROOTSYS/README/CREDITS. *
1010
*************************************************************************/
1111

12-
#include "PythonLimitedAPI.h"
12+
#include <Python.h>
1313

1414
// Bindings
1515
#include "CPyCppyy/API.h"

bindings/pyroot/pythonizations/src/GenericPyz.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "CPyCppyy/API.h"
1515

16-
#include "../../cppyy/CPyCppyy/src/CPyCppyy.h"
16+
#include "../../cppyy/CPyCppyy/src/Cppyy.h"
1717
#include "../../cppyy/CPyCppyy/src/Utility.h"
1818

1919
#include "PyROOTPythonize.h"

bindings/pyroot/pythonizations/src/IOHandler.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* For the list of contributors see $ROOTSYS/README/CREDITS. *
99
*************************************************************************/
1010

11-
#include "PythonLimitedAPI.h"
11+
#include <Python.h>
1212

1313
#include <fcntl.h>
1414
#ifdef _MSC_VER // Visual Studio

bindings/pyroot/pythonizations/src/PyROOTPythonize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef PYROOT_PYTHONIZE_H
1313
#define PYROOT_PYTHONIZE_H
1414

15-
#include "PythonLimitedAPI.h"
15+
#include <Python.h>
1616

1717
namespace PyROOT {
1818

0 commit comments

Comments
 (0)