Commit adbff4eb authored by Karel Koci's avatar Karel Koci 🤘

Add support for python context

This adds support for usage with python `with` statement.
parent 700c6cf0
Pipeline #34405 passed with stage
in 18 seconds
......@@ -55,6 +55,21 @@ static int uci_init(uci_object *self, PyObject *args, PyObject *kwds) {
return 0;
static PyObject *pyuci_enter(uci_object *self, PyObject *args) {
if (!self->ctx) {
PyErr_SetString(UciException, "Entering with non-initialized object is invalid");
return NULL;
return (PyObject*)self;
static PyObject *pyuci_exit(uci_object *self, PyObject *args) {
if (self->ctx)
self->ctx = NULL;
// TODO do we need this?
static PyObject *pyuci_error(uci_object *self, PyObject *excp) {
char *str = NULL;
......@@ -404,6 +419,8 @@ static PyObject *pyuci_set_savedir(uci_object *self, PyObject *args) {
static PyMethodDef uci_methods[] = {
{"__enter__", (PyCFunction)pyuci_enter, METH_VARARGS, "Enter context"},
{"__exit__", (PyCFunction)pyuci_exit, METH_VARARGS, "Exit context"},
{"get", (PyCFunction)pyuci_get, METH_VARARGS, "Get value"},
{"get_all", (PyCFunction)pyuci_get_all, METH_VARARGS, "Get all values even for sections"},
{"set", (PyCFunction)pyuci_set, METH_VARARGS, "Set value"},
......@@ -423,7 +440,6 @@ static PyMethodDef uci_methods[] = {
{"savedir", (PyCFunction)pyuci_savedir, METH_VARARGS, "Returns current savedir"},
{"set_savedir", (PyCFunction)pyuci_set_savedir, METH_VARARGS, "Change used savedir"},
// TODO somehow allow iteration
// TODO add __enter__ and __exit__ methods for context manager support
......@@ -165,3 +165,15 @@ def test_list_configs(tmpdir):
u = uci.Uci(confdir=tmpdir.strpath)
assert u.list_configs() == ['deploy', 'test']
def test_context(tmpdir):
'Test context with Uci. This depends on working test_get.'
config testing 'testing'
option one '0'
option two '1'
with uci.Uci(confdir=tmpdir.strpath) as u:
assert u.get('test', 'testing', 'one') == '0'
assert u.get('test', 'testing', 'two') == '1'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment