From: Vincent Fazio <vfazio@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: brgl@kernel.org, Vincent Fazio <vfazio@gmail.com>
Subject: [libgpiod][PATCH v2 4/8] bindings: python: use suggestions from upgrade_pythoncapi.py
Date: Thu, 23 Apr 2026 17:21:21 -0500 [thread overview]
Message-ID: <20260423222125.29097-5-vfazio@gmail.com> (raw)
In-Reply-To: <20260423222125.29097-1-vfazio@gmail.com>
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]
* Use the `Py_NewRef` function introduced in CPython 3.10 [4] to
increment and return a new reference.
[0]: https://pythoncapi-compat.readthedocs.io/en/latest/index.html
[1]: https://github.com/python/cpython/commit/09bbebea163fe7303264cf4069c51d4d2f22fde4
[2]: https://github.com/python/cpython/commit/3e7b893899bdaa294a72e894694b099a1d370765
[3]: https://docs.python.org/3/c-api/allocation.html#deprecated-aliases
[4]: https://github.com/python/cpython/commit/53a03aafd5812018a3821a2e83063fd3d6cd2576
Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/gpiod/ext/chip.c | 11 +++--------
bindings/python/gpiod/ext/common.c | 2 +-
bindings/python/gpiod/ext/request.c | 2 +-
bindings/python/tests/gpiosim/ext.c | 2 +-
4 files changed, 6 insertions(+), 11 deletions(-)
diff --git a/bindings/python/gpiod/ext/chip.c b/bindings/python/gpiod/ext/chip.c
index be20869..4d96dd8 100644
--- a/bindings/python/gpiod/ext/chip.c
+++ b/bindings/python/gpiod/ext/chip.c
@@ -172,12 +172,7 @@ static PyObject *chip_get_line_name(chip_object *self, PyObject *args)
return Py_gpiod_SetErrFromErrno();
name = gpiod_line_info_get_name(info);
- if (!name) {
- Py_INCREF(Py_None);
- line_name = Py_None;
- } else {
- line_name = PyUnicode_FromString(name);
- }
+ line_name = (name) ? PyUnicode_FromString(name) : Py_NewRef(Py_None);
gpiod_line_info_free(info);
@@ -272,7 +267,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);
@@ -282,7 +277,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
next prev parent reply other threads:[~2026-04-23 22:21 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-23 22:21 [libgpiod][PATCH v2 0/8] bindings: python: modernize C extensions Vincent Fazio
2026-04-23 22:21 ` [libgpiod][PATCH v2 1/8] bindings: python: avoid PyObject_CallMethod during chip finalize Vincent Fazio
2026-04-23 22:21 ` [libgpiod][PATCH v2 2/8] bindings: python: avoid PyObject_CallMethod during request finalize Vincent Fazio
2026-04-23 22:21 ` [libgpiod][PATCH v2 3/8] bindings: python: simplify disallowing _ext.Request from being created Vincent Fazio
2026-04-23 22:21 ` Vincent Fazio [this message]
2026-04-23 22:21 ` [libgpiod][PATCH v2 5/8] bindings: python: use PyImport_ImportModuleAttrString when available Vincent Fazio
2026-04-23 22:21 ` [libgpiod][PATCH v2 6/8] bindings: python: migrate the gpiod._ext module to multi-phase init Vincent Fazio
2026-04-23 22:21 ` [libgpiod][PATCH v2 7/8] bindings: python: tests: migrate the system " Vincent Fazio
2026-04-24 9:03 ` Bartosz Golaszewski
2026-04-24 9:05 ` Bartosz Golaszewski
2026-04-24 9:07 ` Bartosz Golaszewski
2026-04-23 22:21 ` [libgpiod][PATCH v2 8/8] bindings: python: tests: migrate the gpiosim " Vincent Fazio
2026-04-24 9:14 ` [libgpiod][PATCH v2 0/8] bindings: python: modernize C extensions Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260423222125.29097-5-vfazio@gmail.com \
--to=vfazio@gmail.com \
--cc=brgl@kernel.org \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox