[libgpiod][PATCH 5/9] bindings: python: use suggestions from upgrade_pythoncapi.py

Vincent Fazio
2026-04-22T01:21:01Z

The upgrade_pythoncapi.py script performs transforms on C extension
sources to use new syntax [0]. It's generally used with a shim header
for compatibility, but the header is not necessary for these changes.

The suggested changes are:

* Use the `Py_IsNone` function introduced in CPython 3.10 [1] over
direct comparison to `Py_None`.

* Use `PyObject_Free` instead of `PyObject_Del` since `PyObject_Del`
has been aliased to `PyObject_Free` for nearly 24 years [2] and has
been announced as deprecated [3].

[0]: https://pythoncapi-compat.readthedocs.io/en/latest/index.html
[1]: https://github.com/python/cpython/pull/25227
[2]: https://github.com/python/cpython/commit/3e7b893899bdaa294a72e894694b099a1d370765
[3]: https://docs.python.org/3/c-api/allocation.html#deprecated-aliases

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/gpiod/ext/chip.c | 4 ++--
bindings/python/gpiod/ext/common.c | 2 +-
bindings/python/gpiod/ext/request.c | 2 +-
bindings/python/tests/gpiosim/ext.c | 2 +-
4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/bindings/python/gpiod/ext/chip.c b/bindings/python/gpiod/ext/chip.c
index 9061d23..498a5ee 100644
--- a/bindings/python/gpiod/ext/chip.c
+++ b/bindings/python/gpiod/ext/chip.c
@@ -271,7 +271,7 @@ make_request_config(PyObject *consumer_obj, PyObject *event_buffer_size_obj)
return NULL;
}

- if (consumer_obj != Py_None) {
+ if (!Py_IsNone(consumer_obj)) {
consumer = PyUnicode_AsUTF8(consumer_obj);
if (!consumer) {
gpiod_request_config_free(req_cfg);
@@ -281,7 +281,7 @@ make_request_config(PyObject *consumer_obj, PyObject *event_buffer_size_obj)
gpiod_request_config_set_consumer(req_cfg, consumer);
}

- if (event_buffer_size_obj != Py_None) {
+ if (!Py_IsNone(event_buffer_size_obj)) {
event_buffer_size = PyLong_AsSize_t(event_buffer_size_obj);
if (PyErr_Occurred()) {
gpiod_request_config_free(req_cfg);
diff --git a/bindings/python/gpiod/ext/common.c b/bindings/python/gpiod/ext/common.c
index 62201b6..7d2dda7 100644
--- a/bindings/python/gpiod/ext/common.c
+++ b/bindings/python/gpiod/ext/common.c
@@ -12,7 +12,7 @@ void Py_gpiod_dealloc(PyObject *self)
if (ret < 0)
return;

- PyObject_Del(self);
+ PyObject_Free(self);
}

PyObject *Py_gpiod_SetErrFromErrno(void)
diff --git a/bindings/python/gpiod/ext/request.c b/bindings/python/gpiod/ext/request.c
index 9940382..46e07ae 100644
--- a/bindings/python/gpiod/ext/request.c
+++ b/bindings/python/gpiod/ext/request.c
@@ -286,7 +286,7 @@ static PyObject *request_read_edge_events(request_object *self, PyObject *args)
if (!ret)
return NULL;

- if (max_events_obj != Py_None) {
+ if (!Py_IsNone(max_events_obj)) {
max_events = PyLong_AsSize_t(max_events_obj);
if (PyErr_Occurred())
return NULL;
diff --git a/bindings/python/tests/gpiosim/ext.c b/bindings/python/tests/gpiosim/ext.c
index cb5611a..1ebd5af 100644
--- a/bindings/python/tests/gpiosim/ext.c
+++ b/bindings/python/tests/gpiosim/ext.c
@@ -116,7 +116,7 @@ static void chip_dealloc(PyObject *self)
if (ret < 0)
return;

- PyObject_Del(self);
+ PyObject_Free(self);
}

static PyObject *chip_dev_path(chip_object *self, void *Py_UNUSED(ignored))
--
2.43.0