ldb:python bindings - some small cleanup & improvements in "py_ldb_add"
authorMatthias Dieter Wallnöfer <mdw@samba.org>
Fri, 18 Jun 2010 20:08:58 +0000 (22:08 +0200)
committerMatthias Dieter Wallnöfer <mdw@samba.org>
Sat, 19 Jun 2010 15:53:21 +0000 (17:53 +0200)
Also to make it similar to "py_ldb_delete".

source4/lib/ldb/pyldb.c

index 7d999fa2e29dce847aecbd7cd126a70fc5494d43..749ddd025aafaa59cdcfcaeabb958357858ce6b2 100644 (file)
@@ -710,14 +710,19 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
                return NULL;
-       ldb_ctx = PyLdb_AsLdbContext(self);
 
        mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       ldb_ctx = PyLdb_AsLdbContext(self);
+
        if (py_controls == Py_None) {
                parsed_controls = NULL;
        } else {
-               const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
-               parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
+               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 (PyDict_Check(py_msg)) {
@@ -770,13 +775,8 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
                return NULL;
         }
 
-        ret = ldb_build_add_req(&req, ldb_ctx, ldb_ctx,
-                                        msg,
-                                        parsed_controls,
-                                        NULL,
-                                        ldb_op_default_callback,
-                                        NULL);
-
+        ret = ldb_build_add_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
+                               NULL, ldb_op_default_callback, NULL);
         if (ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_TypeError, "failed to build request");
                talloc_free(mem_ctx);
@@ -788,9 +788,8 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 
         ret = ldb_transaction_start(ldb_ctx);
         if (ret != LDB_SUCCESS) {
-               talloc_free(req);
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
         }
 
         ret = ldb_request(ldb_ctx, req);
@@ -807,9 +806,9 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
                        ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
                }
        }
-       talloc_free(req);
+
        talloc_free(mem_ctx);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }