@@ -48,6 +48,7 @@ class SimpleLDAPObject:
48
48
"""
49
49
Drop-in wrapper class around _ldap.LDAPObject
50
50
"""
51
+ _stacklevel = 3
51
52
52
53
CLASSATTR_OPTION_MAPPING = {
53
54
"protocol_version" : ldap .OPT_PROTOCOL_VERSION ,
@@ -95,7 +96,7 @@ def __init__(
95
96
# On by default on Py2, off on Py3.
96
97
self .bytes_mode = bytes_mode
97
98
98
- def _bytesify_input (self , value ):
99
+ def _bytesify_input (self , value , stack_mod = 0 ):
99
100
"""Adapt a value following bytes_mode in Python 2.
100
101
101
102
In Python 3, returns the original value unmodified.
@@ -123,7 +124,7 @@ def _bytesify_input(self, value):
123
124
"Received non-bytes value %r with default (disabled) bytes mode; please choose an explicit "
124
125
"option for bytes_mode on your LDAP connection" % (value ,),
125
126
BytesWarning ,
126
- stacklevel = 6 ,
127
+ stacklevel = self . _stacklevel + stack_mod ,
127
128
)
128
129
return value .encode ('utf-8' )
129
130
else :
@@ -132,21 +133,6 @@ def _bytesify_input(self, value):
132
133
assert not isinstance (value , bytes )
133
134
return value .encode ('utf-8' )
134
135
135
- def _bytesify_inputs (self , * values ):
136
- """Adapt values following bytes_mode.
137
-
138
- Applies _bytesify_input on each arg.
139
-
140
- Usage:
141
- >>> a, b, c = self._bytesify_inputs(a, b, c)
142
- """
143
- if not PY2 :
144
- return values
145
- return (
146
- self ._bytesify_input (value )
147
- for value in values
148
- )
149
-
150
136
def _bytesify_modlist (self , modlist , with_opcode ):
151
137
"""Adapt a modlist according to bytes_mode.
152
138
@@ -159,12 +145,12 @@ def _bytesify_modlist(self, modlist, with_opcode):
159
145
return modlist
160
146
if with_opcode :
161
147
return tuple (
162
- (op , self ._bytesify_input (attr ), val )
148
+ (op , self ._bytesify_input (attr , stack_mod = 1 ), val )
163
149
for op , attr , val in modlist
164
150
)
165
151
else :
166
152
return tuple (
167
- (self ._bytesify_input (attr ), val )
153
+ (self ._bytesify_input (attr , stack_mod = 1 ), val )
168
154
for attr , val in modlist
169
155
)
170
156
@@ -373,8 +359,9 @@ def add_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
373
359
The parameter modlist is similar to the one passed to modify(),
374
360
except that no operation integer need be included in the tuples.
375
361
"""
376
- dn = self ._bytesify_input (dn )
377
- modlist = self ._bytesify_modlist (modlist , with_opcode = False )
362
+ if PY2 :
363
+ dn = self ._bytesify_input (dn )
364
+ modlist = self ._bytesify_modlist (modlist , with_opcode = False )
378
365
return self ._ldap_call (self ._l .add_ext ,dn ,modlist ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
379
366
380
367
def add_ext_s (self ,dn ,modlist ,serverctrls = None ,clientctrls = None ):
@@ -399,7 +386,9 @@ def simple_bind(self,who='',cred='',serverctrls=None,clientctrls=None):
399
386
"""
400
387
simple_bind([who='' [,cred='']]) -> int
401
388
"""
402
- who , cred = self ._bytesify_inputs (who , cred )
389
+ if PY2 :
390
+ who = self ._bytesify_input (who )
391
+ cred = self ._bytesify_input (cred )
403
392
return self ._ldap_call (self ._l .simple_bind ,who ,cred ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
404
393
405
394
def simple_bind_s (self ,who = '' ,cred = '' ,serverctrls = None ,clientctrls = None ):
@@ -458,7 +447,7 @@ def sasl_bind_s(self,dn,mechanism,cred,serverctrls=None,clientctrls=None):
458
447
"""
459
448
return self ._ldap_call (self ._l .sasl_bind_s ,dn ,mechanism ,cred ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
460
449
461
- def compare_ext (self ,dn ,attr ,value ,serverctrls = None ,clientctrls = None ):
450
+ def compare_ext (self ,dn ,attr ,value ,serverctrls = None ,clientctrls = None , _stackup = 0 ):
462
451
"""
463
452
compare_ext(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> int
464
453
compare_ext_s(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> int
@@ -476,11 +465,13 @@ def compare_ext(self,dn,attr,value,serverctrls=None,clientctrls=None):
476
465
A design bug in the library prevents value from containing
477
466
nul characters.
478
467
"""
479
- dn , attr = self ._bytesify_inputs (dn , attr )
468
+ if PY2 :
469
+ dn = self ._bytesify_input (dn , _stackup )
470
+ attr = self ._bytesify_input (attr , _stackup )
480
471
return self ._ldap_call (self ._l .compare_ext ,dn ,attr ,value ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
481
472
482
- def compare_ext_s (self ,dn ,attr ,value ,serverctrls = None ,clientctrls = None ):
483
- msgid = self .compare_ext (dn ,attr ,value ,serverctrls ,clientctrls )
473
+ def compare_ext_s (self ,dn ,attr ,value ,serverctrls = None ,clientctrls = None , _stackup = 0 ):
474
+ msgid = self .compare_ext (dn ,attr ,value ,serverctrls ,clientctrls , _stackup = _stackup + 1 )
484
475
try :
485
476
ldap_res = self .result3 (msgid ,all = 1 ,timeout = self .timeout )
486
477
except ldap .COMPARE_TRUE :
@@ -492,12 +483,12 @@ def compare_ext_s(self,dn,attr,value,serverctrls=None,clientctrls=None):
492
483
)
493
484
494
485
def compare (self ,dn ,attr ,value ):
495
- return self .compare_ext (dn ,attr ,value ,None ,None )
486
+ return self .compare_ext (dn ,attr ,value ,None ,None , _stackup = 1 )
496
487
497
488
def compare_s (self ,dn ,attr ,value ):
498
- return self .compare_ext_s (dn ,attr ,value ,None ,None )
489
+ return self .compare_ext_s (dn ,attr ,value ,None ,None , _stackup = 1 )
499
490
500
- def delete_ext (self ,dn ,serverctrls = None ,clientctrls = None ):
491
+ def delete_ext (self ,dn ,serverctrls = None ,clientctrls = None , _stackup = 0 ):
501
492
"""
502
493
delete(dn) -> int
503
494
delete_s(dn) -> None
@@ -507,19 +498,20 @@ def delete_ext(self,dn,serverctrls=None,clientctrls=None):
507
498
form returns the message id of the initiated request, and the
508
499
result can be obtained from a subsequent call to result().
509
500
"""
510
- dn = self ._bytesify_input (dn )
501
+ if PY2 :
502
+ dn = self ._bytesify_input (dn , _stackup )
511
503
return self ._ldap_call (self ._l .delete_ext ,dn ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
512
504
513
- def delete_ext_s (self ,dn ,serverctrls = None ,clientctrls = None ):
514
- msgid = self .delete_ext (dn ,serverctrls ,clientctrls )
505
+ def delete_ext_s (self ,dn ,serverctrls = None ,clientctrls = None , _stackup = 0 ):
506
+ msgid = self .delete_ext (dn ,serverctrls ,clientctrls , _stackup = _stackup + 1 )
515
507
resp_type , resp_data , resp_msgid , resp_ctrls = self .result3 (msgid ,all = 1 ,timeout = self .timeout )
516
508
return resp_type , resp_data , resp_msgid , resp_ctrls
517
509
518
510
def delete (self ,dn ):
519
- return self .delete_ext (dn ,None ,None )
511
+ return self .delete_ext (dn ,None ,None , _stackup = 1 )
520
512
521
513
def delete_s (self ,dn ):
522
- return self .delete_ext_s (dn ,None ,None )
514
+ return self .delete_ext_s (dn ,None ,None , _stackup = 1 )
523
515
524
516
def extop (self ,extreq ,serverctrls = None ,clientctrls = None ):
525
517
"""
@@ -556,8 +548,9 @@ def modify_ext(self,dn,modlist,serverctrls=None,clientctrls=None):
556
548
"""
557
549
modify_ext(dn, modlist[,serverctrls=None[,clientctrls=None]]) -> int
558
550
"""
559
- dn = self ._bytesify_input (dn )
560
- modlist = self ._bytesify_modlist (modlist , with_opcode = True )
551
+ if PY2 :
552
+ dn = self ._bytesify_input (dn )
553
+ modlist = self ._bytesify_modlist (modlist , with_opcode = True )
561
554
return self ._ldap_call (self ._l .modify_ext ,dn ,modlist ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
562
555
563
556
def modify_ext_s (self ,dn ,modlist ,serverctrls = None ,clientctrls = None ):
@@ -591,7 +584,7 @@ def modify(self,dn,modlist):
591
584
def modify_s (self ,dn ,modlist ):
592
585
return self .modify_ext_s (dn ,modlist ,None ,None )
593
586
594
- def modrdn (self ,dn ,newrdn ,delold = 1 ):
587
+ def modrdn (self ,dn ,newrdn ,delold = 1 , _stackup = 0 ):
595
588
"""
596
589
modrdn(dn, newrdn [,delold=1]) -> int
597
590
modrdn_s(dn, newrdn [,delold=1]) -> None
@@ -605,20 +598,23 @@ def modrdn(self,dn,newrdn,delold=1):
605
598
This operation is emulated by rename() and rename_s() methods
606
599
since the modrdn2* routines in the C library are deprecated.
607
600
"""
608
- return self .rename (dn ,newrdn ,None ,delold )
601
+ return self .rename (dn ,newrdn ,None ,delold , _stackup = _stackup + 1 )
609
602
610
603
def modrdn_s (self ,dn ,newrdn ,delold = 1 ):
611
- return self .rename_s (dn ,newrdn ,None ,delold )
604
+ return self .rename_s (dn ,newrdn ,None ,delold , _stackup = 1 )
612
605
613
606
def passwd (self ,user ,oldpw ,newpw ,serverctrls = None ,clientctrls = None ):
614
- user , oldpw , newpw = self ._bytesify_inputs (user , oldpw , newpw )
607
+ if PY2 :
608
+ user = self ._bytesify_input (user )
609
+ oldpw = self ._bytesify_input (oldpw )
610
+ newpw = self ._bytesify_input (newpw )
615
611
return self ._ldap_call (self ._l .passwd ,user ,oldpw ,newpw ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
616
612
617
613
def passwd_s (self ,user ,oldpw ,newpw ,serverctrls = None ,clientctrls = None ):
618
614
msgid = self .passwd (user ,oldpw ,newpw ,serverctrls ,clientctrls )
619
615
return self .extop_result (msgid ,all = 1 ,timeout = self .timeout )
620
616
621
- def rename (self ,dn ,newrdn ,newsuperior = None ,delold = 1 ,serverctrls = None ,clientctrls = None ):
617
+ def rename (self ,dn ,newrdn ,newsuperior = None ,delold = 1 ,serverctrls = None ,clientctrls = None , _stackup = 0 ):
622
618
"""
623
619
rename(dn, newrdn [, newsuperior=None [,delold=1][,serverctrls=None[,clientctrls=None]]]) -> int
624
620
rename_s(dn, newrdn [, newsuperior=None] [,delold=1][,serverctrls=None[,clientctrls=None]]) -> None
@@ -633,11 +629,14 @@ def rename(self,dn,newrdn,newsuperior=None,delold=1,serverctrls=None,clientctrls
633
629
This actually corresponds to the rename* routines in the
634
630
LDAP-EXT C API library.
635
631
"""
636
- dn , newrdn , newsuperior = self ._bytesify_inputs (dn , newrdn , newsuperior )
632
+ if PY2 :
633
+ dn = self ._bytesify_input (dn , _stackup )
634
+ newrdn = self ._bytesify_input (newrdn , _stackup )
635
+ newsuperior = self ._bytesify_input (newsuperior , _stackup )
637
636
return self ._ldap_call (self ._l .rename ,dn ,newrdn ,newsuperior ,delold ,RequestControlTuples (serverctrls ),RequestControlTuples (clientctrls ))
638
637
639
638
def rename_s (self ,dn ,newrdn ,newsuperior = None ,delold = 1 ,serverctrls = None ,clientctrls = None ):
640
- msgid = self .rename (dn ,newrdn ,newsuperior ,delold ,serverctrls ,clientctrls )
639
+ msgid = self .rename (dn ,newrdn ,newsuperior ,delold ,serverctrls ,clientctrls , _stackup = 1 )
641
640
resp_type , resp_data , resp_msgid , resp_ctrls = self .result3 (msgid ,all = 1 ,timeout = self .timeout )
642
641
return resp_type , resp_data , resp_msgid , resp_ctrls
643
642
@@ -726,7 +725,7 @@ def result4(self,msgid=ldap.RES_ANY,all=1,timeout=None,add_ctrls=0,add_intermedi
726
725
resp_data = self ._bytesify_results (resp_data , with_ctrls = add_ctrls )
727
726
return resp_type , resp_data , resp_msgid , decoded_resp_ctrls , resp_name , resp_value
728
727
729
- def search_ext (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ,serverctrls = None ,clientctrls = None ,timeout = - 1 ,sizelimit = 0 ):
728
+ def search_ext (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ,serverctrls = None ,clientctrls = None ,timeout = - 1 ,sizelimit = 0 , _stackup = 0 ):
730
729
"""
731
730
search(base, scope [,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0]]]) -> int
732
731
search_s(base, scope [,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0]]])
@@ -771,9 +770,12 @@ def search_ext(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrson
771
770
The amount of search results retrieved can be limited with the
772
771
sizelimit parameter if non-zero.
773
772
"""
774
- base , filterstr = self ._bytesify_inputs (base , filterstr )
775
- if attrlist is not None :
776
- attrlist = tuple (self ._bytesify_inputs (* attrlist ))
773
+ if PY2 :
774
+ bytesify_input = self ._bytesify_input
775
+ base = bytesify_input (base , _stackup )
776
+ filterstr = bytesify_input (filterstr , _stackup )
777
+ if attrlist is not None :
778
+ attrlist = tuple (bytesify_input (attr , _stackup + 1 ) for attr in attrlist )
777
779
return self ._ldap_call (
778
780
self ._l .search_ext ,
779
781
base ,scope ,filterstr ,
@@ -783,18 +785,18 @@ def search_ext(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrson
783
785
timeout ,sizelimit ,
784
786
)
785
787
786
- def search_ext_s (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ,serverctrls = None ,clientctrls = None ,timeout = - 1 ,sizelimit = 0 ):
787
- msgid = self .search_ext (base ,scope ,filterstr ,attrlist ,attrsonly ,serverctrls ,clientctrls ,timeout ,sizelimit )
788
+ def search_ext_s (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ,serverctrls = None ,clientctrls = None ,timeout = - 1 ,sizelimit = 0 , _stackup = 1 ):
789
+ msgid = self .search_ext (base ,scope ,filterstr ,attrlist ,attrsonly ,serverctrls ,clientctrls ,timeout ,sizelimit , _stackup = _stackup )
788
790
return self .result (msgid ,all = 1 ,timeout = timeout )[1 ]
789
791
790
792
def search (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ):
791
- return self .search_ext (base ,scope ,filterstr ,attrlist ,attrsonly ,None ,None )
793
+ return self .search_ext (base ,scope ,filterstr ,attrlist ,attrsonly ,None ,None , _stackup = 1 )
792
794
793
- def search_s (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ):
794
- return self .search_ext_s (base ,scope ,filterstr ,attrlist ,attrsonly ,None ,None ,timeout = self .timeout )
795
+ def search_s (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 , _stackup = 0 ):
796
+ return self .search_ext_s (base ,scope ,filterstr ,attrlist ,attrsonly ,None ,None ,timeout = self .timeout , _stackup = _stackup + 2 )
795
797
796
798
def search_st (self ,base ,scope ,filterstr = '(objectClass=*)' ,attrlist = None ,attrsonly = 0 ,timeout = - 1 ):
797
- return self .search_ext_s (base ,scope ,filterstr ,attrlist ,attrsonly ,None ,None ,timeout )
799
+ return self .search_ext_s (base ,scope ,filterstr ,attrlist ,attrsonly ,None ,None ,timeout , _stackup = 2 )
798
800
799
801
def start_tls_s (self ):
800
802
"""
@@ -872,7 +874,8 @@ def search_subschemasubentry_s(self,dn=''):
872
874
"""
873
875
try :
874
876
r = self .search_s (
875
- dn ,ldap .SCOPE_BASE ,'(objectClass=*)' ,['subschemaSubentry' ]
877
+ dn ,ldap .SCOPE_BASE ,'(objectClass=*)' ,['subschemaSubentry' ],
878
+ _stackup = 1
876
879
)
877
880
except (ldap .NO_SUCH_OBJECT ,ldap .NO_SUCH_ATTRIBUTE ,ldap .INSUFFICIENT_ACCESS ):
878
881
r = []
@@ -984,6 +987,9 @@ class ReconnectLDAPObject(SimpleLDAPObject):
984
987
application.
985
988
"""
986
989
990
+ # public method + _apply_method_s()
991
+ _stacklevel = SimpleLDAPObject ._stacklevel + 2
992
+
987
993
__transient_attrs__ = set ([
988
994
'_l' ,
989
995
'_ldap_object_lock' ,
0 commit comments