From 9269db983d847be7a0cdc9eb2bcc4ebe3066be1c Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 11 Jan 2008 00:23:33 +0100 Subject: python/ldb: Add __getitem__ implementation for LdbMessageElement. (This used to be commit e6498a0780dd31dfc623a69432004b606aeaccbe) --- source4/lib/ldb/ldb.i | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'source4/lib/ldb/ldb.i') diff --git a/source4/lib/ldb/ldb.i b/source4/lib/ldb/ldb.i index 57cb6b5f47..0ad628fdf5 100644 --- a/source4/lib/ldb/ldb.i +++ b/source4/lib/ldb/ldb.i @@ -279,10 +279,27 @@ typedef struct ldb_message_element { return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name); } #endif + + PyObject *get(int i) + { + if (i < 0 || i >= $self->num_values) + return Py_None; + + return PyString_FromStringAndSize( + (const char *)$self->values[i].data, + $self->values[i].length); + } + ~ldb_msg_element() { talloc_free($self); } int compare(ldb_msg_element *); } %pythoncode { + def __getitem__(self, i): + ret = self.get(i) + if ret is None: + raise KeyError("no such value") + return ret + def __eq__(self, other): if (isinstance(other, str) and len(set(self)) == 1 and -- cgit From c3695026e1cfac3e835e937ed9343d82500cf197 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 11 Jan 2008 01:02:58 +0100 Subject: ldb/python: Implement __len__ for MessageElement. (This used to be commit a8f90ed34ce9341080b63c801ef54b82de42b8e6) --- source4/lib/ldb/ldb.i | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'source4/lib/ldb/ldb.i') diff --git a/source4/lib/ldb/ldb.i b/source4/lib/ldb/ldb.i index 0ad628fdf5..57fa36584e 100644 --- a/source4/lib/ldb/ldb.i +++ b/source4/lib/ldb/ldb.i @@ -278,6 +278,11 @@ typedef struct ldb_message_element { { return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name); } + + int __len__() + { + return $self->num_values; + } #endif PyObject *get(int i) -- cgit From 2466d2cc5e210a124bb23cb9d2c8d3cc608a7c7f Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 11 Jan 2008 01:55:56 +0100 Subject: ldb/python: Allow comparing a MessageElement to a list or a singleton. (This used to be commit 1ccbab81d79f83bb419104f2bbaf2ae7b368e90f) --- source4/lib/ldb/ldb.i | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'source4/lib/ldb/ldb.i') diff --git a/source4/lib/ldb/ldb.i b/source4/lib/ldb/ldb.i index 57fa36584e..b6718351f8 100644 --- a/source4/lib/ldb/ldb.i +++ b/source4/lib/ldb/ldb.i @@ -306,12 +306,15 @@ typedef struct ldb_message_element { return ret def __eq__(self, other): - if (isinstance(other, str) and - len(set(self)) == 1 and - set(self).pop() == other): + if (len(self) == 1 and self.get(0) == other): return True - return self.__cmp__(other) == 0 - + if isinstance(other, self.__class__): + return self.__cmp__(other) == 0 + o = iter(other) + for i in range(len(self)): + if self.get(i) != o.next(): + return False + return True } } ldb_msg_element; -- cgit From 1f4838b8a3e5f0c7c400097f56329e5a133ec4e8 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 11 Jan 2008 02:36:59 +0100 Subject: python/ldap: Wrap parse_control_strings(). (This used to be commit b27e5a68530c4fd6430cbb174b63f8ff2b6f4e53) --- source4/lib/ldb/ldb.i | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'source4/lib/ldb/ldb.i') diff --git a/source4/lib/ldb/ldb.i b/source4/lib/ldb/ldb.i index b6718351f8..cf4a335954 100644 --- a/source4/lib/ldb/ldb.i +++ b/source4/lib/ldb/ldb.i @@ -50,6 +50,15 @@ typedef int ldb_error; %include "exception.i" %import "stdint.i" +/* Don't expose talloc contexts in Python code. Python does reference + counting for us, so just create a new top-level talloc context. + */ +%typemap(in, numinputs=0, noblock=1) TALLOC_CTX * { + $1 = NULL; +} + + + %constant int SCOPE_DEFAULT = LDB_SCOPE_DEFAULT; %constant int SCOPE_BASE = LDB_SCOPE_BASE; %constant int SCOPE_ONELEVEL = LDB_SCOPE_ONELEVEL; @@ -115,7 +124,7 @@ typedef int ldb_error; } } -%typemap(in,noblock=1,numinputs=1) const char * const *attrs { +%typemap(in,noblock=1,numinputs=1) const char * const *NULL_STR_LIST { if ($input == Py_None) { $1 = NULL; } else if (PySequence_Check($input)) { @@ -129,9 +138,13 @@ typedef int ldb_error; } } -%typemap(freearg,noblock=1) const char * const *attrs { +%typemap(freearg,noblock=1) const char * const *NULL_STR_LIST { talloc_free($1); } + +%apply const char * const *NULL_STR_LIST { const char * const *attrs } +%apply const char * const *NULL_STR_LIST { const char * const *control_strings } + #endif %types(struct ldb_result *); @@ -472,6 +485,14 @@ PyObject *PyExc_LdbError; $result = Py_None; }; +%typemap(out,noblock=1) struct ldb_control ** { + if ($1 == NULL) { + PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(s)", ldb_errstring(arg1))); + SWIG_fail; + } + $result = SWIG_NewPointerObj($1, $1_descriptor, 0); +} + %rename(Ldb) ldb_context; %typemap(in,noblock=1) struct ldb_dn * { @@ -500,6 +521,8 @@ typedef struct ldb_context { struct ldb_result **OUT); ldb_error delete(ldb_dn *dn); ldb_error rename(ldb_dn *olddn, ldb_dn *newdn); + struct ldb_control **parse_control_strings(TALLOC_CTX *mem_ctx, + const char * const*control_strings); ldb_error add(ldb_msg *add_msg); ldb_error add(PyObject *py_msg) { -- cgit