]> git.ipfire.org Git - pakfire.git/commitdiff
python: Take configuration as string and fix return values
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 15:41:20 +0000 (15:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 15:41:36 +0000 (15:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/pakfire.c

index 0c04a31b141df7ba04a9dd61ac8c696821561e88..007a1c08b821cdc8b72b21f7cf1159708c50fba9 100644 (file)
@@ -59,19 +59,17 @@ static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
 }
 
 static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
+       const char* kwlist[] = { "ctx", "path", "arch", "config", NULL };
        struct pakfire_config* c = NULL;
-       const char* kwlist[] = { "ctx", "path", "arch", "conf", NULL };
-       CtxObject* ctx = NULL;
+       const char* config = NULL;
        const char* path = NULL;
        const char* arch = NULL;
-       PyObject* conf = Py_None;
-       int r = 1;
+       CtxObject* ctx = NULL;
+       int r;
 
-       FILE* f = NULL;
-
-       if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|zzO", (char**)kwlist,
-                       &CtxType, &ctx, &path, &arch, &conf))
-               goto ERROR;
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|zzz", (char**)kwlist,
+                       &CtxType, &ctx, &path, &arch, &config))
+               return -1;
 
        // Create a new configuration object
        r = pakfire_config_create(&c);
@@ -80,29 +78,24 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                goto ERROR;
        }
 
-       // Map the configuration
-       if (conf != Py_None) {
-               f = PyObject_AsFileHandle(conf, "r");
-               if (!f)
-                       goto ERROR;
-
-               // Read the configuration
-               r = pakfire_config_read(c, f);
+       // Parse the configuration
+       if (config) {
+               r = pakfire_config_parse(c, config, -1);
                if (r < 0) {
+                       errno = -r;
+
                        PyErr_SetFromErrno(PyExc_OSError);
                        goto ERROR;
                }
        }
 
-       int flags = 0;
-
        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, c, path, arch, 0);
 
        Py_END_ALLOW_THREADS
 
@@ -113,24 +106,20 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                        // Invalid architecture or path
                        case EINVAL:
                                PyErr_SetString(PyExc_ValueError, "Invalid architecture or path");
-                               break;
+                               goto ERROR;
 
                        // Anything else
                        default:
                                PyErr_SetFromErrno(PyExc_OSError);
+                               goto ERROR;
                }
-
-               r = -1;
-               goto ERROR;
     }
 
 ERROR:
        if (c)
                pakfire_config_unref(c);
-       if (f)
-               fclose(f);
 
-       return -r;
+       return r;
 }
 
 static void Pakfire_dealloc(PakfireObject* self) {