Avoid using a utility header for Python replacements included in Samba,
[kai/samba-autobuild/.git] / source4 / param / pyparam.c
index 69a9263c2317ceb796a289ea822d0a52169a4bf1..2d5a584fb2709656573de95464e83b022ecd959d 100644 (file)
 #include "includes.h"
 #include "param/param.h"
 #include "param/loadparm.h"
+#include <Python.h>
 #include "pytalloc.h"
 
+/* There's no Py_ssize_t in 2.4, apparently */
+#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
+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_ptr(obj)
-#define PyLoadparmContext_Check(obj) PyObject_TypeCheck(obj, &PyLoadparmContext)
 
 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
 PyAPI_DATA(PyTypeObject) PyLoadparmService;
@@ -142,10 +152,10 @@ static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
        ret = lp_load((struct loadparm_context *)self->ptr, filename);
 
        if (!ret) {
-               PyErr_SetString(PyExc_RuntimeError, "Unable to load file");
+               PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
                return NULL;
        }
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
@@ -154,10 +164,10 @@ static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
         ret = lp_load_default(self->ptr);
 
        if (!ret) {
-               PyErr_SetString(PyExc_RuntimeError, "Unable to load file");
+               PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
                return NULL;
        }
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
@@ -170,7 +180,7 @@ static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
 
        ret = py_lp_ctx_get_helper(self->ptr, section_name, param_name);
        if (ret == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
        return ret;
 }
 
@@ -205,7 +215,7 @@ static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
                return NULL;
         }
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
@@ -311,13 +321,15 @@ PyTypeObject PyLoadparmService = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
-struct loadparm_context *lp_from_py_object(PyObject *py_obj)
+_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;
@@ -325,10 +337,8 @@ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
 
     if (py_obj == Py_None) {
         lp_ctx = loadparm_init(NULL);
-        if (!lp_load_default(lp_ctx)) {
-            talloc_free(lp_ctx);
-            return NULL;
-        }
+       /* We're not checking that loading the file succeeded *on purpose */
+        lp_load_default(lp_ctx);
         return lp_ctx;
     }