pytalloc: allow for using a system libtalloc-dev with pytalloc
[kai/samba-autobuild/.git] / source4 / param / pyparam.c
index efaedf7b4158120e14f4fae5fa8044f7f705bbed..40d25f7dcd76499789879b8f9b0328e47dacf746 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <stdint.h>
-#include <stdbool.h>
-
+#include <Python.h>
 #include "includes.h"
 #include "param/param.h"
 #include "param/loadparm.h"
-#include <Python.h>
-#include "pytalloc.h"
+#include "lib/talloc/pytalloc.h"
 
 /* There's no Py_ssize_t in 2.4, apparently */
 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
@@ -32,10 +29,6 @@ typedef int Py_ssize_t;
 typedef inquiry lenfunc;
 #endif
 
-#ifndef Py_RETURN_NONE
-#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
-#endif
-
 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
 
 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
@@ -43,7 +36,7 @@ PyAPI_DATA(PyTypeObject) PyLoadparmService;
 
 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
 {
-       return py_talloc_import(&PyLoadparmService, service);
+       return py_talloc_reference(&PyLoadparmService, service);
 }
 
 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
@@ -129,7 +122,13 @@ static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const cha
        {
            int j;
            const char **strlist = *(const char ***)parm_ptr;
-           PyObject *pylist = PyList_New(str_list_length(strlist));
+           PyObject *pylist;
+               
+               if(strlist == NULL) {
+                       return PyList_New(0);
+               }
+               
+               pylist = PyList_New(str_list_length(strlist));
            for (j = 0; strlist[j]; j++) 
                PyList_SetItem(pylist, j, 
                               PyString_FromString(strlist[j]));
@@ -232,6 +231,21 @@ static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
        return ret;
 }
 
+static PyObject *py_lp_ctx_services(py_talloc_Object *self)
+{
+       struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
+       PyObject *ret;
+       int i;
+       ret = PyList_New(lp_numservices(lp_ctx));
+       for (i = 0; i < lp_numservices(lp_ctx); i++) {
+               struct loadparm_service *service = lp_servicebynum(lp_ctx, i);
+               if (service != NULL) {
+                       PyList_SetItem(ret, i, PyString_FromString(lp_servicename(service)));
+               }
+       }
+       return ret;
+}
+
 static PyMethodDef py_lp_ctx_methods[] = {
        { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
                "S.load(filename) -> None\n"
@@ -253,6 +267,8 @@ static PyMethodDef py_lp_ctx_methods[] = {
                "Change a parameter." },
        { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
                "S.private_path(name) -> path\n" },
+       { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
+               "S.services() -> list" },
        { NULL }
 };
 
@@ -279,7 +295,18 @@ static PyGetSetDef py_lp_ctx_getset[] = {
 
 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
-       return py_talloc_import(type, loadparm_init(NULL));
+       py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
+       if (ret == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       ret->talloc_ctx = talloc_new(NULL);
+       if (ret->talloc_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       ret->ptr = loadparm_init(ret->talloc_ctx);
+       return (PyObject *)ret;
 }
 
 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
@@ -325,39 +352,6 @@ PyTypeObject PyLoadparmService = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
-_PUBLIC_ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
-{
-    struct loadparm_context *lp_ctx;
-    if (PyString_Check(py_obj)) {
-        lp_ctx = loadparm_init(NULL);
-        if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
-            talloc_free(lp_ctx);
-           PyErr_Format(PyExc_RuntimeError, 
-                        "Unable to load %s", PyString_AsString(py_obj));
-            return NULL;
-        }
-        return lp_ctx;
-    }
-
-    if (py_obj == Py_None) {
-        lp_ctx = loadparm_init(NULL);
-       /* We're not checking that loading the file succeeded *on purpose */
-        lp_load_default(lp_ctx);
-        return lp_ctx;
-    }
-
-    return PyLoadparmContext_AsLoadparmContext(py_obj);
-}
-
-struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx)
-{
-    struct loadparm_context *ret;
-    ret = loadparm_init(mem_ctx);
-    if (!lp_load_default(ret))
-        return NULL;
-    return ret;
-}
-
 static PyObject *py_default_path(PyObject *self)
 {
     return PyString_FromString(lp_default_path());
@@ -376,6 +370,9 @@ void initparam(void)
        if (PyType_Ready(&PyLoadparmContext) < 0)
                return;
 
+       if (PyType_Ready(&PyLoadparmService) < 0)
+               return;
+
        m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
        if (m == NULL)
                return;