]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
python: pakfire: Keep a reference to the Python context
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Mar 2025 10:41:30 +0000 (10:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Mar 2025 10:41:30 +0000 (10:41 +0000)
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 <michael.tremer@ipfire.org>
src/python/pakfire.c
src/python/pakfire.h

index 8f752bf6226b0d9223cded1eea35bf6136386fae..104bbe36690946b4d856c740dd22d6f996caaff9 100644 (file)
@@ -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);
index df078f1f257942695b57c5a714913906615ff3d4..f8487450b12b12a3fad077c9670696faf2bd1e6c 100644 (file)
 
 #include <pakfire/pakfire.h>
 
+#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;