pyldb: Avoid using PyErr_LDB_ERROR_IS_ERR_RAISE where PyErr_SetLdbError suffices.
[ira/wip.git] / lib / ldb / pyldb.c
index e2a2e7180e1de8bc2994a7752faeb934ece79d2b..a2a5dffd25fc29610aaed5ba98449eb330abd8d5 100644 (file)
@@ -29,7 +29,6 @@
 */
 
 #include <Python.h>
-#include <pytalloc.h>
 #include "ldb_private.h"
 #include "pyldb.h"
 
@@ -40,10 +39,15 @@ static PyObject *PyExc_LdbError;
 staticforward PyTypeObject PyLdbControl;
 staticforward PyTypeObject PyLdbResult;
 staticforward PyTypeObject PyLdbMessage;
+#define PyLdbMessage_Check(ob) PyObject_TypeCheck(ob, &PyLdbMessage)
 staticforward PyTypeObject PyLdbModule;
 staticforward PyTypeObject PyLdbDn;
+#define pyldb_Dn_Check(ob) PyObject_TypeCheck(ob, &PyLdbDn)
 staticforward PyTypeObject PyLdb;
+#define PyLdb_Check(ob) PyObject_TypeCheck(ob, &PyLdb)
 staticforward PyTypeObject PyLdbMessageElement;
+#define pyldb_MessageElement_Check(ob) PyObject_TypeCheck(ob, &PyLdbMessageElement)
+
 staticforward PyTypeObject PyLdbTree;
 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
 static PyObject *PyLdbModule_FromModule(struct ldb_module *mod);
@@ -132,7 +136,7 @@ static PyObject *py_ldb_control_new(PyTypeObject *type, PyObject *args, PyObject
                return NULL;
        }
 
-       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
        parsed_controls = ldb_parse_control_from_string(ldb_ctx, mem_ctx, data);
 
        if (!parsed_controls) {
@@ -320,9 +324,12 @@ static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
 }
 
 /**
- * Create a LDB Result from a Python object. 
+ * Create a LDB Result from a Python object.
  * If conversion fails, NULL will be returned and a Python exception set.
  *
+ * Note: the result object only includes the messages at the moment; extended
+ * result, controls and referrals are ignored.
+ *
  * @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
@@ -341,7 +348,7 @@ static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
        res->msgs = talloc_array(res, struct ldb_message *, res->count);
        for (i = 0; i < res->count; i++) {
                PyObject *item = PyList_GetItem(obj, i);
-               res->msgs[i] = PyLdbMessage_AsMessage(item);
+               res->msgs[i] = pyldb_Message_AsMessage(item);
        }
        return res;
 }
@@ -468,7 +475,7 @@ static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
 
 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
 {
-       struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
+       struct ldb_dn *dn = pyldb_Dn_AsDn((PyObject *)self);
        struct ldb_dn *parent;
        PyLdbDnObject *py_ret;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
@@ -499,9 +506,9 @@ static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "O", &py_other))
                return NULL;
 
-       dn = PyLdbDn_AsDn((PyObject *)self);
+       dn = pyldb_Dn_AsDn((PyObject *)self);
 
-       if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
+       if (!pyldb_Object_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
                return NULL;
 
        return ldb_dn_add_child(dn, other)?Py_True:Py_False;
@@ -514,14 +521,29 @@ static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "O", &py_other))
                return NULL;
 
-       dn = PyLdbDn_AsDn((PyObject *)self);
+       dn = pyldb_Dn_AsDn((PyObject *)self);
 
-       if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
+       if (!pyldb_Object_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
                return NULL;
 
        return ldb_dn_add_base(dn, other)?Py_True:Py_False;
 }
 
+static PyObject *py_ldb_dn_is_child_of(PyLdbDnObject *self, PyObject *args)
+{
+       PyObject *py_base;
+       struct ldb_dn *dn, *base;
+       if (!PyArg_ParseTuple(args, "O", &py_base))
+               return NULL;
+
+       dn = pyldb_Dn_AsDn((PyObject *)self);
+
+       if (!pyldb_Object_AsDn(NULL, py_base, dn_ldb_ctx(dn), &base))
+               return NULL;
+
+       return PyBool_FromLong(ldb_dn_compare_base(base, dn) == 0);
+}
+
 static PyMethodDef py_ldb_dn_methods[] = {
        { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS, 
                "S.validate() -> bool\n"
@@ -540,6 +562,8 @@ static PyMethodDef py_ldb_dn_methods[] = {
        { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
                "S.canonical_str() -> string\n"
                "Canonical version of this DN (like a posix path)." },
+       { "is_child_of", (PyCFunction)py_ldb_dn_is_child_of, METH_VARARGS,
+               "S.is_child_of(basedn) -> int\nReturns True if this DN is a child of basedn\n"},
        { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
                "S.canonical_ex_str() -> string\n"
                "Canonical version of this DN (like a posix path, with terminating newline)." },
@@ -569,16 +593,33 @@ static PyMethodDef py_ldb_dn_methods[] = {
 
 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
 {
-       return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
+       return ldb_dn_get_comp_num(pyldb_Dn_AsDn((PyObject *)self));
+}
+
+/*
+  copy a DN as a python object
+ */
+static PyObject *py_ldb_dn_copy(struct ldb_dn *dn)
+{
+       PyLdbDnObject *py_ret;
+
+       py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
+       if (py_ret == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       py_ret->mem_ctx = talloc_new(NULL);
+       py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
+       return (PyObject *)py_ret;
 }
 
 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
 {
-       struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self), 
+       struct ldb_dn *dn = pyldb_Dn_AsDn((PyObject *)self), 
                                  *other;
        PyLdbDnObject *py_ret;
-       
-       if (!PyObject_AsDn(NULL, py_other, NULL, &other))
+
+       if (!pyldb_Object_AsDn(NULL, py_other, NULL, &other))
                return NULL;
 
        py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
@@ -612,7 +653,7 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
                                         &py_ldb, &str))
                return NULL;
 
-       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
 
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
@@ -675,7 +716,7 @@ 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(PyExc_LdbError, 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_Ldb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -686,7 +727,7 @@ static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "I", &perms))
                return NULL;
 
-       ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
+       ldb_set_create_perms(pyldb_Ldb_AsLdbContext(self), perms);
 
        Py_RETURN_NONE;
 }
@@ -697,38 +738,38 @@ static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "s", &modules_dir))
                return NULL;
 
-       ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
+       ldb_set_modules_dir(pyldb_Ldb_AsLdbContext(self), modules_dir);
 
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(pyldb_Ldb_AsLdbContext(self)), pyldb_Ldb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(pyldb_Ldb_AsLdbContext(self)), pyldb_Ldb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_prepare_commit(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(pyldb_Ldb_AsLdbContext(self)), pyldb_Ldb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(pyldb_Ldb_AsLdbContext(self)), pyldb_Ldb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(pyldb_Ldb_AsLdbContext(self)), pyldb_Ldb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
@@ -739,35 +780,35 @@ static PyObject *py_ldb_repr(PyLdbObject *self)
 
 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_root_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 
 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_schema_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_config_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_default_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
@@ -813,7 +854,7 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
                                         &url, &flags, &py_options))
                return -1;
 
-       ldb = PyLdb_AsLdbContext(self);
+       ldb = pyldb_Ldb_AsLdbContext(self);
 
        if (py_options == Py_None) {
                options = NULL;
@@ -878,10 +919,11 @@ static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwa
                        return NULL;
        }
 
-       ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
+       ret = ldb_connect(pyldb_Ldb_AsLdbContext(self), url, flags, options);
        talloc_free(options);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret,
+                                                                pyldb_Ldb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -909,7 +951,7 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -924,12 +966,12 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwar
                talloc_free(mem_ctx);
                return NULL;
        }
-       msg = PyLdbMessage_AsMessage(py_msg);
+       msg = pyldb_Message_AsMessage(py_msg);
 
        if (validate) {
                ret = ldb_msg_sanity_check(ldb_ctx, msg);
                if (ret != LDB_SUCCESS) {
-                       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+                       PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                        talloc_free(mem_ctx);
                        return NULL;
                }
@@ -949,22 +991,19 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwar
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
        if (ret == LDB_SUCCESS) {
-                       ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
 
        if (ret == LDB_SUCCESS) {
                ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -999,7 +1038,7 @@ static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
        msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_obj));
 
        if (dn_value) {
-               if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
+               if (!pyldb_Object_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
                        PyErr_SetString(PyExc_TypeError, "unable to import dn object");
                        return NULL;
                }
@@ -1014,7 +1053,7 @@ static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
 
        while (PyDict_Next(py_obj, &dict_pos, &key, &value)) {
                char *key_str = PyString_AsString(key);
-               if (strcmp(key_str, "dn") != 0) {
+               if (ldb_attr_cmp(key_str, "dn") != 0) {
                        msg_el = PyObject_AsMessageElement(msg->elements, value,
                                                           mod_flags, key_str);
                        if (msg_el == NULL) {
@@ -1053,7 +1092,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -1064,7 +1103,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        }
 
        if (PyLdbMessage_Check(py_obj)) {
-               msg = PyLdbMessage_AsMessage(py_obj);
+               msg = pyldb_Message_AsMessage(py_obj);
        } else if (PyDict_Check(py_obj)) {
                msg = PyDict_AsMessage(mem_ctx, py_obj, ldb_ctx, LDB_FLAG_MOD_ADD);
        } else {
@@ -1080,7 +1119,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
 
        ret = ldb_msg_sanity_check(ldb_ctx, msg);
        if (ret != LDB_SUCCESS) {
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -1099,22 +1138,19 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
        if (ret == LDB_SUCCESS) {
-                       ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        } 
 
        if (ret == LDB_SUCCESS) {
-                       ret = ldb_transaction_commit(ldb_ctx);
+               ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1145,7 +1181,7 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -1155,7 +1191,7 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
                talloc_free(controls);
        }
 
-       if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
+       if (!pyldb_Object_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -1174,7 +1210,8 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
@@ -1186,10 +1223,6 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
                ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1203,7 +1236,6 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        PyObject *py_dn1, *py_dn2;
        struct ldb_dn *dn1, *dn2;
        int ret;
-       struct ldb_context *ldb;
        TALLOC_CTX *mem_ctx;
        PyObject *py_controls = Py_None;
        struct ldb_control **parsed_controls;
@@ -1211,7 +1243,7 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        struct ldb_request *req;
        const char * const kwnames[] = { "dn1", "dn2", "controls", NULL };
 
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O",
                                         discard_const_p(char *, kwnames),
@@ -1224,7 +1256,6 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb = PyLdb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -1235,12 +1266,12 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        }
 
 
-       if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
+       if (!pyldb_Object_AsDn(mem_ctx, py_dn1, ldb_ctx, &dn1)) {
                talloc_free(mem_ctx);
                return NULL;
        }
 
-       if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
+       if (!pyldb_Object_AsDn(mem_ctx, py_dn2, ldb_ctx, &dn2)) {
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -1259,7 +1290,8 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
@@ -1271,10 +1303,6 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
                ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1289,7 +1317,7 @@ static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *arg
        if (!PyArg_ParseTuple(args, "s", &name))
                return NULL;
 
-       ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
+       ldb_schema_attribute_remove(pyldb_Ldb_AsLdbContext(self), name);
 
        Py_RETURN_NONE;
 }
@@ -1302,9 +1330,9 @@ static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
                return NULL;
 
-       ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
+       ret = ldb_schema_attribute_add(pyldb_Ldb_AsLdbContext(self), attribute, flags, syntax);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, pyldb_Ldb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -1339,12 +1367,12 @@ static PyObject *py_ldb_write_ldif(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       ldif.msg = PyLdbMessage_AsMessage(py_msg);
+       ldif.msg = pyldb_Message_AsMessage(py_msg);
        ldif.changetype = changetype;
 
        mem_ctx = talloc_new(NULL);
 
-       string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif);
+       string = ldb_ldif_write_string(pyldb_Ldb_AsLdbContext(self), mem_ctx, &ldif);
        if (!string) {
                PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
                return NULL;
@@ -1411,10 +1439,10 @@ static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       ldb = PyLdb_AsLdbContext(self);
+       ldb = pyldb_Ldb_AsLdbContext(self);
        ldb_ret = ldb_msg_difference(ldb, ldb,
-                                    PyLdbMessage_AsMessage(py_msg_old),
-                                    PyLdbMessage_AsMessage(py_msg_new),
+                                    pyldb_Message_AsMessage(py_msg_old),
+                                    pyldb_Message_AsMessage(py_msg_new),
                                     &diff);
        if (ldb_ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
@@ -1446,13 +1474,13 @@ static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
        old_val.data = (uint8_t *)PyString_AsString(val);
        old_val.length = PyString_Size(val);
 
-       a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
+       a = ldb_schema_attribute_by_name(pyldb_Ldb_AsLdbContext(self), element_name);
 
        if (a == NULL) {
                Py_RETURN_NONE;
        }
 
-       if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
+       if (a->syntax->ldif_write_fn(pyldb_Ldb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
                talloc_free(mem_ctx);
                Py_RETURN_NONE;
        }
@@ -1494,7 +1522,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_attrs == Py_None) {
                attrs = NULL;
@@ -1509,7 +1537,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_base == Py_None) {
                base = ldb_get_default_basedn(ldb_ctx);
        } else {
-               if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
+               if (!pyldb_Object_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
                        talloc_free(attrs);
                        return NULL;
                }
@@ -1542,7 +1570,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
@@ -1556,7 +1584,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
@@ -1575,7 +1603,7 @@ static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "s", &name))
                return NULL;
 
-       data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
+       data = ldb_get_opaque(pyldb_Ldb_AsLdbContext(self), name);
 
        if (data == NULL)
                Py_RETURN_NONE;
@@ -1595,14 +1623,14 @@ static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
 
        /* FIXME: More interpretation */
 
-       ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
+       ldb_set_opaque(pyldb_Ldb_AsLdbContext(self), name, data);
 
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_modules(PyLdbObject *self)
 {
-       struct ldb_context *ldb = PyLdb_AsLdbContext(self);
+       struct ldb_context *ldb = pyldb_Ldb_AsLdbContext(self);
        PyObject *ret = PyList_New(0);
        struct ldb_module *mod;
 
@@ -1615,7 +1643,7 @@ static PyObject *py_ldb_modules(PyLdbObject *self)
 
 static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
 {
-       struct ldb_context *ldb = PyLdb_AsLdbContext(self);
+       struct ldb_context *ldb = pyldb_Ldb_AsLdbContext(self);
        int type, ret;
        uint64_t value;
 
@@ -1626,10 +1654,8 @@ static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
 
        ret = ldb_sequence_number(ldb, type, &value);
 
-       if (ret != LDB_SUCCESS) {
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
-               return NULL;
-       }
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
+
        return PyLong_FromLongLong(value);
 }
 static PyMethodDef py_ldb_methods[] = {
@@ -1740,7 +1766,7 @@ static PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
 
 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
 {
-       return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
+       return PyLdbModule_FromModule(pyldb_Ldb_AsLdbContext(self)->modules);
 }
 
 static PyGetSetDef py_ldb_getset[] = {
@@ -1750,13 +1776,13 @@ static PyGetSetDef py_ldb_getset[] = {
 
 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
 {
-       struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
        struct ldb_dn *dn;
        struct ldb_result *result;
        unsigned int count;
        int ret;
 
-       if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
+       if (!pyldb_Object_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
                return -1;
        }
 
@@ -1908,29 +1934,30 @@ static PyTypeObject PyLdbResult = {
 
 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
 {
-       return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
+       return PyString_FromFormat("<ldb module '%s'>",
+               pyldb_Module_AsModule(self)->ops->name);
 }
 
 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
 {
-       return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
+       return PyString_FromString(pyldb_Module_AsModule(self)->ops->name);
 }
 
 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
 {
-       PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
+       pyldb_Module_AsModule(self)->ops->start_transaction(pyldb_Module_AsModule(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
 {
-       PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
+       pyldb_Module_AsModule(self)->ops->end_transaction(pyldb_Module_AsModule(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
 {
-       PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
+       pyldb_Module_AsModule(self)->ops->del_transaction(pyldb_Module_AsModule(self));
        Py_RETURN_NONE;
 }
 
@@ -1959,7 +1986,7 @@ static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, P
                        return NULL;
        }
 
-       ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 
+       ret = ldb_build_search_req(&req, mod->ldb, NULL, pyldb_Dn_AsDn(py_base), 
                             scope, NULL /* expr */, attrs,
                             NULL /* controls */, NULL, NULL, NULL);
 
@@ -1993,9 +2020,9 @@ static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
 
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_ADD;
-       req->op.add.message = PyLdbMessage_AsMessage(py_message);
+       req->op.add.message = pyldb_Message_AsMessage(py_message);
 
-       mod = PyLdbModule_AsModule(self);
+       mod = pyldb_Module_AsModule(self);
        ret = mod->ops->add(mod, req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
@@ -2015,9 +2042,9 @@ static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
 
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_MODIFY;
-       req->op.mod.message = PyLdbMessage_AsMessage(py_message);
+       req->op.mod.message = pyldb_Message_AsMessage(py_message);
 
-       mod = PyLdbModule_AsModule(self);
+       mod = pyldb_Module_AsModule(self);
        ret = mod->ops->modify(mod, req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
@@ -2036,9 +2063,9 @@ static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
 
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_DELETE;
-       req->op.del.dn = PyLdbDn_AsDn(py_dn);
+       req->op.del.dn = pyldb_Dn_AsDn(py_dn);
 
-       ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
+       ret = pyldb_Module_AsModule(self)->ops->del(pyldb_Module_AsModule(self), req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
@@ -2057,10 +2084,10 @@ static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
        req = talloc_zero(NULL, struct ldb_request);
 
        req->operation = LDB_RENAME;
-       req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
-       req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
+       req->op.rename.olddn = pyldb_Dn_AsDn(py_dn1);
+       req->op.rename.newdn = pyldb_Dn_AsDn(py_dn2);
 
-       ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
+       ret = pyldb_Module_AsModule(self)->ops->rename(pyldb_Module_AsModule(self), req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
@@ -2093,6 +2120,7 @@ static PyTypeObject PyLdbModule = {
        .tp_basicsize = sizeof(PyLdbModuleObject),
        .tp_dealloc = (destructor)py_ldb_module_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_doc = "LDB module (extension)",
 };
 
 
@@ -2118,12 +2146,12 @@ static struct ldb_message_element *PyObject_AsMessageElement(
 {
        struct ldb_message_element *me;
 
-       if (PyLdbMessageElement_Check(set_obj)) {
+       if (pyldb_MessageElement_Check(set_obj)) {
                PyLdbMessageElementObject *set_obj_as_me = (PyLdbMessageElementObject *)set_obj;
                /* We have to talloc_reference() the memory context, not the pointer
                 * which may not actually be it's own context */
                if (talloc_reference(mem_ctx, set_obj_as_me->mem_ctx)) {
-                       return PyLdbMessageElement_AsMessageElement(set_obj);
+                       return pyldb_MessageElement_AsMessageElement(set_obj);
                }
                return NULL;
        }
@@ -2190,15 +2218,15 @@ static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObjec
        unsigned int i;
        if (!PyArg_ParseTuple(args, "I", &i))
                return NULL;
-       if (i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
+       if (i >= pyldb_MessageElement_AsMessageElement(self)->num_values)
                Py_RETURN_NONE;
 
-       return PyObject_FromLdbValue(&(PyLdbMessageElement_AsMessageElement(self)->values[i]));
+       return PyObject_FromLdbValue(&(pyldb_MessageElement_AsMessageElement(self)->values[i]));
 }
 
 static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
 {
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
        return PyInt_FromLong(el->flags);
 }
 
@@ -2209,7 +2237,7 @@ static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, P
        if (!PyArg_ParseTuple(args, "I", &flags))
                return NULL;
 
-       el = PyLdbMessageElement_AsMessageElement(self);
+       el = pyldb_MessageElement_AsMessageElement(self);
        el->flags = flags;
        Py_RETURN_NONE;
 }
@@ -2223,12 +2251,12 @@ static PyMethodDef py_ldb_msg_element_methods[] = {
 
 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
 {
-       return PyLdbMessageElement_AsMessageElement(self)->num_values;
+       return pyldb_MessageElement_AsMessageElement(self)->num_values;
 }
 
 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
 {
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
        if (idx < 0 || idx >= el->num_values) {
                PyErr_SetString(PyExc_IndexError, "Out of range");
                return NULL;
@@ -2243,14 +2271,16 @@ static PySequenceMethods py_ldb_msg_element_seq = {
 
 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
 {
-       int ret = ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
-                                                                         PyLdbMessageElement_AsMessageElement(other));
+       int ret = ldb_msg_element_compare(pyldb_MessageElement_AsMessageElement(self),
+                                                                         pyldb_MessageElement_AsMessageElement(other));
        return SIGN(ret);
 }
 
 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
 {
-       return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
+       PyObject *el = ldb_msg_element_to_set(NULL,
+                                             pyldb_MessageElement_AsMessageElement(self));
+       return PyObject_GetIter(el);
 }
 
 static PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
@@ -2361,7 +2391,7 @@ static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
 {
        char *element_str = NULL;
        Py_ssize_t i;
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
        PyObject *ret;
 
        for (i = 0; i < el->num_values; i++) {
@@ -2384,7 +2414,7 @@ static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
 
 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
 {
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
 
        if (el->num_values == 1)
                return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
@@ -2410,6 +2440,7 @@ static PyTypeObject PyLdbMessageElement = {
        .tp_as_sequence = &py_ldb_msg_element_seq,
        .tp_new = py_ldb_msg_element_new,
        .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_doc = "An element of a Message",
 };
 
 
@@ -2437,7 +2468,7 @@ static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args)
                return NULL;
        }
 
-       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
 
        msg = PyDict_AsMessage(ldb_ctx, py_dict, ldb_ctx, mod_flags);
        if (!msg) {
@@ -2464,7 +2495,7 @@ static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args
 
 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i, j = 0;
        PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
        if (msg->dn != NULL) {
@@ -2482,14 +2513,14 @@ static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *p
 {
        struct ldb_message_element *el;
        char *name;
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        if (!PyString_Check(py_name)) {
                PyErr_SetNone(PyExc_TypeError);
                return NULL;
        }
        name = PyString_AsString(py_name);
-       if (!strcmp(name, "dn"))
-               return PyLdbDn_FromDn(msg->dn);
+       if (!ldb_attr_cmp(name, "dn"))
+               return pyldb_Dn_FromDn(msg->dn);
        el = ldb_msg_find_element(msg, name);
        if (el == NULL) {
                return NULL;
@@ -2507,33 +2538,47 @@ static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
        return ret;
 }
 
-static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
+static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args, PyObject *kwargs)
 {
-       PyObject *name, *ret, *retobj;
-       retobj = NULL;
-       if (!PyArg_ParseTuple(args, "O|O", &name, &retobj))
+       PyObject *def = NULL;
+       const char *kwnames[] = { "name", "default", "idx", NULL };
+       const char *name = NULL;
+       int idx = -1;
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
+       struct ldb_message_element *el;
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oi:msg",
+                                        discard_const_p(char *, kwnames), &name, &def, &idx)) {
                return NULL;
+       }
 
-       ret = py_ldb_msg_getitem_helper(self, name);
-       if (ret == NULL) {
-               if (PyErr_Occurred())
-                       return NULL;
-               if (retobj != NULL) {
-                       return retobj;
-               } else {
-                       Py_RETURN_NONE;
+       if (strcasecmp(name, "dn") == 0) {
+               return pyldb_Dn_FromDn(msg->dn);
+       }
+
+       el = ldb_msg_find_element(msg, name);
+
+       if (el == NULL || (idx != -1 && el->num_values <= idx)) {
+               if (def != NULL) {
+                       return def;
                }
+               Py_RETURN_NONE;
        }
-       return ret;
+
+       if (idx == -1) {
+               return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg->elements);
+       }
+
+       return PyObject_FromLdbValue(&el->values[idx]);
 }
 
 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i, j = 0;
        PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
        if (msg->dn != NULL) {
-               PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
+               PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", pyldb_Dn_FromDn(msg->dn)));
                j++;
        }
        for (i = 0; i < msg->num_elements; i++, j++) {
@@ -2546,7 +2591,7 @@ static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
 
 static PyObject *py_ldb_msg_elements(PyLdbMessageObject *self)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i = 0;
        PyObject *l = PyList_New(msg->num_elements);
        for (i = 0; i < msg->num_elements; i++) {
@@ -2557,7 +2602,7 @@ static PyObject *py_ldb_msg_elements(PyLdbMessageObject *self)
 
 static PyObject *py_ldb_msg_add(PyLdbMessageObject *self, PyObject *args)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        PyLdbMessageElementObject *py_element;
        int ret;
        struct ldb_message_element *el;
@@ -2588,7 +2633,12 @@ static PyMethodDef py_ldb_msg_methods[] = {
        { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, 
                "S.remove(name)\n\n"
                "Remove all entries for attributes with the specified name."},
-       { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
+       { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS | METH_KEYWORDS,
+         "msg.get(name,default=None,idx=None) -> string\n"
+         "idx is the index into the values array\n"
+         "if idx is None, then a list is returned\n"
+         "if idx is not None, then the element with that index is returned\n"
+         "if you pass the special name 'dn' then the DN object is returned\n"},
        { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
        { "elements", (PyCFunction)py_ldb_msg_elements, METH_NOARGS, NULL },
        { "add", (PyCFunction)py_ldb_msg_add, METH_VARARGS,
@@ -2621,19 +2671,24 @@ static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject
                /* delitem */
                ldb_msg_remove_attr(self->msg, attr_name);
        } else {
+               int ret;
                struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
                                                                           value, 0, attr_name);
                if (el == NULL)
                        return -1;
-               ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
-               ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
+               ldb_msg_remove_attr(pyldb_Message_AsMessage(self), attr_name);
+               ret = ldb_msg_add(pyldb_Message_AsMessage(self), el, el->flags);
+               if (ret != LDB_SUCCESS) {
+                       PyErr_SetLdbError(PyExc_LdbError, ret, NULL);
+                       return -1;
+               }
        }
        return 0;
 }
 
 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
 {
-       return PyLdbMessage_AsMessage(self)->num_elements;
+       return pyldb_Message_AsMessage(self)->num_elements;
 }
 
 static PyMappingMethods py_ldb_msg_mapping = {
@@ -2670,7 +2725,7 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
 
        if (pydn != NULL) {
                struct ldb_dn *dn;
-               if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
+               if (!pyldb_Object_AsDn(NULL, pydn, NULL, &dn)) {
                        talloc_free(mem_ctx);
                        return NULL;
                }
@@ -2705,19 +2760,19 @@ static PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
 
 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
-       return PyLdbDn_FromDn(msg->dn);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
+       return pyldb_Dn_FromDn(msg->dn);
 }
 
 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
-       if (!PyLdbDn_Check(value)) {
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
+       if (!pyldb_Dn_Check(value)) {
                PyErr_SetNone(PyExc_TypeError);
                return -1;
        }
 
-       msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
+       msg->dn = talloc_reference(msg, pyldb_Dn_AsDn(value));
        return 0;
 }
 
@@ -2745,8 +2800,8 @@ static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
 static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
                              PyLdbMessageObject *py_msg2)
 {
-       struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
-                          *msg2 = PyLdbMessage_AsMessage(py_msg2);
+       struct ldb_message *msg1 = pyldb_Message_AsMessage(py_msg1),
+                          *msg2 = pyldb_Message_AsMessage(py_msg2);
        unsigned int i;
        int ret;
 
@@ -2791,6 +2846,7 @@ static PyTypeObject PyLdbMessage = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
        .tp_iter = (getiterfunc)py_ldb_msg_iter,
        .tp_compare = (cmpfunc)py_ldb_msg_compare,
+       .tp_doc = "A LDB Message",
 };
 
 static PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
@@ -2819,6 +2875,7 @@ static PyTypeObject PyLdbTree = {
        .tp_basicsize = sizeof(PyLdbTreeObject),
        .tp_dealloc = (destructor)py_ldb_tree_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_doc = "A search tree",
 };
 
 /* Ldb_module */
@@ -2827,7 +2884,7 @@ static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
        PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_base, *py_attrs, *py_tree;
 
-       py_base = PyLdbDn_FromDn(req->op.search.base);
+       py_base = pyldb_Dn_FromDn(req->op.search.base);
 
        if (py_base == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
@@ -2926,7 +2983,7 @@ static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
        PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_dn;
 
-       py_dn = PyLdbDn_FromDn(req->op.del.dn);
+       py_dn = pyldb_Dn_FromDn(req->op.del.dn);
 
        if (py_dn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
@@ -2949,12 +3006,12 @@ static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
        PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_olddn, *py_newdn;
 
-       py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
+       py_olddn = pyldb_Dn_FromDn(req->op.rename.olddn);
 
        if (py_olddn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
 
-       py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
+       py_newdn = pyldb_Dn_FromDn(req->op.rename.newdn);
 
        if (py_newdn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
@@ -3153,21 +3210,75 @@ static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
        return PyBool_FromLong(ldb_valid_attr_name(name));
 }
 
+/*
+  encode a string using RFC2254 rules
+ */
+static PyObject *py_binary_encode(PyObject *self, PyObject *args)
+{
+       char *str, *encoded;
+       int size = 0;
+       struct ldb_val val;
+       PyObject *ret;
+
+       if (!PyArg_ParseTuple(args, "s#", &str, &size))
+               return NULL;
+       val.data = (uint8_t *)str;
+       val.length = size;
+
+       encoded = ldb_binary_encode(NULL, val);
+       if (encoded == NULL) {
+               PyErr_SetString(PyExc_TypeError, "unable to encode binary string");
+               return NULL;
+       }
+       ret = PyString_FromString(encoded);
+       talloc_free(encoded);
+       return ret;
+}
+
+/*
+  decode a string using RFC2254 rules
+ */
+static PyObject *py_binary_decode(PyObject *self, PyObject *args)
+{
+       char *str;
+       struct ldb_val val;
+       PyObject *ret;
+
+       if (!PyArg_ParseTuple(args, "s", &str))
+               return NULL;
+
+       val = ldb_binary_decode(NULL, str);
+       if (val.data == NULL) {
+               PyErr_SetString(PyExc_TypeError, "unable to decode binary string");
+               return NULL;
+       }
+       ret = Py_BuildValue("s#", val.data, val.length);
+       talloc_free(val.data);
+       return ret;
+}
+
 static PyMethodDef py_ldb_global_methods[] = {
        { "register_module", py_register_module, METH_VARARGS, 
-               "S.register_module(module) -> None\n"
+               "S.register_module(module) -> None\n\n"
                "Register a LDB module."},
        { "timestring", py_timestring, METH_VARARGS, 
-               "S.timestring(int) -> string\n"
+               "S.timestring(int) -> string\n\n"
                "Generate a LDAP time string from a UNIX timestamp" },
        { "string_to_time", py_string_to_time, METH_VARARGS,
-               "S.string_to_time(string) -> int\n"
+               "S.string_to_time(string) -> int\n\n"
                "Parse a LDAP time string into a UNIX timestamp." },
        { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
-               "S.valid_attr_name(name) -> bool\n"
+               "S.valid_attr_name(name) -> bool\n\nn"
                "Check whether the supplied name is a valid attribute name." },
        { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
-               NULL },
+               "S.open() -> Ldb\n\n"
+               "Open a new LDB context." },
+       { "binary_encode", py_binary_encode, METH_VARARGS,
+               "S.binary_encode(string) -> string\n\n"
+               "Perform a RFC2254 binary encoding on a string" },
+       { "binary_decode", py_binary_decode, METH_VARARGS,
+               "S.binary_decode(string) -> string\n\n"
+               "Perform a RFC2254 binary decode on a string" },
        { NULL }
 };
 
@@ -3290,13 +3401,14 @@ void initldb(void)
 
        PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
 
-#define ADD_LDB_STRING(val)  PyModule_AddObject(m, #val, PyString_FromString(val))
+#define ADD_LDB_STRING(val)  PyModule_AddObject(m, #val, PyString_FromString(LDB_## val))
 
-       ADD_LDB_STRING(LDB_SYNTAX_DN);
-       ADD_LDB_STRING(LDB_SYNTAX_DN);
-       ADD_LDB_STRING(LDB_SYNTAX_DIRECTORY_STRING);
-       ADD_LDB_STRING(LDB_SYNTAX_INTEGER);
-       ADD_LDB_STRING(LDB_SYNTAX_BOOLEAN);
-       ADD_LDB_STRING(LDB_SYNTAX_OCTET_STRING);
-       ADD_LDB_STRING(LDB_SYNTAX_UTC_TIME);
+       ADD_LDB_STRING(SYNTAX_DN);
+       ADD_LDB_STRING(SYNTAX_DIRECTORY_STRING);
+       ADD_LDB_STRING(SYNTAX_INTEGER);
+       ADD_LDB_STRING(SYNTAX_BOOLEAN);
+       ADD_LDB_STRING(SYNTAX_OCTET_STRING);
+       ADD_LDB_STRING(SYNTAX_UTC_TIME);
+       ADD_LDB_STRING(OID_COMPARATOR_AND);
+       ADD_LDB_STRING(OID_COMPARATOR_OR);
 }