From: Michael Tremer Date: Wed, 5 Feb 2025 10:28:04 +0000 (+0000) Subject: python: Fix setting/reading the context's cache path X-Git-Tag: 0.9.30~128 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f2cd62ade9622cb877ac0011b9954df35eb4ef9d;p=pakfire.git python: Fix setting/reading the context's cache path Signed-off-by: Michael Tremer --- diff --git a/src/python/ctx.c b/src/python/ctx.c index fb855a7b..122fd647 100644 --- a/src/python/ctx.c +++ b/src/python/ctx.c @@ -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, diff --git a/tests/python/ctx.py b/tests/python/ctx.py index b0584f47..ab2b77db 100755 --- a/tests/python/ctx.py +++ b/tests/python/ctx.py @@ -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):