ldap: Fixed iteration errors from dynldap library.

This commit is contained in:
Timo Mkinen 2013-09-02 13:41:45 +03:00
parent ac6c8de81b
commit f33820a1f5

View file

@ -312,6 +312,19 @@ class Connection:
class LdapResult:
class Iterator:
def __init__(self, result):
self._result = result
self._position = -1
def next(self):
try:
r = self._result[self._position + 1]
self._position = self._position + 1
except IndexError:
raise StopIteration
return r
def __init__(self, conn, msgid, entryclass = LdapEntry):
self._conn = conn
self._msgid = msgid
@ -321,43 +334,47 @@ class LdapResult:
def __del__(self):
try:
self._conn._conn.abandon(self._msgid)
except ldap.LDAPError:
except (ldap.LDAPError, TypeError):
pass
def __getitem__(self, idx):
while len(self._entries)-1 < idx:
try:
self.next()
self._next()
except StopIteration:
raise IndexError('list index out of range')
return self._entries[idx]
def __iter__(self):
return self
return self.Iterator(self)
def __len__(self):
try:
self.next()
except StopIteration:
pass
while True:
try:
self._next()
except StopIteration:
break
return len(self._entries)
def __str__(self):
return str(self.keys())
def _next(self):
if self._msgid is None:
raise StopIteration
try:
(rtype, entry) = self._conn._conn.result(self._msgid, all=0,
timeout=-1)
if rtype == ldap.RES_SEARCH_RESULT:
self._msgid = None
raise StopIteration
except IndexError:
raise StopIteration
self._entries.append(self._entryclass(self._conn, entry[0]))
return self._entries[len(self._entries)-1]
def keys(self):
r = []
for entry in self:
r.append(entry.dn)
return r
def next(self):
try:
(rtype, entry) = self._conn._conn.result(self._msgid, all=0,
timeout=-1)
if rtype == ldap.RES_SEARCH_RESULT:
raise StopIteration
except IndexError:
raise StopIteration
self._entries.append(self._entryclass(self._conn, entry[0]))
return self._entries[len(self._entries)-1]