Skip to content

Commit 9fb9338

Browse files
authored
Show argument name instead of content in LDAPBytesWarning
Based on earlier patch by Christian Heimes python-ldap#145 Fixes: python-ldap#132 Fixes: python-ldap#144
1 parent ab93063 commit 9fb9338

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

Lib/ldap/ldapobject.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def __init__(
122122
# On by default on Py2, off on Py3.
123123
self.bytes_mode = bytes_mode
124124

125-
def _bytesify_input(self, value):
125+
def _bytesify_input(self, arg_name, value):
126126
"""Adapt a value following bytes_mode in Python 2.
127127
128128
In Python 3, returns the original value unmodified.
@@ -147,32 +147,17 @@ def _bytesify_input(self, value):
147147
raise TypeError("All provided fields *must* be bytes when bytes mode is on; got %r" % (value,))
148148
else:
149149
_raise_byteswarning(
150-
"Received non-bytes value %r with default (disabled) bytes mode; "
150+
"Received non-bytes value for '{}' with default (disabled) bytes mode; "
151151
"please choose an explicit "
152-
"option for bytes_mode on your LDAP connection" % (value,))
152+
"option for bytes_mode on your LDAP connection".format(arg_name))
153153
return value.encode('utf-8')
154154
else:
155155
if not isinstance(value, text_type):
156156
raise TypeError("All provided fields *must* be text when bytes mode is off; got %r" % (value,))
157157
assert not isinstance(value, bytes)
158158
return value.encode('utf-8')
159159

160-
def _bytesify_inputs(self, *values):
161-
"""Adapt values following bytes_mode.
162-
163-
Applies _bytesify_input on each arg.
164-
165-
Usage:
166-
>>> a, b, c = self._bytesify_inputs(a, b, c)
167-
"""
168-
if not PY2:
169-
return values
170-
return (
171-
self._bytesify_input(value)
172-
for value in values
173-
)
174-
175-
def _bytesify_modlist(self, modlist, with_opcode):
160+
def _bytesify_modlist(self, arg_name, modlist, with_opcode):
176161
"""Adapt a modlist according to bytes_mode.
177162
178163
A modlist is a tuple of (op, attr, value), where:
@@ -184,12 +169,12 @@ def _bytesify_modlist(self, modlist, with_opcode):
184169
return modlist
185170
if with_opcode:
186171
return tuple(
187-
(op, self._bytesify_input(attr), val)
172+
(op, self._bytesify_input(arg_name, attr), val)
188173
for op, attr, val in modlist
189174
)
190175
else:
191176
return tuple(
192-
(self._bytesify_input(attr), val)
177+
(self._bytesify_input(arg_name, attr), val)
193178
for attr, val in modlist
194179
)
195180

@@ -398,8 +383,9 @@ def add_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
398383
The parameter modlist is similar to the one passed to modify(),
399384
except that no operation integer need be included in the tuples.
400385
"""
401-
dn = self._bytesify_input(dn)
402-
modlist = self._bytesify_modlist(modlist, with_opcode=False)
386+
if PY2:
387+
dn = self._bytesify_input('dn', dn)
388+
modlist = self._bytesify_modlist('modlist', modlist, with_opcode=False)
403389
return self._ldap_call(self._l.add_ext,dn,modlist,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
404390

405391
def add_ext_s(self,dn,modlist,serverctrls=None,clientctrls=None):
@@ -424,7 +410,9 @@ def simple_bind(self,who='',cred='',serverctrls=None,clientctrls=None):
424410
"""
425411
simple_bind([who='' [,cred='']]) -> int
426412
"""
427-
who, cred = self._bytesify_inputs(who, cred)
413+
if PY2:
414+
who = self._bytesify_input('who', who)
415+
cred = self._bytesify_input('cred', cred)
428416
return self._ldap_call(self._l.simple_bind,who,cred,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
429417

430418
def simple_bind_s(self,who='',cred='',serverctrls=None,clientctrls=None):
@@ -501,7 +489,9 @@ def compare_ext(self,dn,attr,value,serverctrls=None,clientctrls=None):
501489
A design bug in the library prevents value from containing
502490
nul characters.
503491
"""
504-
dn, attr = self._bytesify_inputs(dn, attr)
492+
if PY2:
493+
dn = self._bytesify_input('dn', dn)
494+
attr = self._bytesify_input('attr', attr)
505495
return self._ldap_call(self._l.compare_ext,dn,attr,value,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
506496

507497
def compare_ext_s(self,dn,attr,value,serverctrls=None,clientctrls=None):
@@ -532,7 +522,7 @@ def delete_ext(self,dn,serverctrls=None,clientctrls=None):
532522
form returns the message id of the initiated request, and the
533523
result can be obtained from a subsequent call to result().
534524
"""
535-
dn = self._bytesify_input(dn)
525+
dn = self._bytesify_input('dn', dn)
536526
return self._ldap_call(self._l.delete_ext,dn,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
537527

538528
def delete_ext_s(self,dn,serverctrls=None,clientctrls=None):
@@ -581,8 +571,9 @@ def modify_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
581571
"""
582572
modify_ext(dn, modlist[,serverctrls=None[,clientctrls=None]]) -> int
583573
"""
584-
dn = self._bytesify_input(dn)
585-
modlist = self._bytesify_modlist(modlist, with_opcode=True)
574+
if PY2:
575+
dn = self._bytesify_input('dn', dn)
576+
modlist = self._bytesify_modlist('modlist', modlist, with_opcode=True)
586577
return self._ldap_call(self._l.modify_ext,dn,modlist,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
587578

588579
def modify_ext_s(self,dn,modlist,serverctrls=None,clientctrls=None):
@@ -636,7 +627,10 @@ def modrdn_s(self,dn,newrdn,delold=1):
636627
return self.rename_s(dn,newrdn,None,delold)
637628

638629
def passwd(self,user,oldpw,newpw,serverctrls=None,clientctrls=None):
639-
user, oldpw, newpw = self._bytesify_inputs(user, oldpw, newpw)
630+
if PY2:
631+
user = self._bytesify_input('user', user)
632+
oldpw = self._bytesify_input('oldpw', oldpw)
633+
newpw = self._bytesify_input('newpw', newpw)
640634
return self._ldap_call(self._l.passwd,user,oldpw,newpw,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
641635

642636
def passwd_s(self,user,oldpw,newpw,serverctrls=None,clientctrls=None):
@@ -658,7 +652,10 @@ def rename(self,dn,newrdn,newsuperior=None,delold=1,serverctrls=None,clientctrls
658652
This actually corresponds to the rename* routines in the
659653
LDAP-EXT C API library.
660654
"""
661-
dn, newrdn, newsuperior = self._bytesify_inputs(dn, newrdn, newsuperior)
655+
if PY2:
656+
dn = self._bytesify_input('dn', dn)
657+
newrdn = self._bytesify_input('newrdn', newrdn)
658+
newsuperior = self._bytesify_input('newsuperior', newsuperior)
662659
return self._ldap_call(self._l.rename,dn,newrdn,newsuperior,delold,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))
663660

664661
def rename_s(self,dn,newrdn,newsuperior=None,delold=1,serverctrls=None,clientctrls=None):
@@ -796,9 +793,12 @@ def search_ext(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrson
796793
The amount of search results retrieved can be limited with the
797794
sizelimit parameter if non-zero.
798795
"""
799-
base, filterstr = self._bytesify_inputs(base, filterstr)
800-
if attrlist is not None:
801-
attrlist = tuple(self._bytesify_inputs(*attrlist))
796+
if PY2:
797+
base = self._bytesify_input('base', base)
798+
filterstr = self._bytesify_input('filterstr', filterstr)
799+
if attrlist is not None:
800+
attrlist = tuple(self._bytesify_input('attrlist', a)
801+
for a in attrlist)
802802
return self._ldap_call(
803803
self._l.search_ext,
804804
base,scope,filterstr,

Tests/t_ldapobject.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ def test_ldapbyteswarning(self):
348348
self.assertIs(msg.category, ldap.LDAPBytesWarning)
349349
self.assertEqual(
350350
text_type(msg.message),
351-
"Received non-bytes value u'%s' with default (disabled) bytes "
351+
"Received non-bytes value for 'base' with default (disabled) bytes "
352352
"mode; please choose an explicit option for bytes_mode on your "
353-
"LDAP connection" % self.server.suffix
353+
"LDAP connection"
354354
)
355355

356356
@contextlib.contextmanager
@@ -394,10 +394,10 @@ def _test_byteswarning_level_search(self, methodname):
394394
self.assertEqual(len(w), 2, w)
395395

396396
self._check_byteswarning(
397-
w[0], u"Received non-bytes value u'(cn=Foo*)'")
397+
w[0], u"Received non-bytes value for 'filterstr'")
398398

399399
self._check_byteswarning(
400-
w[1], u"Received non-bytes value u'*'")
400+
w[1], u"Received non-bytes value for 'attrlist'")
401401

402402
@unittest.skipUnless(PY2, "no bytes_mode under Py3")
403403
def test_byteswarning_level_search(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy