Skip to content

Commit e56f617

Browse files
tiranencukou
authored andcommitted
Add workarounds for default argument types
Several default arguments are not compatible with bytes mode. Default to bytes in bytes mode. See: python-ldap#147 Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 2f2f64c commit e56f617

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

Lib/ldap/ldapobject.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,12 @@ def search_ext(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrson
795795
"""
796796
if PY2:
797797
base = self._bytesify_input('base', base)
798-
filterstr = self._bytesify_input('filterstr', filterstr)
798+
# workaround for default argument,
799+
# see https://github.com/python-ldap/python-ldap/issues/147
800+
if self.bytes_mode and filterstr == '(objectClass=*)':
801+
filterstr = b'(objectClass=*)'
802+
else:
803+
filterstr = self._bytesify_input('filterstr', filterstr)
799804
if attrlist is not None:
800805
attrlist = tuple(self._bytesify_input('attrlist', a)
801806
for a in attrlist)
@@ -885,7 +890,7 @@ def set_option(self,option,invalue):
885890
invalue = RequestControlTuples(invalue)
886891
return self._ldap_call(self._l.set_option,option,invalue)
887892

888-
def search_subschemasubentry_s(self,dn=''):
893+
def search_subschemasubentry_s(self,dn=None):
889894
"""
890895
Returns the distinguished name of the sub schema sub entry
891896
for a part of a DIT specified by dn.
@@ -895,9 +900,17 @@ def search_subschemasubentry_s(self,dn=''):
895900
896901
Returns: None or text/bytes depending on bytes_mode.
897902
"""
903+
if self.bytes_mode:
904+
empty_dn = b''
905+
attrname = b'subschemaSubentry'
906+
else:
907+
empty_dn = u''
908+
attrname = u'subschemaSubentry'
909+
if dn is None:
910+
dn = empty_dn
898911
try:
899912
r = self.search_s(
900-
dn,ldap.SCOPE_BASE,'(objectClass=*)',['subschemaSubentry']
913+
dn,ldap.SCOPE_BASE,'(objectClass=*)',[attrname]
901914
)
902915
except (ldap.NO_SUCH_OBJECT,ldap.NO_SUCH_ATTRIBUTE,ldap.INSUFFICIENT_ACCESS):
903916
r = []
@@ -906,11 +919,11 @@ def search_subschemasubentry_s(self,dn=''):
906919
try:
907920
if r:
908921
e = ldap.cidict.cidict(r[0][1])
909-
search_subschemasubentry_dn = e.get('subschemaSubentry',[None])[0]
922+
search_subschemasubentry_dn = e.get(attrname,[None])[0]
910923
if search_subschemasubentry_dn is None:
911924
if dn:
912925
# Try to find sub schema sub entry in root DSE
913-
return self.search_subschemasubentry_s(dn='')
926+
return self.search_subschemasubentry_s(dn=empty_dn)
914927
else:
915928
# If dn was already root DSE we can return here
916929
return None
@@ -945,11 +958,19 @@ def read_subschemasubentry_s(self,subschemasubentry_dn,attrs=None):
945958
"""
946959
Returns the sub schema sub entry's data
947960
"""
961+
if self.bytes_mode:
962+
filterstr = b'(objectClass=subschema)'
963+
if attrs is None:
964+
attrs = [attr.encode('utf-8') for attr in SCHEMA_ATTRS]
965+
else:
966+
filterstr = u'(objectClass=subschema)'
967+
if attrs is None:
968+
attrs = SCHEMA_ATTRS
948969
try:
949970
subschemasubentry = self.read_s(
950971
subschemasubentry_dn,
951-
filterstr='(objectClass=subschema)',
952-
attrlist=attrs or SCHEMA_ATTRS
972+
filterstr=filterstr,
973+
attrlist=attrs
953974
)
954975
except ldap.NO_SUCH_OBJECT:
955976
return None
@@ -964,7 +985,7 @@ def find_unique_entry(self,base,scope=ldap.SCOPE_SUBTREE,filterstr='(objectClass
964985
base,
965986
scope,
966987
filterstr,
967-
attrlist=attrlist or ['*'],
988+
attrlist=attrlist,
968989
attrsonly=attrsonly,
969990
serverctrls=serverctrls,
970991
clientctrls=clientctrls,
@@ -979,10 +1000,16 @@ def read_rootdse_s(self, filterstr='(objectClass=*)', attrlist=None):
9791000
"""
9801001
convenience wrapper around read_s() for reading rootDSE
9811002
"""
1003+
if self.bytes_mode:
1004+
base = b''
1005+
attrlist = attrlist or [b'*', b'+']
1006+
else:
1007+
base = u''
1008+
attrlist = attrlist or [u'*', u'+']
9821009
ldap_rootdse = self.read_s(
983-
'',
1010+
base,
9841011
filterstr=filterstr,
985-
attrlist=attrlist or ['*', '+'],
1012+
attrlist=attrlist,
9861013
)
9871014
return ldap_rootdse # read_rootdse_s()
9881015

@@ -991,9 +1018,13 @@ def get_naming_contexts(self):
9911018
returns all attribute values of namingContexts in rootDSE
9921019
if namingContexts is not present (not readable) then empty list is returned
9931020
"""
1021+
if self.bytes_mode:
1022+
name = b'namingContexts'
1023+
else:
1024+
name = u'namingContexts'
9941025
return self.read_rootdse_s(
995-
attrlist=['namingContexts']
996-
).get('namingContexts', [])
1026+
attrlist=[name]
1027+
).get(name, [])
9971028

9981029

9991030
class ReconnectLDAPObject(SimpleLDAPObject):

Tests/t_ldapobject.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ def test_bytesmode_search_results_have_bytes(self):
162162
self.assertEqual(type(value), bytes)
163163

164164
@unittest.skipUnless(PY2, "no bytes_mode under Py3")
165-
@unittest.expectedFailure
166165
def test_bytesmode_search_defaults(self):
167166
l = self._get_bytes_ldapobject()
168167
base = 'cn=Foo1,' + self.server.suffix
@@ -335,7 +334,7 @@ def test_search_subschema(self):
335334

336335
@unittest.skipUnless(PY2, "no bytes_mode under Py3")
337336
def test_search_subschema_have_bytes(self):
338-
l = self._get_bytes_ldapobject(explicit=False)
337+
l = self._get_bytes_ldapobject()
339338
dn = l.search_subschemasubentry_s()
340339
self.assertIsInstance(dn, bytes)
341340
self.assertEqual(dn, b"cn=Subschema")
@@ -526,7 +525,6 @@ def test_dse(self):
526525
)
527526

528527
@unittest.skipUnless(PY2, "no bytes_mode under Py3")
529-
@unittest.expectedFailure
530528
def test_dse_bytes(self):
531529
l = self._get_bytes_ldapobject()
532530
dse = l.read_rootdse_s()

0 commit comments

Comments
 (0)