From 7c7e7618dcc0dc7a2766c5ba20f526e3249ea357 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 18 Mar 2025 10:41:30 +0000 Subject: [PATCH] python: pakfire: Keep a reference to the Python context Since we are registering all sorts of callbacks to here, we cannot deallocate the Ctx object, or we won't be able to use them any more. This is being fixed by holding a reference to the Python object instead of the context itself. That will at least help us with lots of these problems. There is still, however, the problem that both the Python Ctx and Pakfire object can be dereferenced when the backend objects will remain existing. Signed-off-by: Michael Tremer --- src/python/pakfire.c | 25 +++++++++++++------------ src/python/pakfire.h | 9 +++++++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/python/pakfire.c b/src/python/pakfire.c index 8f752bf6..104bbe36 100644 --- a/src/python/pakfire.c +++ b/src/python/pakfire.c @@ -73,6 +73,10 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { &CtxType, &ctx, &path, &arch, &config, &stub)) return -1; + // Keep the reference to the context + self->ctx = ctx; + Py_INCREF(self->ctx); + // Setup flags if (stub) flags |= PAKFIRE_FLAGS_STUB; @@ -97,11 +101,8 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { Py_BEGIN_ALLOW_THREADS - // Store a reference to the context - self->ctx = pakfire_ctx_ref(ctx->ctx); - // Create a new Pakfire instance - r = pakfire_create(&self->pakfire, self->ctx, c, path, arch, flags); + r = pakfire_create(&self->pakfire, self->ctx->ctx, c, path, arch, flags); if (r < 0) { Py_BLOCK_THREADS errno = -r; @@ -124,11 +125,11 @@ static void Pakfire_dealloc(PakfireObject* self) { if (self->pakfire) pakfire_unref(self->pakfire); - if (self->ctx) - pakfire_ctx_unref(self->ctx); Py_END_ALLOW_THREADS + Py_XDECREF(self->ctx); + Py_TYPE(self)->tp_free((PyObject *)self); } @@ -180,7 +181,7 @@ static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args, PyObj return NULL; // Generate a new key - int r = pakfire_key_generate(&key, self->ctx, algo, comment); + int r = pakfire_key_generate(&key, self->ctx->ctx, algo, comment); if (r) { PyErr_SetFromErrno(PyExc_OSError); return NULL; @@ -211,7 +212,7 @@ static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) { return NULL; // Import the key - r = pakfire_key_import(&key, self->ctx, f); + r = pakfire_key_import(&key, self->ctx->ctx, f); if (r) { PyErr_SetFromErrno(PyExc_OSError); goto ERROR; @@ -241,7 +242,7 @@ static PyObject* Pakfire_whatprovides(PakfireObject* self, PyObject* args) { return NULL; // Create a new list - r = pakfire_packagelist_create(&list, self->ctx); + r = pakfire_packagelist_create(&list, self->ctx->ctx); if (r < 0) { errno = -r; PyErr_SetFromErrno(PyExc_OSError); @@ -277,7 +278,7 @@ static PyObject* Pakfire_whatrequires(PakfireObject* self, PyObject* args) { Py_BEGIN_ALLOW_THREADS // Create a new list - r = pakfire_packagelist_create(&list, self->ctx); + r = pakfire_packagelist_create(&list, self->ctx->ctx); if (r < 0) { Py_BLOCK_THREADS errno = -r; @@ -321,7 +322,7 @@ static PyObject* Pakfire_search(PakfireObject* self, PyObject* args, PyObject* k if (name_only) flags |= PAKFIRE_SEARCH_NAME_ONLY; - r = pakfire_packagelist_create(&list, self->ctx); + r = pakfire_packagelist_create(&list, self->ctx->ctx); if (r < 0) { errno = -r; PyErr_SetFromErrno(PyExc_OSError); @@ -645,7 +646,7 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* // Parse the environment if (environ) { // Create a new environment - r = pakfire_env_create(&env, self->ctx); + r = pakfire_env_create(&env, self->ctx->ctx); if (r < 0) { errno = -r; PyErr_SetFromErrno(PyExc_OSError); diff --git a/src/python/pakfire.h b/src/python/pakfire.h index df078f1f..f8487450 100644 --- a/src/python/pakfire.h +++ b/src/python/pakfire.h @@ -25,11 +25,16 @@ #include +#include "ctx.h" + typedef struct { PyObject_HEAD - struct pakfire_ctx* ctx; - struct pakfire* pakfire; + // The Python context object + CtxObject* ctx; + + // Pakfire + struct pakfire* pakfire; } PakfireObject; extern PyTypeObject PakfireType; -- 2.39.5