]> git.ipfire.org Git - pakfire.git/commitdiff
python: Fix setting/reading the context's cache path
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 5 Feb 2025 10:28:04 +0000 (10:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 5 Feb 2025 10:28:04 +0000 (10:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/ctx.c
tests/python/ctx.py

index fb855a7b096eb9a32a04fde666cdfcf414b1c772..122fd647c18e76c76773d4ebdfeec06842d020e0 100644 (file)
@@ -216,25 +216,38 @@ static PyObject* Ctx_set_logger(CtxObject* self, PyObject* args) {
        Py_RETURN_NONE;
 }
 
-static PyObject* Ctx_set_cache_path(CtxObject* self, PyObject* value) {
+static PyObject* Ctx_get_cache_path(CtxObject* self) {
+       const char* path = NULL;
+
+       // Fetch the path
+       path = pakfire_ctx_get_cache_path(self->ctx);
+       if (!path) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       return PyUnicode_FromString(path);
+}
+
+static int Ctx_set_cache_path(CtxObject* self, PyObject* value) {
        const char* path = NULL;
        int r;
 
        // Fetch the path
        path = PyUnicode_AsUTF8(value);
        if (!path)
-               return NULL;
+               return -1;
 
        // Set the cache path
        r = pakfire_ctx_set_cache_path(self->ctx, path);
-       if (r) {
+       if (r < 0) {
                errno = -r;
                PyErr_SetFromErrno(PyExc_OSError);
 
-               return NULL;
+               return r;
        }
 
-       Py_RETURN_NONE;
+       return 0;
 }
 
 static struct PyMethodDef Ctx_methods[] = {
@@ -250,7 +263,7 @@ static struct PyMethodDef Ctx_methods[] = {
 static struct PyGetSetDef Ctx_getsetters[] = {
        {
                "cache_path",
-               NULL,
+               (getter)Ctx_get_cache_path,
                (setter)Ctx_set_cache_path,
                NULL,
                NULL,
index b0584f4779df4d6bf5147d732c5dc5219dab2ed6..ab2b77db2ab647d0a0dcbab671add0fb72145374 100755 (executable)
@@ -39,6 +39,17 @@ class CtxTests(tests.TestCase):
 
                self.assertIsInstance(ctx, pakfire.Ctx)
 
+       # Cache Path
+
+       def test_cache_path(self):
+               ctx = self.setup_ctx()
+
+               # Set the cache path
+               ctx.cache_path = "/tmp"
+
+               # Read the cache path
+               self.assertEqual(ctx.cache_path, "/tmp")
+
        # Logging
 
        def test_log_default(self):