Fix more unresolved symbols.
[ira/wip.git] / source4 / lib / ldb / pyldb.c
index ed77added5df786885160c4ff6c9425f2066830f..bfa152b34631c58d5831669fe00cdfbc2e648692 100644 (file)
@@ -1,11 +1,11 @@
 /*
    Unix SMB/CIFS implementation.
 
-   Swig interface to ldb.
+   Python interface to ldb.
 
    Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
    Copyright (C) 2006 Simo Sorce <idra@samba.org>
-   Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
+   Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
 
         ** NOTE! The following LGPL license applies to the ldb
         ** library. This does NOT imply that all of Samba is released
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "ldb_includes.h"
+#include "replace.h"
+#include "ldb_private.h"
+#include <Python.h>
 #include "pyldb.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;
-typedef intargfunc sizeargfunc;
+typedef intargfunc ssizeargfunc;
 #endif
 
-/* Picked out of thin air. To do this properly, we should probably have some part of the 
- * errors in LDB be allocated to bindings ? */
-#define LDB_ERR_PYTHON_EXCEPTION       142
+#ifndef Py_RETURN_NONE
+#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#endif
 
 static PyObject *PyExc_LdbError;
 
-void PyErr_SetLdbError(int ret, struct ldb_context *ldb_ctx)
-{
-       if (ret == LDB_ERR_PYTHON_EXCEPTION)
-               return; /* Python exception should already be set, just keep that */
-       PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
-}
+PyAPI_DATA(PyTypeObject) PyLdbMessage;
+PyAPI_DATA(PyTypeObject) PyLdbModule;
+PyAPI_DATA(PyTypeObject) PyLdbDn;
+PyAPI_DATA(PyTypeObject) PyLdb;
+PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
+PyAPI_DATA(PyTypeObject) PyLdbTree;
+
 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx, 
                                                           struct ldb_message_element *el, 
                                                           struct ldb_val *val)
 {
-       const struct ldb_schema_attribute *a;
        struct ldb_val new_val;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
        PyObject *ret;
        
        new_val = *val;
-       
-       if (ldb_ctx != NULL) {          
-               a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
-       
-               if (a != NULL) {
-                       if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
-                               talloc_free(mem_ctx);
-                               return NULL;
-                       }
-               }
-       } 
-       
+
        ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
        
        talloc_free(mem_ctx);
@@ -76,6 +67,14 @@ static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
        return ret;
 }
 
+/**
+ * Obtain a ldb DN from a Python object.
+ *
+ * @param mem_ctx Memory context
+ * @param object Python object
+ * @param ldb_ctx LDB context
+ * @return Whether or not the conversion succeeded
+ */
 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
                   struct ldb_context *ldb_ctx, struct ldb_dn **dn)
 {
@@ -96,12 +95,18 @@ bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
        return false;
 }
 
+/**
+ * Create a Python object from a ldb_result.
+ *
+ * @param result LDB result to convert
+ * @return Python object with converted result (a list object)
+ */
 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
 {
        PyObject *ret;
        int i;
        if (result == NULL) {
-               return Py_None;
+               Py_RETURN_NONE;
        } 
        ret = PyList_New(result->count);
        for (i = 0; i < result->count; i++) {
@@ -111,7 +116,16 @@ static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
        return ret;
 }
 
-static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, PyObject *obj)
+/**
+ * Create a LDB Result from a Python object. 
+ * If conversion fails, NULL will be returned and a Python exception set.
+ *
+ * @param mem_ctx Memory context in which to allocate the LDB Result
+ * @param obj Python object to convert
+ * @return a ldb_result, or NULL if the conversion failed
+ */
+static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, 
+                                                                                          PyObject *obj)
 {
        struct ldb_result *res;
        int i;
@@ -292,9 +306,11 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
        PyObject *py_ldb;
        struct ldb_context *ldb_ctx;
        PyLdbDnObject *py_ret;
-       const char *kwnames[] = { "ldb", "dn", NULL };
+       const char * const kwnames[] = { "ldb", "dn", NULL };
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os", (char **)kwnames, &py_ldb, &str))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
+                                        discard_const_p(char *, kwnames),
+                                        &py_ldb, &str))
                return NULL;
 
        ldb_ctx = PyLdb_AsLdbContext(py_ldb);
@@ -355,8 +371,8 @@ PyTypeObject PyLdbDn = {
 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
 {
-       PyObject *fn = context;
-       PyObject_CallFunction(fn, (char *)"(i,O)", level, PyString_FromFormatV(fmt, ap));
+       PyObject *fn = (PyObject *)context;
+       PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
 }
 
 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
@@ -368,9 +384,9 @@ static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
 
        Py_INCREF(cb);
        /* FIXME: Where do we DECREF cb ? */
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
        
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
@@ -381,7 +397,7 @@ static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
 
        ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
@@ -392,31 +408,31 @@ static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
 
        ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
-       return Py_None;
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
-       return Py_None;
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
-       return Py_None;
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
-       return Py_None;
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_repr(PyLdbObject *self)
@@ -428,7 +444,7 @@ static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
 {
        struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
        if (dn == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
        return PyLdbDn_FromDn(dn);
 }
 
@@ -437,41 +453,40 @@ static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
 {
        struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
        if (dn == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
        return PyLdbDn_FromDn(dn);
 }
 
-
 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
 {
        struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
        if (dn == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
        return PyLdbDn_FromDn(dn);
 }
 
-
 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
 {
        struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
        if (dn == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
        return PyLdbDn_FromDn(dn);
 }
 
-static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
+static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
+                                                                               const char *paramname)
 {
        const char **ret;
        int i;
        if (!PyList_Check(list)) {
-               PyErr_SetString(PyExc_TypeError, "options is not a list");
+               PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
                return NULL;
        }
        ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
        for (i = 0; i < PyList_Size(list); i++) {
                PyObject *item = PyList_GetItem(list, i);
                if (!PyString_Check(item)) {
-                       PyErr_SetString(PyExc_TypeError, "options should be strings");
+                       PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
                        return NULL;
                }
                ret[i] = PyString_AsString(item);
@@ -482,7 +497,7 @@ static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
 
 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
 {
-       const char *kwnames[] = { "url", "flags", "options", NULL };
+       const char * const kwnames[] = { "url", "flags", "options", NULL };
        char *url = NULL;
        PyObject *py_options = Py_None;
        const char **options;
@@ -490,8 +505,9 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        int ret;
        struct ldb_context *ldb;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__", (char **)kwnames,
-                                                                        &url, &flags, &py_options))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
+                                        discard_const_p(char *, kwnames),
+                                        &url, &flags, &py_options))
                return -1;
 
        ldb = PyLdb_AsLdbContext(self);
@@ -499,7 +515,7 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        if (py_options == Py_None) {
                options = NULL;
        } else {
-               options = PyList_AsStringList(ldb, py_options);
+               options = PyList_AsStringList(ldb, py_options, "options");
                if (options == NULL)
                        return -1;
        }
@@ -507,7 +523,7 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        if (url != NULL) {
                ret = ldb_connect(ldb, url, flags, options);
                if (ret != LDB_SUCCESS) {
-                       PyErr_SetLdbError(ret, ldb);
+                       PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
                        return -1;
                }
        }
@@ -520,7 +536,7 @@ static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs
 {
        PyLdbObject *ret;
        struct ldb_context *ldb;
-       ldb = ldb_init(NULL, event_context_init(NULL)); 
+       ldb = ldb_init(NULL, NULL);
        if (ldb == NULL) {
                PyErr_NoMemory();
                return NULL;
@@ -538,19 +554,21 @@ static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs
 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
 {
        char *url;
-       int flags;
+       int flags = 0;
        PyObject *py_options = Py_None;
        int ret;
        const char **options;
-       const char *kwnames[] = { "url", "flags", "options", NULL };
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO", (char **)kwnames, &url, &flags,
-                                                                        &py_options))
+       const char * const kwnames[] = { "url", "flags", "options", NULL };
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
+                                        discard_const_p(char *, kwnames),
+                                        &url, &flags, &py_options))
                return NULL;
 
        if (py_options == Py_None) {
                options = NULL;
        } else {
-               options = PyList_AsStringList(NULL, py_options);
+               options = PyList_AsStringList(NULL, py_options, "options");
                if (options == NULL)
                        return NULL;
        }
@@ -558,9 +576,9 @@ static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwa
        ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
        talloc_free(options);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
@@ -576,9 +594,9 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
        }
 
        ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
@@ -633,13 +651,11 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
        }
        
        ret = ldb_add(PyLdb_AsLdbContext(self), msg);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
-
-
 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
 {
        PyObject *py_dn;
@@ -655,9 +671,9 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
                return NULL;
 
        ret = ldb_delete(ldb, dn);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
@@ -677,9 +693,9 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
                return NULL;
 
        ret = ldb_rename(ldb, dn1, dn2);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
@@ -690,7 +706,7 @@ static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *arg
 
        ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
@@ -703,20 +719,21 @@ static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
 
        ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
 {
        if (ldif == NULL) {
-               return Py_None;
+               Py_RETURN_NONE;
        } else {
        /* We don't want this attached to the 'ldb' any more */
                talloc_steal(NULL, ldif);
-               return Py_BuildValue((char *)"(iO)", ldif->changetype, 
-                                                        PyLdbMessage_FromMessage(ldif->msg));
+               return Py_BuildValue(discard_const_p(char, "(iO)"),
+                                    ldif->changetype,
+                                    PyLdbMessage_FromMessage(ldif->msg));
        }
 }
 
@@ -758,12 +775,12 @@ static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
        a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
 
        if (a == NULL) {
-               return Py_None;
+               Py_RETURN_NONE;
        }
        
        if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
                talloc_free(mem_ctx);
-               return Py_None;
+               Py_RETURN_NONE;
        }
 
        ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
@@ -780,7 +797,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        char *expr = NULL;
        PyObject *py_attrs = Py_None;
        PyObject *py_controls = Py_None;
-       const char *kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
+       const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
        int ret;
        struct ldb_result *res;
        struct ldb_request *req;
@@ -789,8 +806,9 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        struct ldb_control **parsed_controls;
        struct ldb_dn *base;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO", (char **)kwnames,
-                                                                        &py_base, &scope, &expr, &py_attrs, &py_controls))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
+                                        discard_const_p(char *, kwnames),
+                                        &py_base, &scope, &expr, &py_attrs, &py_controls))
                return NULL;
 
        ldb_ctx = PyLdb_AsLdbContext(self);
@@ -798,7 +816,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_attrs == Py_None) {
                attrs = NULL;
        } else {
-               attrs = PyList_AsStringList(ldb_ctx, py_attrs);
+               attrs = PyList_AsStringList(ldb_ctx, py_attrs, "attrs");
                if (attrs == NULL)
                        return NULL;
        }
@@ -813,7 +831,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_controls == Py_None) {
                parsed_controls = NULL;
        } else {
-               const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
+               const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
                parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
                talloc_free(controls);
        }
@@ -836,7 +854,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        if (ret != LDB_SUCCESS) {
                talloc_free(res);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
@@ -848,6 +866,12 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        talloc_free(req);
 
+       if (ret != LDB_SUCCESS) {
+               talloc_free(res);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
+       }
+
        return PyLdbResult_FromResult(res);
 }
 
@@ -862,7 +886,7 @@ static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
        data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
 
        if (data == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
 
        /* FIXME: More interpretation */
 
@@ -881,7 +905,7 @@ static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
 
        ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_modules(PyLdbObject *self)
@@ -997,7 +1021,7 @@ static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
 }
 
 static PyGetSetDef py_ldb_getset[] = {
-       { (char *)"firstmodule", (getter)py_ldb_get_firstmodule, NULL, NULL },
+       { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
        { NULL }
 };
 
@@ -1014,7 +1038,7 @@ static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
 
        ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
        if (ret != LDB_SUCCESS) {
-               PyErr_SetLdbError(ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return -1;
        }
 
@@ -1077,19 +1101,19 @@ static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
 {
        PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
 {
        PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
 {
        PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
@@ -1097,23 +1121,25 @@ static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, P
        PyObject *py_base, *py_tree, *py_attrs;
        int ret, scope;
        struct ldb_request *req;
-       const char *kwnames[] = { "base", "scope", "tree", "attrs", NULL };
+       const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
        struct ldb_module *mod;
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO", (char **)kwnames, 
-                                                                        &py_base, &scope, &py_tree, &py_attrs))
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
+                                        discard_const_p(char *, kwnames),
+                                        &py_base, &scope, &py_tree, &py_attrs))
                return NULL;
 
        mod = self->mod;
 
        ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 
-                            scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs),
+                            scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs, "attrs"),
                             NULL /* controls */, NULL, NULL, NULL);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
 
        ret = mod->ops->search(mod, req);
        talloc_free(req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
 
        return PyLdbResult_FromResult(req->op.search.res);
 }
@@ -1136,9 +1162,9 @@ static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
        mod = PyLdbModule_AsModule(self);
        ret = mod->ops->add(mod, req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args) 
@@ -1158,9 +1184,9 @@ static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
        mod = PyLdbModule_AsModule(self);
        ret = mod->ops->modify(mod, req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args) 
@@ -1178,9 +1204,9 @@ static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
        
        ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
@@ -1200,9 +1226,9 @@ static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
        
        ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyMethodDef py_ldb_module_methods[] = {
@@ -1233,6 +1259,21 @@ PyTypeObject PyLdbModule = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
+
+/**
+ * Create a ldb_message_element from a Python object.
+ *
+ * This will accept any sequence objects that contains strings, or 
+ * a string object.
+ *
+ * A reference to set_obj will be borrowed. 
+ *
+ * @param mem_ctx Memory context
+ * @param set_obj Python object to convert
+ * @param flags ldb_message_element flags to set
+ * @param attr_name Name of the attribute
+ * @return New ldb_message_element, allocated as child of mem_ctx
+ */
 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
                                                                                           PyObject *set_obj, int flags,
                                                                                           const char *attr_name)
@@ -1250,9 +1291,7 @@ struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
                me->num_values = 1;
                me->values = talloc_array(me, struct ldb_val, me->num_values);
                me->values[0].length = PyString_Size(set_obj);
-               me->values[0].data = (uint8_t *)talloc_strndup(me->values,
-                                       PyString_AsString(set_obj),
-                                       me->values[0].length);
+               me->values[0].data = (uint8_t *)PyString_AsString(set_obj);
        } else if (PySequence_Check(set_obj)) {
                int i;
                me->num_values = PySequence_Size(set_obj);
@@ -1295,7 +1334,7 @@ static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObjec
        if (!PyArg_ParseTuple(args, "i", &i))
                return NULL;
        if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
-               return Py_None;
+               Py_RETURN_NONE;
 
        return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self), 
                                                                 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
@@ -1360,9 +1399,12 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb
        struct ldb_message_element *el;
        int flags = 0;
        char *name = NULL;
-       const char *kwnames[] = { "elements", "flags", "name", NULL };
+       const char * const kwnames[] = { "elements", "flags", "name", NULL };
        PyLdbMessageElementObject *ret;
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", (char **)kwnames, &py_elements, &flags, &name))
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
+                                        discard_const_p(char *, kwnames),
+                                        &py_elements, &flags, &name))
                return NULL;
 
        el = talloc_zero(NULL, struct ldb_message_element);
@@ -1434,7 +1476,7 @@ static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
        if (el->num_values == 1)
                return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
        else 
-               return Py_None;
+               Py_RETURN_NONE;
 }
 
 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
@@ -1465,7 +1507,7 @@ static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args
 
        ldb_msg_remove_attr(self->msg, name);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
@@ -1516,7 +1558,7 @@ static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
 
        ret = py_ldb_msg_getitem_helper(self, name);
        if (ret == NULL)
-               return Py_None;
+               Py_RETURN_NONE;
        return ret;
 }
 
@@ -1584,11 +1626,14 @@ static PyMappingMethods py_ldb_msg_mapping = {
 
 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
-       const char *kwnames[] = { "dn", NULL };
+       const char * const kwnames[] = { "dn", NULL };
        struct ldb_message *ret;
        PyObject *pydn = NULL;
        PyLdbMessageObject *py_ret;
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char **)kwnames, &pydn))
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
+                                        discard_const_p(char *, kwnames),
+                                        &pydn))
                return NULL;
 
        ret = ldb_msg_new(NULL);
@@ -1638,7 +1683,7 @@ static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *cl
 }
 
 static PyGetSetDef py_ldb_msg_getset[] = {
-       { (char *)"dn", (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
+       { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
        { NULL }
 };
 
@@ -1702,7 +1747,7 @@ PyTypeObject PyLdbTree = {
 /* Ldb_module */
 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_base, *py_attrs, *py_tree;
 
        py_base = PyLdbDn_FromDn(req->op.search.base);
@@ -1725,7 +1770,9 @@ static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
                        PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
        }
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"search", (char *)"OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
+                                       discard_const_p(char, "OiOO"),
+                                       py_base, req->op.search.scope, py_tree, py_attrs);
 
        Py_DECREF(py_attrs);
        Py_DECREF(py_tree);
@@ -1747,16 +1794,18 @@ static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
 
 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_msg;
 
-       py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.add.message);
+       py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
 
        if (py_msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"add", (char *)"O", py_msg);
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
+                                       discard_const_p(char, "O"),
+                                       py_msg);
 
        Py_DECREF(py_msg);
 
@@ -1771,16 +1820,18 @@ static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
 
 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_msg;
 
-       py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.mod.message);
+       py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
 
        if (py_msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"modify", (char *)"O", py_msg);
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
+                                       discard_const_p(char, "O"),
+                                       py_msg);
 
        Py_DECREF(py_msg);
 
@@ -1795,7 +1846,7 @@ static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
 
 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_dn;
 
        py_dn = PyLdbDn_FromDn(req->op.del.dn);
@@ -1803,7 +1854,9 @@ static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
        if (py_dn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"delete", (char *)"O", py_dn);
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
+                                       discard_const_p(char, "O"),
+                                       py_dn);
 
        if (py_result == NULL) {
                return LDB_ERR_PYTHON_EXCEPTION;
@@ -1816,7 +1869,7 @@ static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
 
 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_olddn, *py_newdn;
 
        py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
@@ -1829,7 +1882,9 @@ static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
        if (py_newdn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"rename", (char *)"OO", py_olddn, py_newdn);
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
+                                       discard_const_p(char, "OO"),
+                                       py_olddn, py_newdn);
 
        Py_DECREF(py_olddn);
        Py_DECREF(py_newdn);
@@ -1845,30 +1900,33 @@ static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
 
 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"request", (char *)"");
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
+                                       discard_const_p(char, ""));
 
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"extended", (char *)"");
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
+                                       discard_const_p(char, ""));
 
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
 static int py_module_start_transaction(struct ldb_module *mod)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"start_transaction", (char *)"");
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
+                                       discard_const_p(char, ""));
 
        if (py_result == NULL) {
                return LDB_ERR_PYTHON_EXCEPTION;
@@ -1881,10 +1939,11 @@ static int py_module_start_transaction(struct ldb_module *mod)
 
 static int py_module_end_transaction(struct ldb_module *mod)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"end_transaction", (char *)"");
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
+                                       discard_const_p(char, ""));
 
        if (py_result == NULL) {
                return LDB_ERR_PYTHON_EXCEPTION;
@@ -1897,10 +1956,11 @@ static int py_module_end_transaction(struct ldb_module *mod)
 
 static int py_module_del_transaction(struct ldb_module *mod)
 {
-       PyObject *py_ldb = mod->private_data;
+       PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result;
 
-       py_result = PyObject_CallMethod(py_ldb, (char *)"del_transaction", (char *)"");
+       py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
+                                       discard_const_p(char, ""));
 
        if (py_result == NULL) {
                return LDB_ERR_PYTHON_EXCEPTION;
@@ -1917,9 +1977,9 @@ static int py_module_destructor(struct ldb_module *mod)
        return 0;
 }
 
-static int py_module_init (struct ldb_module *mod)
+static int py_module_init(struct ldb_module *mod)
 {
-       PyObject *py_class = mod->ops->private_data;
+       PyObject *py_class = (PyObject *)mod->ops->private_data;
        PyObject *py_result, *py_next, *py_ldb;
 
        py_ldb = PyLdb_FromLdbContext(mod->ldb);
@@ -1932,7 +1992,8 @@ static int py_module_init (struct ldb_module *mod)
        if (py_next == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
 
-       py_result = PyObject_CallFunction(py_class, (char *)"OO", py_ldb, py_next);
+       py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
+                                         py_ldb, py_next);
 
        if (py_result == NULL) {
                return LDB_ERR_PYTHON_EXCEPTION;
@@ -1960,7 +2021,7 @@ static PyObject *py_register_module(PyObject *module, PyObject *args)
                return NULL;
        }
 
-       ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, (char *)"name")));
+       ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
 
        Py_INCREF(input);
        ops->private_data = input;
@@ -1978,9 +2039,9 @@ static PyObject *py_register_module(PyObject *module, PyObject *args)
 
        ret = ldb_register_module(ops);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *py_timestring(PyObject *module, PyObject *args)
@@ -2111,7 +2172,7 @@ void initldb(void)
 
        PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
 
-       PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
+       PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
        PyModule_AddObject(m, "LdbError", PyExc_LdbError);
 
        Py_INCREF(&PyLdb);