Skip to content

Commit 94ced7a

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 6173e6e commit 94ced7a

File tree

12 files changed

+58
-42
lines changed

12 files changed

+58
-42
lines changed

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/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.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ inline PyObject* CT2CppName(PyObject* pytc, const char* cpd, bool allow_voidp)
4646
const std::string& name = CT2CppNameS(pytc, allow_voidp);
4747
if (!name.empty()) {
4848
if (name == "const char*") cpd = "";
49-
return CPyCppyy_PyText_FromString((std::string{name}+cpd).c_str());
49+
#if PY_VERSION_HEX < 0x03000000
50+
return PyString_FromString((std::string{name}+cpd).c_str());
51+
#else
52+
return PyUnicode_FromString((std::string{name}+cpd).c_str());
53+
#endif
5054
}
5155
return nullptr;
5256
}

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

bindings/pyroot/pythonizations/src/PythonLimitedAPI.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

bindings/pyroot/pythonizations/src/RPyROOTApplication.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*************************************************************************/
1111

1212
// Bindings
13-
#include "PythonLimitedAPI.h"
13+
#include <Python.h>
1414
#include "RPyROOTApplication.h"
1515

1616
// ROOT

0 commit comments

Comments
 (0)