bindings: python: add missing Py_None reference increment
PATCH 1/1 bindings: python: add missing Py_None reference increment
For lines with no name, chip_get_line_name() returns Py_None but does not increment its reference count. Do it enough times and the following error will occur with cpython before v3.12:
Fatal Python error: none_dealloc: deallocating None: bug likely caused by a refcount error in a C extension
Add the missing Py_INCREF(). It's safe to do even on python >= v3.12 as Py_INCREF() has no effect on immortal objects in more recent versions.
--- bindings/python/gpiod/ext/chip.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
bindings/python/gpiod/ext/chip.c +6 -1
@@ -166,7 +166,12 @@ static PyObject *chip_get_line_name(chip_object *self, PyObject *args)return Py_gpiod_SetErrFromErrno (); name =gpiod_line_info_get_name (info); - line_name = (name ==NULL ) ? Py_None :PyUnicode_FromString (name); +if (!name) { +Py_INCREF (Py_None); + line_name = Py_None; + }else { + line_name =PyUnicode_FromString (name); + }gpiod_line_info_free (info);
Signature · 1 line
2.47.3
Bartosz Golaszewski
AppliedQuoted 10 lines · Bartosz Golaszewski
For lines with no name,
chip_get_line_name()returnsPy_Nonebut does not increment its reference count. Do it enough times and the following error will occur with cpython before v3.12:Fatal Python error:
none_dealloc: deallocating None: bug likely caused by a refcount error in a C extensionAdd the missing
Py_INCREF(). It's safe to do even on python >= v3.12 asPy_INCREF()has no effect on immortal objects in more recent versions.[...]
Applied, thanks!
[1/1] bindings: python: add missing Py_None reference increment
commit: 8a3058296d95c55f42578463e231693887fccbad
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>