From db5f13491715429b96e5e7502dc95b6eb5eda41c Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Mon, 20 Apr 2026 19:34:51 +0200 Subject: [PATCH] python: add combat shim for older distros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Newer SWIG started generating Py_NewRef() calls in its own runtime boilerplate, but Py_NewRef wasn't added until Python 3.10. The mismatch hits any distro with SWIG ≥ 4.1 but Python < 3.10. Suggested-by: Martin Belanger Signed-off-by: Daniel Wagner --- libnvme/libnvme/meson.build | 4 ++++ libnvme/libnvme/nvme.i | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/libnvme/libnvme/meson.build b/libnvme/libnvme/meson.build index de4111881a..10941a632e 100644 --- a/libnvme/libnvme/meson.build +++ b/libnvme/libnvme/meson.build @@ -6,6 +6,10 @@ # Authors: Martin Belanger # if want_python + if not cc.has_header_symbol('Python.h', 'Py_NewRef', dependencies: py3_dep) + warning('Python < 3.10 detected: providing Py_NewRef compatibility shim') + endif + r = run_command(swig, ['-version'], check: true) # Returns: "\nSWIG Version 4.1.1\n\nCompiled with ..." swig_version = r.stdout().split('\n')[1].split()[2].strip() if swig_version.version_compare('<4.1.0') diff --git a/libnvme/libnvme/nvme.i b/libnvme/libnvme/nvme.i index b2641f548d..d55a6c50db 100644 --- a/libnvme/libnvme/nvme.i +++ b/libnvme/libnvme/nvme.i @@ -10,6 +10,20 @@ clashes with the same macro defined in Python.h. */ #undef fallthrough + +#include + +/* + * Py_NewRef was added in Python 3.10; provide a compat shim for + * older versions + */ +#if PY_VERSION_HEX < 0x030a0000 +static inline PyObject *Py_NewRef(PyObject *obj) +{ + Py_INCREF(obj); + return obj; +} +#endif %} %module(docstring="Python bindings for libnvme") nvme