[Python-modules-commits] [ldif3] 01/05: Import ldif3_3.2.1.orig.tar.gz

Michael Fladischer fladi at moszumanska.debian.org
Wed Jan 11 11:34:43 UTC 2017


This is an automated email from the git hooks/post-receive script.

fladi pushed a commit to branch master
in repository ldif3.

commit db36ca0ae34ffcc18bcbdaef27ba204170a7796c
Author: Michael Fladischer <FladischerMichael at fladi.at>
Date:   Wed Jan 11 12:05:19 2017 +0100

    Import ldif3_3.2.1.orig.tar.gz
---
 CHANGES.rst |  7 +++++++
 ldif3.py    | 29 +++++++++++++++++++----------
 tests.py    | 19 +++++++++++++++++++
 3 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index 0dd97a1..064ae13 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,3 +1,10 @@
+3.2.1 (2016-12-27)
+------------------
+
+-   Ignore non-unicode characters in "dn" in non-strict mode. (Fixes `#4
+    https://github.com/xi/ldif3/issues/6`_)
+
+
 3.2.0 (2016-06-03)
 ------------------
 
diff --git a/ldif3.py b/ldif3.py
index a9e50eb..7b1276b 100644
--- a/ldif3.py
+++ b/ldif3.py
@@ -14,7 +14,7 @@ except ImportError:  # pragma: nocover
     from urllib.parse import urlparse
     from urllib.request import urlopen
 
-__version__ = '3.2.0'
+__version__ = '3.2.1'
 
 __all__ = [
     # constants
@@ -280,7 +280,7 @@ class LDIFParser(object):
         for line in self._iter_unfolded_lines():
             if line:
                 lines.append(line)
-            else:
+            elif lines:
                 self.records_read += 1
                 yield lines
                 lines = []
@@ -288,6 +288,22 @@ class LDIFParser(object):
             self.records_read += 1
             yield lines
 
+    def _decode_value(self, attr_type, attr_value):
+        if attr_type == u'dn':
+            try:
+                return attr_type, attr_value.decode('utf8')
+            except UnicodeError as err:
+                self._error(err)
+                return attr_type, attr_value.decode('utf8', 'ignore')
+
+        elif self._encoding is not None:
+            try:
+                return attr_type, attr_value.decode(self._encoding)
+            except UnicodeError:
+                pass
+
+        return attr_type, attr_value
+
     def _parse_attr(self, line):
         """Parse a single attribute type/value pair."""
         colon_pos = line.index(b':')
@@ -305,14 +321,7 @@ class LDIFParser(object):
         else:
             attr_value = line[colon_pos + 1:].strip()
 
-        if attr_type == u'dn':
-            return attr_type, attr_value.decode('utf8')
-        elif self._encoding is not None:
-            try:
-                return attr_type, attr_value.decode(self._encoding)
-            except UnicodeError:
-                pass
-        return attr_type, attr_value
+        return self._decode_value(attr_type, attr_value)
 
     def _error(self, msg):
         if self._strict:
diff --git a/tests.py b/tests.py
index f90153b..8264480 100644
--- a/tests.py
+++ b/tests.py
@@ -32,6 +32,8 @@ mail: foobar at example.org
 modifytimestamp: 4a463e9a
 """
 
+BYTES_SPACE = b'\n\n'.join([block + b'\n' for block in BYTES.split(b'\n\n')])
+
 BYTES_OUT = b"""dn: cn=Alice Alison,mail=alicealison at example.com
 cn: Alison Alison
 mail: alicealison at example.com
@@ -181,6 +183,11 @@ class TestLDIFParser(unittest.TestCase):
     def test_iter_blocks(self):
         self.assertEqual(list(self.p._iter_blocks()), BLOCKS)
 
+    def test_iter_blocks_with_additional_spaces(self):
+        self.stream = BytesIO(BYTES_SPACE)
+        self.p = ldif3.LDIFParser(self.stream)
+        self.assertEqual(list(self.p._iter_blocks()), BLOCKS)
+
     def _test_error(self, fn):
         self.p._strict = True
         with self.assertRaises(ValueError):
@@ -236,6 +243,18 @@ class TestLDIFParser(unittest.TestCase):
         attr_type, attr_value = self.p._parse_attr(b'foo:< ' + URL + b'\n')
         self.assertEqual(attr_value, '')
 
+    def test_parse_attr_dn_non_utf8(self):
+        def run():
+            attr = (
+                b'dn: \x75\x69\x64\x3d\x6b\x6f\xb3\x6f\x62'
+                b'\x69\x7a\x6e\x65\x73\x75\x40\x77\n'
+            )
+            attr_type, attr_value = self.p._parse_attr(attr)
+            self.assertEqual(attr_type, 'dn')
+            self.assertEqual(attr_value, 'uid=koobiznesu at w')
+
+        self._test_error(run)
+
     def test_parse(self):
         items = list(self.p.parse())
         for i, item in enumerate(items):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/ldif3.git



More information about the Python-modules-commits mailing list