[libgpiod][PATCH 3/9] bindings: python: avoid PyObject_CallMethod during request finalize

Vincent Fazio
2026-04-22T01:20:59Z

The PyObject_CallMethod function jumps through hoops to find the method
to call and to build a stack (if necessary) to call a given method.

In the case of finalizing the request object, that overhead can be
avoided by simply sharing a function with `request_release` to close the
underlying gpiod_line_request object.

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/gpiod/ext/request.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/bindings/python/gpiod/ext/request.c b/bindings/python/gpiod/ext/request.c
index 9acf828..1e1f65a 100644
--- a/bindings/python/gpiod/ext/request.c
+++ b/bindings/python/gpiod/ext/request.c
@@ -22,10 +22,19 @@ static int request_init(PyObject *Py_UNUSED(ignored0),
return -1;
}

+static void internal_request_release(request_object *self)
+{
+ if (self->request){
+ Py_BEGIN_ALLOW_THREADS;
+ gpiod_line_request_release(self->request);
+ Py_END_ALLOW_THREADS;
+ self->request = NULL;
+ }
+}
+
static void request_finalize(request_object *self)
{
- if (self->request)
- PyObject_CallMethod((PyObject *)self, "release", "");
+ internal_request_release(self);

if (self->offsets)
PyMem_Free(self->offsets);
@@ -121,10 +130,7 @@ static PyGetSetDef request_getset[] = {
static PyObject *
request_release(request_object *self, PyObject *Py_UNUSED(ignored))
{
- Py_BEGIN_ALLOW_THREADS;
- gpiod_line_request_release(self->request);
- Py_END_ALLOW_THREADS;
- self->request = NULL;
+ internal_request_release(self);

Py_RETURN_NONE;
}
--
2.43.0