s4:ldb Rework use of talloc and ldif objects in python wrapper
authorAndrew Bartlett <abartlet@samba.org>
Sun, 5 Jul 2009 23:31:38 +0000 (09:31 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Sun, 5 Jul 2009 23:50:47 +0000 (09:50 +1000)
The talloc hirarchy here was a bit odd - we would both steal the
parsed ldif onto 'NULL', then reference it onto a python talloc
wrapper.

Now we just leave the reference, after we complete building the object.

Andrew Bartlett

source4/lib/ldb/pyldb.c

index 9bdd71db185e4e68eb5fcc615005a6047e68ad05..2e0f4fdf3606b88576b905d8949aa8417171825e 100644 (file)
@@ -790,7 +790,6 @@ static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
                Py_RETURN_NONE;
        } else {
        /* We don't want this attached to the 'ldb' any more */
-               talloc_steal(NULL, ldif);
                return Py_BuildValue(discard_const_p(char, "(iO)"),
                                     ldif->changetype,
                                     PyLdbMessage_FromMessage(ldif->msg));
@@ -804,13 +803,29 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
        struct ldb_ldif *ldif;
        const char *s;
 
+       TALLOC_CTX *mem_ctx;
+
        if (!PyArg_ParseTuple(args, "s", &s))
                return NULL;
 
+       mem_ctx = talloc_new(NULL);
+       if (!mem_ctx) {
+               Py_RETURN_NONE;
+       }
+
        list = PyList_New(0);
-       while ((ldif = ldb_ldif_read_string(self->ldb_ctx, &s)) != NULL) {
-               PyList_Append(list, ldb_ldif_to_pyobject(ldif));
+       while (s && *s != '\0') {
+               ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
+               talloc_steal(mem_ctx, ldif);
+               if (ldif) {
+                       PyList_Append(list, ldb_ldif_to_pyobject(ldif));
+               } else {
+                       PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
+                       talloc_free(mem_ctx);
+                       return NULL;
+               }
        }
+       talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
        return PyObject_GetIter(list);
 }