Skip to content

Commit 4389d9b

Browse files
committed
Use custom ldap.LDAPBytesWarning class
Under Python 2, python-ldap now uses custom ldap.LDAPBytesWarning instead of BytesWarning to emit warnings in default bytes mode. Closes: python-ldap#99 Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 084ffe0 commit 4389d9b

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

Doc/bytes_mode.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ Unspecified: relaxed mode with warnings
6666

6767
Text values returned from python-ldap are always ``unicode``.
6868
Text values supplied to python-ldap should be ``unicode``;
69-
warnings are emitted when they are not.
69+
warnings of type :class:`ldap.LDAPBytesWarning` are emitted when they
70+
are not. :class:`ldap.LDAPBytesWarning` is a subclass of
71+
:class:`BytesWarning`.
7072

7173
Backwards-compatible behavior is not scheduled for removal until Python 2
7274
itself reaches end of life.
@@ -103,3 +105,18 @@ Note that only the result's *values* are of the ``bytes`` type:
103105
'sn': [b'Barrois'],
104106
}),
105107
]
108+
109+
110+
Filter warning
111+
--------------
112+
113+
The bytes mode warnings can be filtered out and ignored with a
114+
simple filter.
115+
116+
.. code-block:: python
117+
118+
import warnings
119+
import ldap
120+
121+
if hasattr(ldap, 'LDAPBytesWarning'):
122+
warnings.simplefilter('ignore', ldap.LDAPBytesWarning)

Lib/ldap/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def release(self):
8686

8787
from ldap.functions import open,initialize,init,get_option,set_option,escape_str,strf_secs,strp_secs
8888

89-
from ldap.ldapobject import NO_UNIQUE_ENTRY
89+
from ldap.ldapobject import NO_UNIQUE_ENTRY, LDAPBytesWarning
9090

9191
from ldap.dn import explode_dn,explode_rdn,str2dn,dn2str
9292
del str2dn

Lib/ldap/ldapobject.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'LDAPObject',
1515
'SimpleLDAPObject',
1616
'ReconnectLDAPObject',
17+
'LDAPBytesWarning'
1718
]
1819

1920

@@ -37,6 +38,12 @@
3738
else:
3839
text_type = str
3940

41+
42+
class LDAPBytesWarning(BytesWarning):
43+
"""python-ldap bytes mode warning
44+
"""
45+
46+
4047
class NO_UNIQUE_ENTRY(ldap.NO_SUCH_OBJECT):
4148
"""
4249
Exception raised if a LDAP search returned more than entry entry
@@ -84,7 +91,7 @@ def __init__(
8491
"Under Python 2, python-ldap uses bytes by default. "
8592
"This will be removed in Python 3 (no bytes for DN/RDN/field names). "
8693
"Please call initialize(..., bytes_mode=False) explicitly.",
87-
BytesWarning,
94+
LDAPBytesWarning,
8895
stacklevel=2,
8996
)
9097
bytes_mode = True
@@ -122,7 +129,7 @@ def _bytesify_input(self, value):
122129
warnings.warn(
123130
"Received non-bytes value %r with default (disabled) bytes mode; please choose an explicit "
124131
"option for bytes_mode on your LDAP connection" % (value,),
125-
BytesWarning,
132+
LDAPBytesWarning,
126133
stacklevel=6,
127134
)
128135
return value.encode('utf-8')

Tests/t_ldapobject.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import unittest
2121
import pickle
22+
import warnings
2223
from slapdtest import SlapdTestCase, requires_sasl
2324

2425
# Switch off processing .ldaprc or ldap.conf before importing _ldap
@@ -314,7 +315,41 @@ def test007_timeout(self):
314315
l.abandon(m)
315316
with self.assertRaises(ldap.TIMEOUT):
316317
result = l.result(m, timeout=0.001)
317-
318+
319+
def assertIsSubclass(self, cls, other):
320+
self.assertTrue(
321+
issubclass(cls, other),
322+
cls.__mro__
323+
)
324+
325+
@unittest.skipUnless(PY2, "no bytes_mode under Py3")
326+
def test_ldapbyteswarning(self):
327+
self.assertIsSubclass(ldap.LDAPBytesWarning, BytesWarning)
328+
self.assertIsSubclass(ldap.LDAPBytesWarning, Warning)
329+
self.assertIsInstance(self.server.suffix, text_type)
330+
with warnings.catch_warnings(record=True) as w:
331+
warnings.resetwarnings()
332+
warnings.simplefilter('default')
333+
conn = self._get_bytes_ldapobject(explicit=False)
334+
result = conn.search_s(
335+
self.server.suffix,
336+
ldap.SCOPE_SUBTREE,
337+
b'(cn=Foo*)',
338+
attrlist=[b'*'],
339+
)
340+
self.assertEqual(len(result), 4)
341+
342+
# ReconnectLDAP only emits one warning
343+
self.assertGreaterEqual(len(w), 1, w)
344+
msg = w[-1]
345+
self.assertIs(msg.category, ldap.LDAPBytesWarning)
346+
self.assertEqual(
347+
text_type(msg.message),
348+
"Received non-bytes value u'%s' with default (disabled) bytes "
349+
"mode; please choose an explicit option for bytes_mode on your "
350+
"LDAP connection" % self.server.suffix
351+
)
352+
318353

319354
class Test01_ReconnectLDAPObject(Test00_SimpleLDAPObject):
320355
"""

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