pyldb: use dn.is_child_of() instead of dn.compare_base()
authorAndrew Tridgell <tridge@samba.org>
Thu, 14 Jul 2011 03:17:49 +0000 (13:17 +1000)
committerAndrew Tridgell <tridge@samba.org>
Thu, 21 Jul 2011 01:44:36 +0000 (11:44 +1000)
the compare_base() C API doesn't really fit well in python, as it
returns 0 for true. Better to have a boolean function for the python
interface.

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>

lib/ldb/pyldb.c
lib/ldb/tests/python/api.py

index 4c71569f052b611844e170ea5e55e29024ce7ba7..adec424aa1e215f7cfb39d3c3083b8799b8d8ae6 100644 (file)
@@ -522,7 +522,7 @@ static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
        return ldb_dn_add_base(dn, other)?Py_True:Py_False;
 }
 
-static PyObject *py_ldb_dn_compare_base(PyLdbDnObject *self, PyObject *args)
+static PyObject *py_ldb_dn_is_child_of(PyLdbDnObject *self, PyObject *args)
 {
        PyObject *py_base;
        struct ldb_dn *dn, *base;
@@ -534,7 +534,7 @@ static PyObject *py_ldb_dn_compare_base(PyLdbDnObject *self, PyObject *args)
        if (!PyObject_AsDn(NULL, py_base, dn_ldb_ctx(dn), &base))
                return NULL;
 
-       return PyInt_FromLong(ldb_dn_compare_base(base, dn));
+       return PyBool_FromLong(ldb_dn_compare_base(base, dn) == 0);
 }
 
 static PyMethodDef py_ldb_dn_methods[] = {
@@ -555,8 +555,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)." },
-       { "compare_base", (PyCFunction)py_ldb_dn_compare_base, METH_VARARGS,
-               "S.compare_base(basedn) -> int\n\n"},
+       { "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)." },
index bc556d6143bb2f6e93dfce0a1f7acf9e0c7b3831..bd10b0b293f7783179e7cac9b1d8476ae5e87965 100755 (executable)
@@ -437,17 +437,18 @@ class DnTests(unittest.TestCase):
         x = ldb.Dn(self.ldb, "dc=foo26,bar=bloe")
         self.assertEquals("/bloe\nfoo26", x.canonical_ex_str())
 
-    def test_ldb_base_compare(self):
+    def test_ldb_is_child_of(self):
         """Testing ldb_dn_compare_dn"""
         dn1 = ldb.Dn(self.ldb, "dc=base")
         dn2 = ldb.Dn(self.ldb, "cn=foo,dc=base")
         dn3 = ldb.Dn(self.ldb, "cn=bar,dc=base")
         dn4 = ldb.Dn(self.ldb, "cn=baz,cn=bar,dc=base")
 
-        self.assertEquals(0, dn2.compare_base(dn1))
-        self.assertEquals(0, dn4.compare_base(dn1))
-        self.assertEquals(0, dn4.compare_base(dn3))
-        self.assertFalse(dn3.compare_base(dn2) == 0)
+        self.assertTrue(dn2.is_child_of(dn1))
+        self.assertTrue(dn4.is_child_of(dn1))
+        self.assertTrue(dn4.is_child_of(dn3))
+        self.assertFalse(dn3.is_child_of(dn2))
+        self.assertFalse(dn1.is_child_of(dn4))
 
 class LdbMsgTests(unittest.TestCase):