ldb:pyldb - optimise includes
[metze/samba/wip.git] / source4 / lib / ldb / pyldb.c
index bcfbedd962af25bdb4574d300e3311515f7ca3aa..44a006ffb305c0aa37cfa6cf22ebc5585b1c1c59 100644 (file)
@@ -26,9 +26,6 @@
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <Python.h>
-#include "replace.h"
-#include "ldb_private.h"
 #include "pyldb.h"
 
 /* There's no Py_ssize_t in 2.4, apparently */
@@ -48,24 +45,24 @@ static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_
                return; /* Python exception should already be set, just keep that */
 
        PyErr_SetObject(error, 
-                                       Py_BuildValue(discard_const_p(char, "(i,s)"), ret, 
-                                 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
+                       Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
+                                     ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
 }
 
 static PyObject *PyExc_LdbError;
 
-PyAPI_DATA(PyTypeObject) PyLdbMessage;
-PyAPI_DATA(PyTypeObject) PyLdbModule;
-PyAPI_DATA(PyTypeObject) PyLdbDn;
-PyAPI_DATA(PyTypeObject) PyLdb;
-PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
-PyAPI_DATA(PyTypeObject) PyLdbTree;
+extern PyTypeObject PyLdbMessage;
+extern PyTypeObject PyLdbModule;
+extern PyTypeObject PyLdbDn;
+extern PyTypeObject PyLdb;
+extern PyTypeObject PyLdbMessageElement;
+extern PyTypeObject PyLdbTree;
 
 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
 
 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx, 
-                                                          struct ldb_message_element *el, 
-                                                          struct ldb_val *val)
+                                      struct ldb_message_element *el,
+                                      struct ldb_val *val)
 {
        struct ldb_val new_val;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
@@ -89,14 +86,13 @@ static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
 {
        PyObject *ret;
-       int i;
+       Py_ssize_t i;
        if (result == NULL) {
                Py_RETURN_NONE;
        } 
        ret = PyList_New(result->count);
        for (i = 0; i < result->count; i++) {
-               PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
-               );
+               PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i]));
        }
        return ret;
 }
@@ -110,10 +106,10 @@ static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
  * @return a ldb_result, or NULL if the conversion failed
  */
 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, 
-                                                                                          PyObject *obj)
+                                              PyObject *obj)
 {
        struct ldb_result *res;
-       int i;
+       Py_ssize_t i;
 
        if (obj == Py_None)
                return NULL;
@@ -357,24 +353,6 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
        return (PyObject *)py_ret;
 }
 
-PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
-{
-       PyLdbDnObject *py_ret;
-
-       if (dn == NULL) {
-               Py_RETURN_NONE;
-       }
-
-       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 = talloc_reference(py_ret->mem_ctx, dn);
-       return (PyObject *)py_ret;
-}
-
 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
 {
        talloc_free(self->mem_ctx);
@@ -508,10 +486,10 @@ static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
 }
 
 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
-                                                                               const char *paramname)
+                                       const char *paramname)
 {
        const char **ret;
-       int i;
+       Py_ssize_t i;
        if (!PyList_Check(list)) {
                PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
                return NULL;
@@ -524,7 +502,7 @@ static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
                        return NULL;
                }
                ret[i] = talloc_strndup(ret, PyString_AsString(item),
-                                                          PyString_Size(item));
+                                       PyString_Size(item));
        }
        ret[i] = NULL;
        return ret;
@@ -700,21 +678,75 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
 }
 
 
+/**
+ * Obtain a ldb message from a Python Dictionary object.
+ *
+ * @param mem_ctx Memory context
+ * @param py_obj Python Dictionary object
+ * @param ldb_ctx LDB context
+ * @param mod_flags Flags to be set on every message element
+ * @return ldb_message on success or NULL on failure
+ */
+static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
+                                           PyObject *py_obj,
+                                           struct ldb_context *ldb_ctx,
+                                           unsigned int mod_flags)
+{
+       struct ldb_message *msg;
+       unsigned int msg_pos = 0;
+       Py_ssize_t dict_pos = 0;
+       PyObject *key, *value;
+       struct ldb_message_element *msg_el;
+       PyObject *dn_value = PyDict_GetItemString(py_obj, "dn");
+
+       msg = ldb_msg_new(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)) {
+                       PyErr_SetString(PyExc_TypeError, "unable to import dn object");
+                       return NULL;
+               }
+               if (msg->dn == NULL) {
+                       PyErr_SetString(PyExc_TypeError, "dn set but not found");
+                       return NULL;
+               }
+       } else {
+               PyErr_SetString(PyExc_TypeError, "no dn set");
+               return NULL;
+       }
+
+       while (PyDict_Next(py_obj, &dict_pos, &key, &value)) {
+               char *key_str = PyString_AsString(key);
+               if (strcmp(key_str, "dn") != 0) {
+                       msg_el = PyObject_AsMessageElement(msg->elements, value,
+                                                          mod_flags, key_str);
+                       if (msg_el == NULL) {
+                               PyErr_SetString(PyExc_TypeError, "unable to import element");
+                               return NULL;
+                       }
+                       memcpy(&msg->elements[msg_pos], msg_el, sizeof(*msg_el));
+                       msg_pos++;
+               }
+       }
+
+       msg->num_elements = msg_pos;
+
+       return msg;
+}
+
 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 {
-       PyObject *py_msg;
+       PyObject *py_obj;
        int ret;
-       Py_ssize_t dict_pos, msg_pos;
-       struct ldb_message_element *msgel;
-       struct ldb_message *msg;
        struct ldb_context *ldb_ctx;
        struct ldb_request *req;
-       PyObject *key, *value;
+       struct ldb_message *msg = NULL;
        PyObject *py_controls = Py_None;
        TALLOC_CTX *mem_ctx;
        struct ldb_control **parsed_controls;
 
-       if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
+       if (!PyArg_ParseTuple(args, "O|O", &py_obj, &py_controls ))
                return NULL;
 
        mem_ctx = talloc_new(NULL);
@@ -731,49 +763,22 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
                parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
                talloc_free(controls);
        }
-       if (PyDict_Check(py_msg)) {
-               PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
-               msg = ldb_msg_new(mem_ctx);
-               msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
-               msg_pos = dict_pos = 0;
-               if (dn_value) {
-                       if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
-                               PyErr_SetString(PyExc_TypeError, "unable to import dn object");
-                               talloc_free(mem_ctx);
-                               return NULL;
-                       }
-                       if (msg->dn == NULL) {
-                               PyErr_SetString(PyExc_TypeError, "dn set but not found");
-                               talloc_free(mem_ctx);
-                               return NULL;
-                       }
-               }
-
-               while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
-                       char *key_str = PyString_AsString(key);
-                       if (strcmp(key_str, "dn") != 0) {
-                               msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
-                               if (msgel == NULL) {
-                                       PyErr_SetString(PyExc_TypeError, "unable to import element");
-                                       talloc_free(mem_ctx);
-                                       return NULL;
-                               }
-                               memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
-                               msg_pos++;
-                       }
-               }
-
-               if (msg->dn == NULL) {
-                       PyErr_SetString(PyExc_TypeError, "no dn set");
-                       talloc_free(mem_ctx);
-                       return NULL;
-               }
 
-               msg->num_elements = msg_pos;
+       if (PyLdbMessage_Check(py_obj)) {
+               msg = PyLdbMessage_AsMessage(py_obj);
+       } else if (PyDict_Check(py_obj)) {
+               msg = PyDict_AsMessage(mem_ctx, py_obj, ldb_ctx, LDB_FLAG_MOD_ADD);
        } else {
-               msg = PyLdbMessage_AsMessage(py_msg);
+               PyErr_SetString(PyExc_TypeError,
+                               "Dictionary or LdbMessage object expected!");
+       }
+
+       if (!msg) {
+               /* we should have a PyErr already set */
+               talloc_free(mem_ctx);
+               return NULL;
        }
-        
+
        ret = ldb_msg_sanity_check(ldb_ctx, msg);
         if (ret != LDB_SUCCESS) {
                PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
@@ -898,18 +903,33 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
        int ret;
        struct ldb_context *ldb;
        TALLOC_CTX *mem_ctx;
+       PyObject *py_controls = Py_None;
+       struct ldb_control **parsed_controls;
+       struct ldb_context *ldb_ctx;
+       struct ldb_request *req;
 
-       if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
+       ldb_ctx = PyLdb_AsLdbContext(self);
+
+       if (!PyArg_ParseTuple(args, "OO|O", &py_dn1, &py_dn2, &py_controls))
                return NULL;
 
+
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
                PyErr_NoMemory();
                return NULL;
        }
-
        ldb = PyLdb_AsLdbContext(self);
 
+       if (py_controls == Py_None) {
+               parsed_controls = NULL;
+       } else {
+               const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
+               talloc_free(controls);
+       }
+
+
        if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
                talloc_free(mem_ctx);
                return NULL;
@@ -920,9 +940,40 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       ret = ldb_rename(ldb, dn1, dn2);
+       ret = ldb_build_rename_req(&req, ldb_ctx, mem_ctx, dn1, dn2, parsed_controls,
+                               NULL, ldb_op_default_callback, NULL);
+       if (ret != LDB_SUCCESS) {
+               PyErr_SetString(PyExc_TypeError, "failed to build request");
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+       /* do request and autostart a transaction */
+       /* Then let's LDB handle the message error in case of pb as they are meaningful */
+
+       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);
+       }
+
+       ret = ldb_request(ldb_ctx, req);
+       if (ret == LDB_SUCCESS) {
+               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);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -966,7 +1017,7 @@ static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
 }
 
 
-static PyObject *py_ldb_write_ldif(PyLdbMessageObject *self, PyObject *args)
+static PyObject *py_ldb_write_ldif(PyLdbObject *self, PyObject *args)
 {
        int changetype;
        PyObject *py_msg;
@@ -1035,9 +1086,11 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
 
 static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
 {
+       int ldb_ret;
        PyObject *py_msg_old;
        PyObject *py_msg_new;
        struct ldb_message *diff;
+       struct ldb_context *ldb;
        PyObject *py_ret;
 
        if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new))
@@ -1053,14 +1106,20 @@ static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       diff = ldb_msg_diff(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg_old), PyLdbMessage_AsMessage(py_msg_new));
-       if (!diff) {
+       ldb = PyLdb_AsLdbContext(self);
+       ldb_ret = ldb_msg_difference(ldb, ldb,
+                                    PyLdbMessage_AsMessage(py_msg_old),
+                                    PyLdbMessage_AsMessage(py_msg_new),
+                                    &diff);
+       if (ldb_ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
                return NULL;
        }
 
        py_ret = PyLdbMessage_FromMessage(diff);
 
+       talloc_unlink(ldb, diff);
+
        return py_ret;
 }
 
@@ -1176,14 +1235,14 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
                                   ldb_search_default_callback,
                                   NULL);
 
-       talloc_steal(req, attrs);
-
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
                PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
+       talloc_steal(req, attrs);
+
        ret = ldb_request(ldb_ctx, req);
 
        if (ret == LDB_SUCCESS) {
@@ -1255,7 +1314,6 @@ static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
        int type, ret;
        uint64_t value;
 
-       /* type "int" rather than "enum" for "scope" is intentional */
        if (!PyArg_ParseTuple(args, "i", &type))
                return NULL;
 
@@ -1390,13 +1448,15 @@ static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
        struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
        struct ldb_dn *dn;
        struct ldb_result *result;
+       unsigned int count;
        int ret;
-       int count;
 
-       if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
+       if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
                return -1;
+       }
 
-       ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
+       ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL,
+                        NULL);
        if (ret != LDB_SUCCESS) {
                PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return -1;
@@ -1406,6 +1466,14 @@ static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
 
        talloc_free(result);
 
+       if (count > 1) {
+               PyErr_Format(PyExc_RuntimeError,
+                            "Searching for [%s] dn gave %u results!",
+                            ldb_dn_get_linearized(dn),
+                            count);
+               return -1;
+       }
+
        return count;
 }
 
@@ -1653,8 +1721,9 @@ PyTypeObject PyLdbModule = {
  * @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)
+                                                     PyObject *set_obj,
+                                                     int flags,
+                                                     const char *attr_name)
 {
        struct ldb_message_element *me;
 
@@ -1678,11 +1747,17 @@ struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
                me->values[0].data = talloc_memdup(me, 
                        (uint8_t *)PyString_AsString(set_obj), me->values[0].length+1);
        } else if (PySequence_Check(set_obj)) {
-               int i;
+               Py_ssize_t i;
                me->num_values = PySequence_Size(set_obj);
                me->values = talloc_array(me, struct ldb_val, me->num_values);
                for (i = 0; i < me->num_values; i++) {
                        PyObject *obj = PySequence_GetItem(set_obj, i);
+                       if (!PyString_Check(obj)) {
+                               PyErr_Format(PyExc_TypeError,
+                                            "Expected string as element %zd in list", i);
+                               talloc_free(me);
+                               return NULL;
+                       }
 
                        me->values[i].length = PyString_Size(obj);
                        me->values[i].data = talloc_memdup(me, 
@@ -1697,10 +1772,10 @@ struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
 }
 
 
-static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, 
-                                                                struct ldb_message_element *me)
+static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
+                                       struct ldb_message_element *me)
 {
-       int i;
+       Py_ssize_t i;
        PyObject *result;
 
        /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
@@ -1716,10 +1791,10 @@ static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
 
 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
 {
-       int i;
-       if (!PyArg_ParseTuple(args, "i", &i))
+       unsigned int i;
+       if (!PyArg_ParseTuple(args, "I", &i))
                return NULL;
-       if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
+       if (i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
                Py_RETURN_NONE;
 
        return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self), 
@@ -1825,7 +1900,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb
        el = talloc_zero(mem_ctx, struct ldb_message_element);
 
        if (py_elements != NULL) {
-               int i;
+               Py_ssize_t i;
                if (PyString_Check(py_elements)) {
                        el->num_values = 1;
                        el->values = talloc_array(el, struct ldb_val, 1);
@@ -1839,8 +1914,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb
                                PyObject *item = PySequence_GetItem(py_elements, i);
                                if (!PyString_Check(item)) {
                                        PyErr_Format(PyExc_TypeError, 
-                                                       "Expected string as element %d in list", 
-                                                       i);
+                                                    "Expected string as element %zd in list", i);
                                        talloc_free(mem_ctx);
                                        return NULL;
                                }
@@ -1874,7 +1948,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb
 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
 {
        char *element_str = NULL;
-       int i;
+       Py_ssize_t i;
        struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
        PyObject *ret;
 
@@ -1926,6 +2000,45 @@ PyTypeObject PyLdbMessageElement = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
+
+static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args)
+{
+       PyObject *py_ldb;
+       PyObject *py_dict;
+       PyObject *py_ret;
+       struct ldb_message *msg;
+       struct ldb_context *ldb_ctx;
+       unsigned int mod_flags = LDB_FLAG_MOD_REPLACE;
+
+       if (!PyArg_ParseTuple(args, "O!O!|I",
+                             &PyLdb, &py_ldb, &PyDict_Type, &py_dict,
+                             &mod_flags)) {
+               return NULL;
+       }
+
+       /* mask only flags we are going to use */
+       mod_flags = LDB_FLAG_MOD_TYPE(mod_flags);
+       if (!mod_flags) {
+               PyErr_SetString(PyExc_ValueError,
+                               "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
+                               " expected as mod_flag value");
+               return NULL;
+       }
+
+       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+
+       msg = PyDict_AsMessage(ldb_ctx, py_dict, ldb_ctx, mod_flags);
+       if (!msg) {
+               return NULL;
+       }
+
+       py_ret = PyLdbMessage_FromMessage(msg);
+
+       talloc_unlink(ldb_ctx, msg);
+
+       return py_ret;
+}
+
 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
 {
        char *name;
@@ -1940,7 +2053,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);
-       int i, j = 0;
+       Py_ssize_t i, j = 0;
        PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
        if (msg->dn != NULL) {
                PyList_SetItem(obj, j, PyString_FromString("dn"));
@@ -2000,9 +2113,8 @@ static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
 {
        struct ldb_message *msg = PyLdbMessage_AsMessage(self);
-       int i, j;
+       Py_ssize_t i, j = 0;
        PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
-       j = 0;
        if (msg->dn != NULL) {
                PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
                j++;
@@ -2014,6 +2126,11 @@ static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
 }
 
 static PyMethodDef py_ldb_msg_methods[] = { 
+       { "from_dict", (PyCFunction)py_ldb_msg_from_dict, METH_CLASS | METH_VARARGS,
+               "Message.from_dict(ldb, dict, mod_flag) -> ldb.Message\n"
+               "Class method to create ldb.Message object from Dictionary.\n"
+               "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE.\n"
+               "mod_flag defaults to FLAG_MOD_REPLACE"},
        { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
        { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
        { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
@@ -2046,7 +2163,7 @@ static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject
                ldb_msg_remove_attr(self->msg, attr_name);
        } else {
                struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
-                                                                                       value, 0, attr_name);
+                                                                          value, 0, attr_name);
                if (el == NULL)
                        return -1;
                ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
@@ -2547,14 +2664,14 @@ static PyObject *py_register_module(PyObject *module, PyObject *args)
 
 static PyObject *py_timestring(PyObject *module, PyObject *args)
 {
-       time_t t;
-       unsigned long val;
+       /* most times "time_t" is a signed integer type with 32 or 64 bit:
+        * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
+       long int t_val;
        char *tresult;
        PyObject *ret;
-       if (!PyArg_ParseTuple(args, "l", &val))
+       if (!PyArg_ParseTuple(args, "l", &t_val))
                return NULL;
-       t = (time_t)val;
-       tresult = ldb_timestring(NULL, t);
+       tresult = ldb_timestring(NULL, (time_t) t_val);
        ret = PyString_FromString(tresult);
        talloc_free(tresult);
        return ret;
@@ -2702,4 +2819,6 @@ void initldb(void)
        PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
        PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
        PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
+
+       PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
 }