Skip to content

Commit 60e41d7

Browse files
author
Martin Belanger
committed
swig: fix Py_NewRef compatibility with Python < 3.10
SWIG >= 4.1 generates calls to Py_NewRef() in its runtime boilerplate. Py_NewRef was introduced in Python 3.10, causing an undefined symbol error on older distributions (e.g. SLES 15.6/15.7 with Python 3.6). Add a static inline shim inside the %begin block, guarded by PY_VERSION_HEX, so it precedes SWIG's own runtime in the generated C file. Python.h is included explicitly at that point to make PyObject available. A meson warning is emitted at configure time when the older Python is detected. Signed-off-by: Martin Belanger <[email protected]> Assisted-by: Claude Sonnet 4.6 <[email protected]>
1 parent ff76cc9 commit 60e41d7

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

libnvme/libnvme/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
# Authors: Martin Belanger <[email protected]>
77
#
88
if want_python
9+
if not cc.has_header_symbol('Python.h', 'Py_NewRef', dependencies: py3_dep)
10+
warning('Python < 3.10 detected: Py_NewRef compatibility shim will be used')
11+
endif
12+
913
r = run_command(swig, ['-version'], check: true) # Returns: "\nSWIG Version 4.1.1\n\nCompiled with ..."
1014
swig_version = r.stdout().split('\n')[1].split()[2].strip()
1115
if swig_version.version_compare('<4.1.0')

libnvme/libnvme/nvme.i

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
clashes with the same macro defined in Python.h.
1111
*/
1212
#undef fallthrough
13+
14+
#include <Python.h>
15+
16+
/* WORKAROUND: Py_NewRef() was introduced in Python 3.10. SWIG >= 4.1 generates
17+
calls to it in its runtime boilerplate, which breaks older
18+
distributions (e.g. SLES 15.6/15.7 with Python 3.6).
19+
*/
20+
#if PY_VERSION_HEX < 0x030a0000
21+
static inline PyObject *Py_NewRef(PyObject *obj)
22+
{
23+
Py_INCREF(obj);
24+
return obj;
25+
}
26+
#endif
1327
%}
1428

1529
%module(docstring="Python bindings for libnvme") nvme

0 commit comments

Comments
 (0)