Skip to content

Commit

Permalink
Show argument name instead of content in LDAPBytesWarning
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Viktorin authored and GitHub committed Dec 20, 2017
1 parent ab93063 commit 9fb9338
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
66 changes: 33 additions & 33 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __init__(
# On by default on Py2, off on Py3.
self.bytes_mode = bytes_mode

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

def _bytesify_inputs(self, *values):
"""Adapt values following bytes_mode.
Applies _bytesify_input on each arg.
Usage:
>>> a, b, c = self._bytesify_inputs(a, b, c)
"""
if not PY2:
return values
return (
self._bytesify_input(value)
for value in values
)

def _bytesify_modlist(self, modlist, with_opcode):
def _bytesify_modlist(self, arg_name, modlist, with_opcode):
"""Adapt a modlist according to bytes_mode.
A modlist is a tuple of (op, attr, value), where:
Expand All @@ -184,12 +169,12 @@ def _bytesify_modlist(self, modlist, with_opcode):
return modlist
if with_opcode:
return tuple(
(op, self._bytesify_input(attr), val)
(op, self._bytesify_input(arg_name, attr), val)
for op, attr, val in modlist
)
else:
return tuple(
(self._bytesify_input(attr), val)
(self._bytesify_input(arg_name, attr), val)
for attr, val in modlist
)

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

def add_ext_s(self,dn,modlist,serverctrls=None,clientctrls=None):
Expand All @@ -424,7 +410,9 @@ def simple_bind(self,who='',cred='',serverctrls=None,clientctrls=None):
"""
simple_bind([who='' [,cred='']]) -> int
"""
who, cred = self._bytesify_inputs(who, cred)
if PY2:
who = self._bytesify_input('who', who)
cred = self._bytesify_input('cred', cred)
return self._ldap_call(self._l.simple_bind,who,cred,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))

def simple_bind_s(self,who='',cred='',serverctrls=None,clientctrls=None):
Expand Down Expand Up @@ -501,7 +489,9 @@ def compare_ext(self,dn,attr,value,serverctrls=None,clientctrls=None):
A design bug in the library prevents value from containing
nul characters.
"""
dn, attr = self._bytesify_inputs(dn, attr)
if PY2:
dn = self._bytesify_input('dn', dn)
attr = self._bytesify_input('attr', attr)
return self._ldap_call(self._l.compare_ext,dn,attr,value,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))

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

def delete_ext_s(self,dn,serverctrls=None,clientctrls=None):
Expand Down Expand Up @@ -581,8 +571,9 @@ def modify_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
"""
modify_ext(dn, modlist[,serverctrls=None[,clientctrls=None]]) -> int
"""
dn = self._bytesify_input(dn)
modlist = self._bytesify_modlist(modlist, with_opcode=True)
if PY2:
dn = self._bytesify_input('dn', dn)
modlist = self._bytesify_modlist('modlist', modlist, with_opcode=True)
return self._ldap_call(self._l.modify_ext,dn,modlist,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))

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

def passwd(self,user,oldpw,newpw,serverctrls=None,clientctrls=None):
user, oldpw, newpw = self._bytesify_inputs(user, oldpw, newpw)
if PY2:
user = self._bytesify_input('user', user)
oldpw = self._bytesify_input('oldpw', oldpw)
newpw = self._bytesify_input('newpw', newpw)
return self._ldap_call(self._l.passwd,user,oldpw,newpw,RequestControlTuples(serverctrls),RequestControlTuples(clientctrls))

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

def rename_s(self,dn,newrdn,newsuperior=None,delold=1,serverctrls=None,clientctrls=None):
Expand Down Expand Up @@ -796,9 +793,12 @@ def search_ext(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrson
The amount of search results retrieved can be limited with the
sizelimit parameter if non-zero.
"""
base, filterstr = self._bytesify_inputs(base, filterstr)
if attrlist is not None:
attrlist = tuple(self._bytesify_inputs(*attrlist))
if PY2:
base = self._bytesify_input('base', base)
filterstr = self._bytesify_input('filterstr', filterstr)
if attrlist is not None:
attrlist = tuple(self._bytesify_input('attrlist', a)
for a in attrlist)
return self._ldap_call(
self._l.search_ext,
base,scope,filterstr,
Expand Down
8 changes: 4 additions & 4 deletions Tests/t_ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ def test_ldapbyteswarning(self):
self.assertIs(msg.category, ldap.LDAPBytesWarning)
self.assertEqual(
text_type(msg.message),
"Received non-bytes value u'%s' with default (disabled) bytes "
"Received non-bytes value for 'base' with default (disabled) bytes "
"mode; please choose an explicit option for bytes_mode on your "
"LDAP connection" % self.server.suffix
"LDAP connection"
)

@contextlib.contextmanager
Expand Down Expand Up @@ -394,10 +394,10 @@ def _test_byteswarning_level_search(self, methodname):
self.assertEqual(len(w), 2, w)

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

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

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

0 comments on commit 9fb9338

Please sign in to comment.