@@ -122,7 +122,7 @@ def __init__(
122
122
# On by default on Py2, off on Py3.
123
123
self .bytes_mode = bytes_mode
124
124
125
- def _bytesify_input (self , value ):
125
+ def _bytesify_input (self , arg_name , value ):
126
126
"""Adapt a value following bytes_mode in Python 2.
127
127
128
128
In Python 3, returns the original value unmodified.
@@ -147,32 +147,17 @@ def _bytesify_input(self, value):
147
147
raise TypeError ("All provided fields *must* be bytes when bytes mode is on; got %r" % (value ,))
148
148
else :
149
149
_raise_byteswarning (
150
- "Received non-bytes value %r with default (disabled) bytes mode; "
150
+ "Received non-bytes value for '{}' with default (disabled) bytes mode; "
151
151
"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 ))
153
153
return value .encode ('utf-8' )
154
154
else :
155
155
if not isinstance (value , text_type ):
156
156
raise TypeError ("All provided fields *must* be text when bytes mode is off; got %r" % (value ,))
157
157
assert not isinstance (value , bytes )
158
158
return value .encode ('utf-8' )
159
159
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 ):
176
161
"""Adapt a modlist according to bytes_mode.
177
162
178
163
A modlist is a tuple of (op, attr, value), where:
@@ -184,12 +169,12 @@ def _bytesify_modlist(self, modlist, with_opcode):
184
169
return modlist
185
170
if with_opcode :
186
171
return tuple (
187
- (op , self ._bytesify_input (attr ), val )
172
+ (op , self ._bytesify_input (arg_name , attr ), val )
188
173
for op , attr , val in modlist
189
174
)
190
175
else :
191
176
return tuple (
192
- (self ._bytesify_input (attr ), val )
177
+ (self ._bytesify_input (arg_name , attr ), val )
193
178
for attr , val in modlist
194
179
)
195
180
@@ -398,8 +383,9 @@ def add_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
398
383
The parameter modlist is similar to the one passed to modify(),
399
384
except that no operation integer need be included in the tuples.
400
385
"""
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 )
403
389
return self ._ldap_call (self ._l .add_ext ,dn ,modlist ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
404
390
405
391
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):
424
410
"""
425
411
simple_bind([who='' [,cred='']]) -> int
426
412
"""
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 )
428
416
return self ._ldap_call (self ._l .simple_bind ,who ,cred ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
429
417
430
418
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):
501
489
A design bug in the library prevents value from containing
502
490
nul characters.
503
491
"""
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 )
505
495
return self ._ldap_call (self ._l .compare_ext ,dn ,attr ,value ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
506
496
507
497
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):
532
522
form returns the message id of the initiated request, and the
533
523
result can be obtained from a subsequent call to result().
534
524
"""
535
- dn = self ._bytesify_input (dn )
525
+ dn = self ._bytesify_input ('dn' , dn )
536
526
return self ._ldap_call (self ._l .delete_ext ,dn ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
537
527
538
528
def delete_ext_s (self ,dn ,serverctrls = None ,clientctrls = None ):
@@ -581,8 +571,9 @@ def modify_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
581
571
"""
582
572
modify_ext(dn, modlist[,serverctrls=None[,clientctrls=None]]) -> int
583
573
"""
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 )
586
577
return self ._ldap_call (self ._l .modify_ext ,dn ,modlist ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
587
578
588
579
def modify_ext_s (self ,dn ,modlist ,serverctrls = None ,clientctrls = None ):
@@ -636,7 +627,10 @@ def modrdn_s(self,dn,newrdn,delold=1):
636
627
return self .rename_s (dn ,newrdn ,None ,delold )
637
628
638
629
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 )
640
634
return self ._ldap_call (self ._l .passwd ,user ,oldpw ,newpw ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
641
635
642
636
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
658
652
This actually corresponds to the rename* routines in the
659
653
LDAP-EXT C API library.
660
654
"""
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 )
662
659
return self ._ldap_call (self ._l .rename ,dn ,newrdn ,newsuperior ,delold ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
663
660
664
661
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
796
793
The amount of search results retrieved can be limited with the
797
794
sizelimit parameter if non-zero.
798
795
"""
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 )
802
802
return self ._ldap_call (
803
803
self ._l .search_ext ,
804
804
base ,scope ,filterstr ,
0 commit comments