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

Add support for passing tuples and lists to Uci.set

parent 4f0b365f
#include "pyhelper.h"
bool is_pystring(PyObject *pystr) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_Check(pystr);
#else
return PyString_Check(pystr);
#endif
}
const char *pystring(PyObject *pystr) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_AsUTF8(pystr);
#else
return PyString_AsString(pystr);
#endif
}
bool is_pytable(PyObject *pyobj) {
return PyList_Check(pyobj) || PyTuple_Check(pyobj);
}
Py_ssize_t pytable_size(PyObject *pyobj) {
if (PyList_Check(pyobj))
return PyList_Size(pyobj);
else if (PyTuple_Check(pyobj))
return PyTuple_Size(pyobj);
PyErr_SetNone(PyExc_NotImplementedError);
return 0;
}
const char *pytable_string(PyObject *pyobj, int index) {
PyObject *str = NULL;
if (PyList_Check(pyobj))
str = PyList_GetItem(pyobj, index);
else if (PyTuple_Check(pyobj))
str = PyTuple_GetItem(pyobj, index);
else
PyErr_SetNone(PyExc_NotImplementedError);
if (!str)
return NULL;
return pystring(str);
}
/*
* Copyright 2018, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of the PyUCI.
*
* PyUCI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PyUCI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PyUCI. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _PYHELPER_H_
#define _PYHELPER_H_
#include <Python.h>
#include <stdbool.h>
// Stupid helper functions for getting string from python str object
bool is_pystring(PyObject *pystr);
const char *pystring(PyObject *pystr);
// Functions for accessing both Lists and Tuples.
bool is_pytable(PyObject *pyobj);
Py_ssize_t pytable_size(PyObject *pyobj);
const char *pytable_string(PyObject *pyobj, int index);
#endif /* _PYHELPER_H_ */
......@@ -19,6 +19,7 @@
#include "pyuci.h"
#include <uci.h>
#include <stdio.h>
#include "pyhelper.h"
// Uci exceptions
static PyObject *UciException;
......@@ -191,18 +192,17 @@ static PyObject *pyuci_set(uci_object *self, PyObject *args) {
uci_lookup_ptr(self->ctx, &ptr, NULL, true);
if (PySet_Check(data)) {
// TODO
PyErr_SetNone(PyExc_NotImplementedError);
return NULL;
#if PY_MAJOR_VERSION >= 3
} else if (PyUnicode_Check(data)) {
ptr.value = PyUnicode_AsUTF8(data);
#else
} else if (PyString_Check(data)) {
ptr.value = PyString_AsString(data);
#endif
PyObject_Print(data, stderr, 0);
if (is_pytable(data)) {
uci_delete(self->ctx, &ptr);
int i;
for (i = 0; i < pytable_size(data); i++) {
if (!(ptr.value = pytable_string(data, i)))
return NULL;
if (uci_add_list(self->ctx, &ptr))
return pyuci_error(self, UciException);
}
} else if (is_pystring(data)) {
ptr.value = pystring(data);
if (!ptr.value)
return NULL;
if (uci_set(self->ctx, &ptr))
......
......@@ -11,6 +11,6 @@ setup(
license="MIT",
ext_modules=[
Extension("uci", ["ucimodule.c", "pyuci.c"], libraries=["uci"])
Extension("uci", ["ucimodule.c", "pyuci.c", "pyhelper.c"], libraries=["uci"])
],
)
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