1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

python/ldb: Add __getitem__ implementation for LdbMessageElement.

(This used to be commit e6498a0780)
This commit is contained in:
Jelmer Vernooij 2008-01-11 00:23:33 +01:00
parent 4f6177da78
commit 9269db983d
4 changed files with 73 additions and 0 deletions

View File

@ -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

View File

@ -94,6 +94,12 @@ class ldb_msg_element(object):
def __init__(self): raise AttributeError, "No constructor defined"
__repr__ = _swig_repr
__swig_destroy__ = _ldb.delete_ldb_msg_element
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
@ -104,6 +110,7 @@ class ldb_msg_element(object):
ldb_msg_element.__iter__ = new_instancemethod(_ldb.ldb_msg_element___iter__,None,ldb_msg_element)
ldb_msg_element.__set__ = new_instancemethod(_ldb.ldb_msg_element___set__,None,ldb_msg_element)
ldb_msg_element.get = new_instancemethod(_ldb.ldb_msg_element_get,None,ldb_msg_element)
ldb_msg_element.__cmp__ = new_instancemethod(_ldb.ldb_msg_element___cmp__,None,ldb_msg_element)
ldb_msg_element_swigregister = _ldb.ldb_msg_element_swigregister
ldb_msg_element_swigregister(ldb_msg_element)

View File

@ -2889,6 +2889,14 @@ SWIG_AsVal_int (PyObject * obj, int *val)
SWIGINTERN ldb_msg_element *new_ldb_msg_element(PyObject *set_obj,int flags,char const *name){
return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
}
SWIGINTERN PyObject *ldb_msg_element_get(ldb_msg_element *self,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);
}
SWIGINTERN void delete_ldb_msg_element(ldb_msg_element *self){ talloc_free(self); }
PyObject *ldb_msg_list_elements(ldb_msg *msg)
@ -3671,6 +3679,40 @@ fail:
}
SWIGINTERN PyObject *_wrap_ldb_msg_element_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) {
PyObject *resultobj = 0;
ldb_msg_element *arg1 = (ldb_msg_element *) 0 ;
int arg2 ;
PyObject *result = 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
int val2 ;
int ecode2 = 0 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
char * kwnames[] = {
(char *) "self",(char *) "i", NULL
};
if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"OO:ldb_msg_element_get",kwnames,&obj0,&obj1)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_ldb_message_element, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "ldb_msg_element_get" "', argument " "1"" of type '" "ldb_msg_element *""'");
}
arg1 = (ldb_msg_element *)(argp1);
ecode2 = SWIG_AsVal_int(obj1, &val2);
if (!SWIG_IsOK(ecode2)) {
SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "ldb_msg_element_get" "', argument " "2"" of type '" "int""'");
}
arg2 = (int)(val2);
result = (PyObject *)ldb_msg_element_get(arg1,arg2);
resultobj = result;
return resultobj;
fail:
return NULL;
}
SWIGINTERN PyObject *_wrap_delete_ldb_msg_element(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
ldb_msg_element *arg1 = (ldb_msg_element *) 0 ;
@ -5254,6 +5296,7 @@ static PyMethodDef SwigMethods[] = {
{ (char *)"ldb_msg_element___iter__", (PyCFunction)_wrap_ldb_msg_element___iter__, METH_O, NULL},
{ (char *)"ldb_msg_element___set__", (PyCFunction)_wrap_ldb_msg_element___set__, METH_O, NULL},
{ (char *)"new_MessageElement", (PyCFunction) _wrap_new_MessageElement, METH_VARARGS | METH_KEYWORDS, NULL},
{ (char *)"ldb_msg_element_get", (PyCFunction) _wrap_ldb_msg_element_get, METH_VARARGS | METH_KEYWORDS, NULL},
{ (char *)"delete_ldb_msg_element", (PyCFunction)_wrap_delete_ldb_msg_element, METH_O, NULL},
{ (char *)"ldb_msg_element___cmp__", (PyCFunction) _wrap_ldb_msg_element___cmp__, METH_VARARGS | METH_KEYWORDS, NULL},
{ (char *)"ldb_msg_element_swigregister", ldb_msg_element_swigregister, METH_VARARGS, NULL},

View File

@ -382,6 +382,12 @@ class MessageElementTests(unittest.TestCase):
x = ldb.MessageElement(["foo"])
self.assertEquals(["foo"], list(x))
def test_get_item(self):
x = ldb.MessageElement(["foo", "bar"])
self.assertEquals("foo", x[0])
self.assertEquals("bar", x[1])
self.assertRaises(KeyError, lambda: x[-1])
class ExampleModule:
name = "example"