Skip to contentSkip to conversation

bindings: python: add missing Py_None reference increment

PATCH Applied Bartosz Golaszewski 1 participant 2 messages Jan 13 GPIO

lore
Share & tools
lore
#0 lore raw

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.

Fixes 0c0993569c54 ("bindings: python: ext: add ability to query line name")
---
 bindings/python/gpiod/ext/chip.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
bindings/python/gpiod/ext/chip.c +6 -1
index 98435d9..53c4b80 100644
@@ -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
author Applied
#1 lore raw
Quoted 10 lines · Bartosz Golaszewski

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.

[...]

Applied, thanks!

[1/1] bindings: python: add missing Py_None reference increment

      commit: 8a3058296d95c55f42578463e231693887fccbad

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>