1616 PY2 = False
1717 text_type = str
1818
19+ import contextlib
20+ import linecache
1921import os
2022import unittest
23+ import warnings
2124import pickle
2225import warnings
2326from slapdtest import SlapdTestCase , requires_sasl
@@ -329,7 +332,7 @@ def test_ldapbyteswarning(self):
329332 self .assertIsInstance (self .server .suffix , text_type )
330333 with warnings .catch_warnings (record = True ) as w :
331334 warnings .resetwarnings ()
332- warnings .simplefilter ('default' )
335+ warnings .simplefilter ('always' , ldap . LDAPBytesWarning )
333336 conn = self ._get_bytes_ldapobject (explicit = False )
334337 result = conn .search_s (
335338 self .server .suffix ,
@@ -350,6 +353,64 @@ def test_ldapbyteswarning(self):
350353 "LDAP connection" % self .server .suffix
351354 )
352355
356+ @contextlib .contextmanager
357+ def catch_byteswarnings (self , * args , ** kwargs ):
358+ with warnings .catch_warnings (record = True ) as w :
359+ conn = self ._get_bytes_ldapobject (* args , ** kwargs )
360+ warnings .resetwarnings ()
361+ warnings .simplefilter ('always' , ldap .LDAPBytesWarning )
362+ yield conn , w
363+
364+ def _test_byteswarning_level_search (self , methodname ):
365+ with self .catch_byteswarnings (explicit = False ) as (conn , w ):
366+ method = getattr (conn , methodname )
367+ result = method (
368+ self .server .suffix .encode ('utf-8' ),
369+ ldap .SCOPE_SUBTREE ,
370+ '(cn=Foo*)' ,
371+ attrlist = ['*' ], # CORRECT LINE
372+ )
373+ self .assertEqual (len (result ), 4 )
374+
375+ self .assertEqual (len (w ), 2 , w )
376+
377+ def _normalize (filename ):
378+ # Python 2 likes to report the ".pyc" file in warnings,
379+ # tracebacks or __file__.
380+ # Use the corresponding ".py" in that case.
381+ if filename .endswith ('.pyc' ):
382+ return filename [:- 1 ]
383+ return filename
384+
385+ self .assertIs (w [0 ].category , ldap .LDAPBytesWarning )
386+ self .assertIn (
387+ u"Received non-bytes value u'(cn=Foo*)'" ,
388+ text_type (w [0 ].message )
389+ )
390+ self .assertEqual (_normalize (w [1 ].filename ), _normalize (__file__ ))
391+ self .assertEqual (_normalize (w [0 ].filename ), _normalize (__file__ ))
392+ self .assertIn (
393+ 'CORRECT LINE' ,
394+ linecache .getline (w [0 ].filename , w [0 ].lineno )
395+ )
396+
397+ self .assertIs (w [1 ].category , ldap .LDAPBytesWarning )
398+ self .assertIn (
399+ u"Received non-bytes value u'*'" ,
400+ text_type (w [1 ].message )
401+ )
402+ self .assertIn (_normalize (w [1 ].filename ), _normalize (__file__ ))
403+ self .assertIn (
404+ 'CORRECT LINE' ,
405+ linecache .getline (w [1 ].filename , w [1 ].lineno )
406+ )
407+
408+ @unittest .skipUnless (PY2 , "no bytes_mode under Py3" )
409+ def test_byteswarning_level_search (self ):
410+ self ._test_byteswarning_level_search ('search_s' )
411+ self ._test_byteswarning_level_search ('search_st' )
412+ self ._test_byteswarning_level_search ('search_ext_s' )
413+
353414
354415class Test01_ReconnectLDAPObject (Test00_SimpleLDAPObject ):
355416 """
0 commit comments