Skip to content

Commit 28fa501

Browse files
ChunJiaoZhaotingleby
authored andcommitted
python-binding: Fix Python 3.13+ compatibility
PyEval_InitThreads and PyEval_CallObject were deprecated since Python 3.9 and removed in Python 3.12+ and 3.13+ respectively. These APIs are unavailable on Debian 13 which ships Python 3.13. Refer to: #1135 Signed-off-by: Chun Jiao Zhao <chunjiao.zhao@siemens.com>
1 parent 81ecdb6 commit 28fa501

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/python/mraapy.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ mraa_python_isr(void (*isr)(void*), void* isr_args)
2525
if (arglist == NULL) {
2626
syslog(LOG_ERR, "gpio: Py_BuildValue NULL");
2727
} else {
28+
#if PY_VERSION_HEX >= 0x03090000
29+
ret = PyObject_CallObject((PyObject*) isr, arglist);
30+
#else
2831
ret = PyEval_CallObject((PyObject*) isr, arglist);
32+
#endif
2933
if (ret == NULL) {
30-
syslog(LOG_ERR, "gpio: PyEval_CallObject failed");
34+
syslog(LOG_ERR, "gpio: Python call failed");
3135
PyObject *pvalue, *ptype, *ptraceback;
3236
PyObject *pvalue_pystr, *ptype_pystr, *ptraceback_pystr;
3337
PyObject *pvalue_ustr, *ptype_ustr, *ptraceback_ustr;

src/python/mraapython.i

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ class Spi;
151151
// Initialise python threads, this allows use to grab the GIL when we are
152152
// required to do so
153153
Py_InitializeEx(0);
154+
#if PY_VERSION_HEX < 0x03090000
155+
// PyEval_InitThreads() is deprecated since Python 3.9 and removed in Python 3.12
156+
// In Python 3.7+, the GIL is initialized automatically by Py_Initialize()
157+
// Only call PyEval_InitThreads() for Python < 3.9
158+
// Reference: https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
154159
PyEval_InitThreads();
160+
#endif
155161
// Add mraa_init() to the module initialisation process and set isr function
156162
mraa_result_t res = mraa_init();
157163
if (res == MRAA_SUCCESS) {

0 commit comments

Comments
 (0)