Skip to content

Commit 31abc2e

Browse files
authored
Merge pull request python-ldap#103 – Use custom ldap.LDAPBytesWarning class
python-ldap#103
2 parents a28d2be + 8290270 commit 31abc2e

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

Doc/bytes_mode.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Unspecified: relaxed mode with warnings
6868
Text values supplied to python-ldap should be ``unicode``;
6969
warnings are emitted when they are not.
7070

71+
The warnings are of type :class:`~ldap.LDAPBytesWarning`, which
72+
is a subclass of :class:`BytesWarning` designed to be easily
73+
:ref:`filtered out <filter-bytes-warning>` if needed.
74+
7175
Backwards-compatible behavior is not scheduled for removal until Python 2
7276
itself reaches end of life.
7377

@@ -103,3 +107,20 @@ Note that only the result's *values* are of the ``bytes`` type:
103107
'sn': [b'Barrois'],
104108
}),
105109
]
110+
111+
112+
.. _filter-bytes-warning:
113+
114+
Filtering warnings
115+
------------------
116+
117+
The bytes mode warnings can be filtered out and ignored with a
118+
simple filter.
119+
120+
.. code-block:: python
121+
122+
import warnings
123+
import ldap
124+
125+
if hasattr(ldap, 'LDAPBytesWarning'):
126+
warnings.simplefilter('ignore', ldap.LDAPBytesWarning)

Doc/reference/ldap.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,18 @@ The above exceptions are raised when a result code from an underlying API
564564
call does not indicate success.
565565

566566

567+
Warnings
568+
========
569+
570+
.. py:class:: LDAPBytesWarning
571+
572+
Raised when bytes/text mismatch in non-strict bytes mode.
573+
574+
See :ref:`bytes_mode` for details.
575+
576+
.. versionadded:: 3.0.0
577+
578+
567579
.. _ldap-objects:
568580

569581
LDAPObject classes

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