From b9119c0f0f524d43ff09825dffb24a5e77a240f4 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 19 May 2008 23:07:04 +0200 Subject: [PATCH 1/4] Add __repr__ implementations for ldb.Message, ldb.MessageElement and ldb.Dn. --- source/lib/ldb/ldb.i | 50 ++++++++++++++++++++++++--- source/lib/ldb/ldb.py | 30 +++++++++++++++-- source/lib/ldb/ldb_wrap.c | 54 +++++++++++++++++++++++++----- source/lib/ldb/tests/python/api.py | 38 ++++++++++++++++++++- 4 files changed, 155 insertions(+), 17 deletions(-) diff --git a/source/lib/ldb/ldb.i b/source/lib/ldb/ldb.i index 6b94f19cb5b..75482011fb0 100644 --- a/source/lib/ldb/ldb.i +++ b/source/lib/ldb/ldb.i @@ -229,6 +229,14 @@ fail: return ldb_dn_canonical_ex_string($self, $self); } #ifdef SWIGPYTHON + char *__repr__(void) + { + char *dn = ldb_dn_get_linearized($self), *ret; + asprintf(&ret, "Dn('%s')", dn); + talloc_free(dn); + return ret; + } + ldb_dn *__add__(ldb_dn *other) { ldb_dn *ret = ldb_dn_copy(NULL, $self); @@ -376,6 +384,9 @@ typedef struct ldb_message_element { raise KeyError("no such value") return ret + def __repr__(self): + return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__())) + def __eq__(self, other): if (len(self) == 1 and self.get(0) == other): return True @@ -400,17 +411,22 @@ typedef struct ldb_message_element { else $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0); } -%rename(__getitem__) ldb_message::find_element; //%typemap(out) ldb_msg_element *; %inline { PyObject *ldb_msg_list_elements(ldb_msg *msg) { - int i; - PyObject *obj = PyList_New(msg->num_elements); - for (i = 0; i < msg->num_elements; i++) - PyList_SetItem(obj, i, PyString_FromString(msg->elements[i].name)); + int 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")); + j++; + } + for (i = 0; i < msg->num_elements; i++) { + PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name)); + j++; + } return obj; } } @@ -466,6 +482,28 @@ typedef struct ldb_message { } #endif void remove_attr(const char *name); +%pythoncode { + def get(self, key, default=None): + if key == "dn": + return self.dn + return self.find_element(key) + + def __getitem__(self, key): + ret = self.get(key, None) + if ret is None: + raise KeyError("No such element") + return ret + + def iteritems(self): + for k in self.keys(): + yield k, self[k] + + def items(self): + return list(self.iteritems()) + + def __repr__(self): + return "Message(%s)" % repr(dict(self.iteritems())) +} } } ldb_msg; @@ -753,6 +791,8 @@ typedef struct ldb_context { def search(self, base=None, scope=SCOPE_DEFAULT, expression=None, attrs=None, controls=None): + if not (attrs is None or isinstance(attrs, list)): + raise TypeError("attributes not a list") parsed_controls = None if controls is not None: parsed_controls = self.parse_control_strings(controls) diff --git a/source/lib/ldb/ldb.py b/source/lib/ldb/ldb.py index b148782c63a..60644d352cd 100644 --- a/source/lib/ldb/ldb.py +++ b/source/lib/ldb/ldb.py @@ -68,7 +68,6 @@ CHANGETYPE_MODIFY = _ldb.CHANGETYPE_MODIFY ldb_val_to_py_object = _ldb.ldb_val_to_py_object class Dn(object): thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag') - __repr__ = _swig_repr def __init__(self, *args, **kwargs): _ldb.Dn_swiginit(self,_ldb.new_Dn(*args, **kwargs)) __swig_destroy__ = _ldb.delete_Dn @@ -93,6 +92,7 @@ Dn.add_child = new_instancemethod(_ldb.Dn_add_child,None,Dn) Dn.add_base = new_instancemethod(_ldb.Dn_add_base,None,Dn) Dn.canonical_str = new_instancemethod(_ldb.Dn_canonical_str,None,Dn) Dn.canonical_ex_str = new_instancemethod(_ldb.Dn_canonical_ex_str,None,Dn) +Dn.__repr__ = new_instancemethod(_ldb.Dn___repr__,None,Dn) Dn.__add__ = new_instancemethod(_ldb.Dn___add__,None,Dn) Dn_swigregister = _ldb.Dn_swigregister Dn_swigregister(Dn) @@ -108,6 +108,9 @@ class ldb_msg_element(object): raise KeyError("no such value") return ret + def __repr__(self): + return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__())) + def __eq__(self, other): if (len(self) == 1 and self.get(0) == other): return True @@ -139,7 +142,28 @@ class Message(object): def __init__(self, *args, **kwargs): _ldb.Message_swiginit(self,_ldb.new_Message(*args, **kwargs)) __swig_destroy__ = _ldb.delete_Message -Message.__getitem__ = new_instancemethod(_ldb.Message___getitem__,None,Message) + def get(self, key, default=None): + if key == "dn": + return self.dn + return self.find_element(key) + + def __getitem__(self, key): + ret = self.get(key, None) + if ret is None: + raise KeyError("No such element") + return ret + + def iteritems(self): + for k in self.keys(): + yield k, self[k] + + def items(self): + return list(self.iteritems()) + + def __repr__(self): + return "Message(%s)" % repr(dict(self.iteritems())) + +Message.find_element = new_instancemethod(_ldb.Message_find_element,None,Message) Message.__setitem__ = new_instancemethod(_ldb.Message___setitem__,None,Message) Message.__len__ = new_instancemethod(_ldb.Message___len__,None,Message) Message.keys = new_instancemethod(_ldb.Message_keys,None,Message) @@ -202,6 +226,8 @@ class Ldb(object): def search(self, base=None, scope=SCOPE_DEFAULT, expression=None, attrs=None, controls=None): + if not (attrs is None or isinstance(attrs, list)): + raise TypeError("attributes not a list") parsed_controls = None if controls is not None: parsed_controls = self.parse_control_strings(controls) diff --git a/source/lib/ldb/ldb_wrap.c b/source/lib/ldb/ldb_wrap.c index 390652eebe6..f13ed4dc3b9 100644 --- a/source/lib/ldb/ldb_wrap.c +++ b/source/lib/ldb/ldb_wrap.c @@ -2719,6 +2719,12 @@ SWIGINTERN char const *ldb_dn_canonical_str(ldb_dn *self){ SWIGINTERN char const *ldb_dn_canonical_ex_str(ldb_dn *self){ return ldb_dn_canonical_ex_string(self, self); } +SWIGINTERN char *ldb_dn___repr__(ldb_dn *self){ + char *dn = ldb_dn_get_linearized(self), *ret; + asprintf(&ret, "Dn('%s')", dn); + talloc_free(dn); + return ret; + } SWIGINTERN ldb_dn *ldb_dn___add__(ldb_dn *self,ldb_dn *other){ ldb_dn *ret = ldb_dn_copy(NULL, self); ldb_dn_add_child(ret, other); @@ -2970,10 +2976,16 @@ SWIGINTERN void delete_ldb_msg_element(ldb_msg_element *self){ talloc_free(self) PyObject *ldb_msg_list_elements(ldb_msg *msg) { - int i; - PyObject *obj = PyList_New(msg->num_elements); - for (i = 0; i < msg->num_elements; i++) - PyList_SetItem(obj, i, PyString_FromString(msg->elements[i].name)); + int 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")); + j++; + } + for (i = 0; i < msg->num_elements; i++) { + PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name)); + j++; + } return obj; } @@ -3678,6 +3690,29 @@ fail: } +SWIGINTERN PyObject *_wrap_Dn___repr__(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + ldb_dn *arg1 = (ldb_dn *) 0 ; + char *result = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject *swig_obj[1] ; + + if (!args) SWIG_fail; + swig_obj[0] = args; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_ldb_dn, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Dn___repr__" "', argument " "1"" of type '" "ldb_dn *""'"); + } + arg1 = (ldb_dn *)(argp1); + result = (char *)ldb_dn___repr__(arg1); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + SWIGINTERN PyObject *_wrap_Dn___add__(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { PyObject *resultobj = 0; ldb_dn *arg1 = (ldb_dn *) 0 ; @@ -4074,7 +4109,7 @@ fail: } -SWIGINTERN PyObject *_wrap_Message___getitem__(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { +SWIGINTERN PyObject *_wrap_Message_find_element(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { PyObject *resultobj = 0; ldb_msg *arg1 = (ldb_msg *) 0 ; char *arg2 = (char *) 0 ; @@ -4090,15 +4125,15 @@ SWIGINTERN PyObject *_wrap_Message___getitem__(PyObject *SWIGUNUSEDPARM(self), P (char *) "self",(char *) "name", NULL }; - if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"OO:Message___getitem__",kwnames,&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"OO:Message_find_element",kwnames,&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_ldb_message, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Message___getitem__" "', argument " "1"" of type '" "ldb_msg *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Message_find_element" "', argument " "1"" of type '" "ldb_msg *""'"); } arg1 = (ldb_msg *)(argp1); res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Message___getitem__" "', argument " "2"" of type '" "char const *""'"); + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Message_find_element" "', argument " "2"" of type '" "char const *""'"); } arg2 = (char *)(buf2); if (arg1 == NULL) @@ -5673,6 +5708,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"Dn_add_base", (PyCFunction) _wrap_Dn_add_base, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"Dn_canonical_str", (PyCFunction)_wrap_Dn_canonical_str, METH_O, NULL}, { (char *)"Dn_canonical_ex_str", (PyCFunction)_wrap_Dn_canonical_ex_str, METH_O, NULL}, + { (char *)"Dn___repr__", (PyCFunction)_wrap_Dn___repr__, METH_O, NULL}, { (char *)"Dn___add__", (PyCFunction) _wrap_Dn___add__, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"Dn_swigregister", Dn_swigregister, METH_VARARGS, NULL}, { (char *)"Dn_swiginit", Dn_swiginit, METH_VARARGS, NULL}, @@ -5689,7 +5725,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"Message_dn_get", (PyCFunction)_wrap_Message_dn_get, METH_O, NULL}, { (char *)"new_Message", (PyCFunction) _wrap_new_Message, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"delete_Message", (PyCFunction)_wrap_delete_Message, METH_O, NULL}, - { (char *)"Message___getitem__", (PyCFunction) _wrap_Message___getitem__, METH_VARARGS | METH_KEYWORDS, NULL}, + { (char *)"Message_find_element", (PyCFunction) _wrap_Message_find_element, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"Message___setitem__", _wrap_Message___setitem__, METH_VARARGS, NULL}, { (char *)"Message___len__", (PyCFunction)_wrap_Message___len__, METH_O, NULL}, { (char *)"Message_keys", (PyCFunction)_wrap_Message_keys, METH_O, NULL}, diff --git a/source/lib/ldb/tests/python/api.py b/source/lib/ldb/tests/python/api.py index 5f3f727b5d1..6f073f79a84 100755 --- a/source/lib/ldb/tests/python/api.py +++ b/source/lib/ldb/tests/python/api.py @@ -60,6 +60,10 @@ class SimpleLdb(unittest.TestCase): l = ldb.Ldb("foo.tdb") self.assertEquals(len(l.search("", ldb.SCOPE_SUBTREE, "(dc=*)", ["dc"])), 0) + def test_search_attr_string(self): + l = ldb.Ldb("foo.tdb") + self.assertRaises(TypeError, l.search, attrs="dc") + def test_opaque(self): l = ldb.Ldb("foo.tdb") l.set_opaque("my_opaque", l) @@ -257,6 +261,10 @@ class DnTests(unittest.TestCase): x = ldb.Dn(self.ldb, "dc=foo,bar=bloe") self.assertEquals(x.__str__(), "dc=foo,bar=bloe") + def test_repr(self): + x = ldb.Dn(self.ldb, "dc=foo,bla=blie") + self.assertEquals(x.__repr__(), "Dn('dc=foo,bla=blie')") + def test_get_casefold(self): x = ldb.Dn(self.ldb, "dc=foo,bar=bloe") self.assertEquals(x.get_casefold(), "DC=FOO,BAR=bloe") @@ -347,6 +355,16 @@ class LdbMsgTests(unittest.TestCase): self.msg = ldb.Message(ldb.Dn(ldb.Ldb(), "dc=foo")) self.assertEquals("dc=foo", str(self.msg.dn)) + def test_iter_items(self): + self.assertEquals(0, len(self.msg.items())) + self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo") + self.assertEquals(1, len(self.msg.items())) + + def test_repr(self): + self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo") + self.msg["dc"] = "foo" + self.assertEquals("Message({'dn': Dn('dc=foo'), 'dc': MessageElement(['foo'])})", repr(self.msg)) + def test_len(self): self.assertEquals(0, len(self.msg)) @@ -374,14 +392,26 @@ class LdbMsgTests(unittest.TestCase): self.assertEquals(["bar"], list(self.msg["foo"])) def test_keys(self): + self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO") self.msg["foo"] = ["bla"] self.msg["bar"] = ["bla"] - self.assertEquals(["foo", "bar"], self.msg.keys()) + self.assertEquals(["dn", "foo", "bar"], self.msg.keys()) def test_dn(self): self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO") self.assertEquals("@BASEINFO", self.msg.dn.__str__()) + def test_get_dn(self): + self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO") + self.assertEquals("@BASEINFO", self.msg.get("dn").__str__()) + + def test_get_other(self): + self.msg["foo"] = ["bar"] + self.assertEquals("bar", self.msg.get("foo")[0]) + + def test_get_unknown(self): + self.assertRaises(KeyError, self.msg.get, "lalalala") + class MessageElementTests(unittest.TestCase): def test_cmp_element(self): @@ -395,6 +425,12 @@ class MessageElementTests(unittest.TestCase): x = ldb.MessageElement(["foo"]) self.assertEquals(["foo"], list(x)) + def test_repr(self): + x = ldb.MessageElement(["foo"]) + self.assertEquals("MessageElement(['foo'])", repr(x)) + x = ldb.MessageElement(["foo", "bla"]) + self.assertEquals("MessageElement(['foo','bla'])", repr(x)) + def test_get_item(self): x = ldb.MessageElement(["foo", "bar"]) self.assertEquals("foo", x[0]) From 5607aea07f66f09fd5b33842d07d2fbbf44d13e7 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 19 May 2008 23:12:13 +0200 Subject: [PATCH 2/4] Add __repr__ implementation for Ldb. --- source/lib/ldb/ldb.i | 6 ++++++ source/lib/ldb/ldb.py | 2 +- source/lib/ldb/ldb_wrap.c | 32 ++++++++++++++++++++++++++++++ source/lib/ldb/tests/python/api.py | 4 ++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/source/lib/ldb/ldb.i b/source/lib/ldb/ldb.i index 75482011fb0..18e981f7bea 100644 --- a/source/lib/ldb/ldb.i +++ b/source/lib/ldb/ldb.i @@ -781,6 +781,12 @@ typedef struct ldb_context { return PyObject_GetIter(list); } + char *__repr__(void) + { + char *ret; + asprintf(&ret, "", ret); + return ret; + } #endif } %pythoncode { diff --git a/source/lib/ldb/ldb.py b/source/lib/ldb/ldb.py index 60644d352cd..e9f4055fbf7 100644 --- a/source/lib/ldb/ldb.py +++ b/source/lib/ldb/ldb.py @@ -215,7 +215,6 @@ LDB_ERR_AFFECTS_MULTIPLE_DSAS = _ldb.LDB_ERR_AFFECTS_MULTIPLE_DSAS LDB_ERR_OTHER = _ldb.LDB_ERR_OTHER class Ldb(object): thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag') - __repr__ = _swig_repr def __init__(self, *args, **kwargs): _ldb.Ldb_swiginit(self,_ldb.new_Ldb(*args, **kwargs)) __swig_destroy__ = _ldb.delete_Ldb @@ -260,6 +259,7 @@ Ldb.schema_attribute_add = new_instancemethod(_ldb.Ldb_schema_attribute_add,None Ldb.setup_wellknown_attributes = new_instancemethod(_ldb.Ldb_setup_wellknown_attributes,None,Ldb) Ldb.__contains__ = new_instancemethod(_ldb.Ldb___contains__,None,Ldb) Ldb.parse_ldif = new_instancemethod(_ldb.Ldb_parse_ldif,None,Ldb) +Ldb.__repr__ = new_instancemethod(_ldb.Ldb___repr__,None,Ldb) Ldb_swigregister = _ldb.Ldb_swigregister Ldb_swigregister(Ldb) diff --git a/source/lib/ldb/ldb_wrap.c b/source/lib/ldb/ldb_wrap.c index f13ed4dc3b9..d787266416a 100644 --- a/source/lib/ldb/ldb_wrap.c +++ b/source/lib/ldb/ldb_wrap.c @@ -3200,6 +3200,11 @@ SWIGINTERN PyObject *ldb_parse_ldif(ldb *self,char const *s){ } return PyObject_GetIter(list); } +SWIGINTERN char *ldb___repr__(ldb *self){ + char *ret; + asprintf(&ret, "", ret); + return ret; + } static char *timestring(time_t t) { @@ -5573,6 +5578,32 @@ fail: } +SWIGINTERN PyObject *_wrap_Ldb___repr__(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + ldb *arg1 = (ldb *) 0 ; + char *result = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject *swig_obj[1] ; + + if (!args) SWIG_fail; + swig_obj[0] = args; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_ldb_context, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Ldb___repr__" "', argument " "1"" of type '" "ldb *""'"); + } + arg1 = (ldb *)(argp1); + if (arg1 == NULL) + SWIG_exception(SWIG_ValueError, + "ldb context must be non-NULL"); + result = (char *)ldb___repr__(arg1); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + SWIGINTERN PyObject *Ldb_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *obj; if (!SWIG_Python_UnpackTuple(args,(char*)"swigregister", 1, 1,&obj)) return NULL; @@ -5762,6 +5793,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"Ldb_setup_wellknown_attributes", (PyCFunction)_wrap_Ldb_setup_wellknown_attributes, METH_O, NULL}, { (char *)"Ldb___contains__", (PyCFunction) _wrap_Ldb___contains__, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"Ldb_parse_ldif", (PyCFunction) _wrap_Ldb_parse_ldif, METH_VARARGS | METH_KEYWORDS, NULL}, + { (char *)"Ldb___repr__", (PyCFunction)_wrap_Ldb___repr__, METH_O, NULL}, { (char *)"Ldb_swigregister", Ldb_swigregister, METH_VARARGS, NULL}, { (char *)"Ldb_swiginit", Ldb_swiginit, METH_VARARGS, NULL}, { (char *)"valid_attr_name", (PyCFunction) _wrap_valid_attr_name, METH_VARARGS | METH_KEYWORDS, NULL}, diff --git a/source/lib/ldb/tests/python/api.py b/source/lib/ldb/tests/python/api.py index 6f073f79a84..1ae3fde7446 100755 --- a/source/lib/ldb/tests/python/api.py +++ b/source/lib/ldb/tests/python/api.py @@ -36,6 +36,10 @@ class SimpleLdb(unittest.TestCase): x = ldb.Ldb() x.connect("foo.tdb") + def test_repr(self): + x = ldb.Ldb() + self.assertTrue(repr(x).startswith(" Date: Mon, 19 May 2008 23:36:11 +0200 Subject: [PATCH 3/4] Fix dependency. --- source/lib/tdb/tdb.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/lib/tdb/tdb.mk b/source/lib/tdb/tdb.mk index c91b1289cb2..fa8db6d34cf 100644 --- a/source/lib/tdb/tdb.mk +++ b/source/lib/tdb/tdb.mk @@ -50,7 +50,7 @@ install-python:: build-python cp $(tdbdir)/tdb.py $(DESTDIR)`$(PYTHON) -c "import distutils.sysconfig; print distutils.sysconfig.get_python_lib(0, prefix='$(prefix)')"` cp _tdb.$(SHLIBEXT) $(DESTDIR)`$(PYTHON) -c "import distutils.sysconfig; print distutils.sysconfig.get_python_lib(1, prefix='$(prefix)')"` -check-python:: build-python +check-python:: build-python $(TDB_SONAME) $(LIB_PATH_VAR)=. PYTHONPATH=".:$(tdbdir)" $(PYTHON) $(tdbdir)/python/tests/simple.py install-swig:: From 205699ed663a3c6d27695dee25bf26978615b475 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 19 May 2008 23:36:33 +0200 Subject: [PATCH 4/4] Add __repr__ implementation for Tdb. --- source/lib/tdb/python/tests/simple.py | 7 ++- source/lib/tdb/tdb.i | 4 +- source/lib/tdb/tdb.py | 8 ++-- source/lib/tdb/tdb_wrap.c | 65 +++++++++++++++++++-------- 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/source/lib/tdb/python/tests/simple.py b/source/lib/tdb/python/tests/simple.py index 94407b63981..7147718c91f 100644 --- a/source/lib/tdb/python/tests/simple.py +++ b/source/lib/tdb/python/tests/simple.py @@ -3,8 +3,8 @@ # Note that this tests the interface of the Python bindings # It does not test tdb itself. # -# Copyright (C) 2007 Jelmer Vernooij -# Published under the GNU LGPL +# Copyright (C) 2007-2008 Jelmer Vernooij +# Published under the GNU LGPLv3 or later import tdb from unittest import TestCase @@ -25,6 +25,9 @@ class SimpleTdbTests(TestCase): def tearDown(self): del self.tdb + def test_repr(self): + self.assertTrue(repr(self.tdb).startswith("Tdb('")) + def test_lockall(self): self.tdb.lock_all() diff --git a/source/lib/tdb/tdb.i b/source/lib/tdb/tdb.i index c82d2d0a6da..704f0facc63 100644 --- a/source/lib/tdb/tdb.i +++ b/source/lib/tdb/tdb.i @@ -182,8 +182,8 @@ typedef struct tdb_context { } %pythoncode { - def __str__(self): - return self.name() + def __repr__(self): + return "Tdb('%s')" % self.name() # Random access to keys, values def __getitem__(self, key): diff --git a/source/lib/tdb/tdb.py b/source/lib/tdb/tdb.py index 0effa3ff98f..eb76ca64593 100644 --- a/source/lib/tdb/tdb.py +++ b/source/lib/tdb/tdb.py @@ -1,5 +1,5 @@ # This file was automatically generated by SWIG (http://www.swig.org). -# Version 1.3.33 +# Version 1.3.35 # # Don't modify this file, modify the SWIG interface instead. @@ -80,11 +80,11 @@ TDB_ERR_EINVAL = _tdb.TDB_ERR_EINVAL TDB_ERR_RDONLY = _tdb.TDB_ERR_RDONLY class tdb(object): thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag') - def __init__(self): raise AttributeError, "No constructor defined" + def __init__(self, *args, **kwargs): raise AttributeError, "No constructor defined" __repr__ = _swig_repr __swig_destroy__ = _tdb.delete_tdb - def __str__(self): - return self.name() + def __repr__(self): + return "Tdb('%s')" % self.name() def __getitem__(self, key): diff --git a/source/lib/tdb/tdb_wrap.c b/source/lib/tdb/tdb_wrap.c index 6a5b7feffc2..f36d5699376 100644 --- a/source/lib/tdb/tdb_wrap.c +++ b/source/lib/tdb/tdb_wrap.c @@ -1,6 +1,6 @@ /* ---------------------------------------------------------------------------- * This file was automatically generated by SWIG (http://www.swig.org). - * Version 1.3.33 + * Version 1.3.35 * * This file is not intended to be easily readable and contains a number of * coding conventions designed to improve portability and efficiency. Do not make @@ -126,7 +126,7 @@ /* This should only be incremented when either the layout of swig_type_info changes, or for whatever reason, the runtime changes incompatibly */ -#define SWIG_RUNTIME_VERSION "3" +#define SWIG_RUNTIME_VERSION "4" /* define SWIG_TYPE_TABLE_NAME as "SWIG_TYPE_TABLE" */ #ifdef SWIG_TYPE_TABLE @@ -161,6 +161,7 @@ /* Flags for pointer conversions */ #define SWIG_POINTER_DISOWN 0x1 +#define SWIG_CAST_NEW_MEMORY 0x2 /* Flags for new pointer objects */ #define SWIG_POINTER_OWN 0x1 @@ -301,10 +302,10 @@ SWIGINTERNINLINE int SWIG_CheckState(int r) { extern "C" { #endif -typedef void *(*swig_converter_func)(void *); +typedef void *(*swig_converter_func)(void *, int *); typedef struct swig_type_info *(*swig_dycast_func)(void **); -/* Structure to store inforomation on one type */ +/* Structure to store information on one type */ typedef struct swig_type_info { const char *name; /* mangled name of this type */ const char *str; /* human readable name of this type */ @@ -431,8 +432,8 @@ SWIG_TypeCheckStruct(swig_type_info *from, swig_type_info *into) { Cast a pointer up an inheritance hierarchy */ SWIGRUNTIMEINLINE void * -SWIG_TypeCast(swig_cast_info *ty, void *ptr) { - return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr); +SWIG_TypeCast(swig_cast_info *ty, void *ptr, int *newmemory) { + return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr, newmemory); } /* @@ -856,7 +857,7 @@ SWIG_Python_AddErrorMsg(const char* mesg) Py_DECREF(old_str); Py_DECREF(value); } else { - PyErr_Format(PyExc_RuntimeError, mesg); + PyErr_SetString(PyExc_RuntimeError, mesg); } } @@ -1416,7 +1417,7 @@ PySwigObject_dealloc(PyObject *v) { PySwigObject *sobj = (PySwigObject *) v; PyObject *next = sobj->next; - if (sobj->own) { + if (sobj->own == SWIG_POINTER_OWN) { swig_type_info *ty = sobj->ty; PySwigClientData *data = ty ? (PySwigClientData *) ty->clientdata : 0; PyObject *destroy = data ? data->destroy : 0; @@ -1434,12 +1435,13 @@ PySwigObject_dealloc(PyObject *v) res = ((*meth)(mself, v)); } Py_XDECREF(res); - } else { - const char *name = SWIG_TypePrettyName(ty); + } #if !defined(SWIG_PYTHON_SILENT_MEMLEAK) - printf("swig/python detected a memory leak of type '%s', no destructor found.\n", name); -#endif + else { + const char *name = SWIG_TypePrettyName(ty); + printf("swig/python detected a memory leak of type '%s', no destructor found.\n", (name ? name : "unknown")); } +#endif } Py_XDECREF(next); PyObject_DEL(v); @@ -1944,7 +1946,7 @@ SWIG_Python_GetSwigThis(PyObject *pyobj) SWIGRUNTIME int SWIG_Python_AcquirePtr(PyObject *obj, int own) { - if (own) { + if (own == SWIG_POINTER_OWN) { PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); if (sobj) { int oldown = sobj->own; @@ -1965,6 +1967,8 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int return SWIG_OK; } else { PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); + if (own) + *own = 0; while (sobj) { void *vptr = sobj->ptr; if (ty) { @@ -1978,7 +1982,15 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int if (!tc) { sobj = (PySwigObject *)sobj->next; } else { - if (ptr) *ptr = SWIG_TypeCast(tc,vptr); + if (ptr) { + int newmemory = 0; + *ptr = SWIG_TypeCast(tc,vptr,&newmemory); + if (newmemory == SWIG_CAST_NEW_MEMORY) { + assert(own); + if (own) + *own = *own | SWIG_CAST_NEW_MEMORY; + } + } break; } } @@ -1988,7 +2000,8 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int } } if (sobj) { - if (own) *own = sobj->own; + if (own) + *own = *own | sobj->own; if (flags & SWIG_POINTER_DISOWN) { sobj->own = 0; } @@ -2053,8 +2066,13 @@ SWIG_Python_ConvertFunctionPtr(PyObject *obj, void **ptr, swig_type_info *ty) { } if (ty) { swig_cast_info *tc = SWIG_TypeCheck(desc,ty); - if (!tc) return SWIG_ERROR; - *ptr = SWIG_TypeCast(tc,vptr); + if (tc) { + int newmemory = 0; + *ptr = SWIG_TypeCast(tc,vptr,&newmemory); + assert(!newmemory); /* newmemory handling not yet implemented */ + } else { + return SWIG_ERROR; + } } else { *ptr = vptr; } @@ -2500,7 +2518,7 @@ static swig_module_info swig_module = {swig_types, 11, 0, 0, 0, 0}; #define SWIG_name "_tdb" -#define SWIGVERSION 0x010333 +#define SWIGVERSION 0x010335 #define SWIG_VERSION SWIGVERSION @@ -3753,7 +3771,7 @@ SWIGRUNTIME void SWIG_InitializeModule(void *clientdata) { size_t i; swig_module_info *module_head, *iter; - int found; + int found, init; clientdata = clientdata; @@ -3763,6 +3781,9 @@ SWIG_InitializeModule(void *clientdata) { swig_module.type_initial = swig_type_initial; swig_module.cast_initial = swig_cast_initial; swig_module.next = &swig_module; + init = 1; + } else { + init = 0; } /* Try and load any already created modules */ @@ -3791,6 +3812,12 @@ SWIG_InitializeModule(void *clientdata) { module_head->next = &swig_module; } + /* When multiple interpeters are used, a module could have already been initialized in + a different interpreter, but not yet have a pointer in this interpreter. + In this case, we do not want to continue adding types... everything should be + set up already */ + if (init == 0) return; + /* Now work on filling in swig_module.types */ #ifdef SWIGRUNTIME_DEBUG printf("SWIG_InitializeModule: size %d\n", swig_module.size);