Commits

Andriy Kornatskyy  committed 9466bda

shrink_uuid returns a native string;parse_uuid argument is native string

  • Participants
  • Parent commits 5062a03

Comments (0)

Files changed (2)

File src/wheezy/core/comp.py

 
 
 if PY3:  # pragma: nocover
-    b = lambda s: s.encode('latin1')
+
+    def ntob(n, encoding):
+        """ Converts native string to bytes
+        """
+        return n.encode(encoding)
+
+    def bton(b, encoding):
+        """ Converts bytes to native string
+        """
+        return b.decode(encoding)
+
     u = lambda s: s
 
-    def n(s):
-        if isinstance(s, bytes):
-            return s.decode('latin1')
-        else:
-            return s
+else:  # pragma: nocover
 
-else:  # pragma: nocover
-    b = lambda s: s
+    def ntob(n, encoding):
+        """ Converts native string to bytes
+        """
+        return n
+
+    def bton(b, encoding):
+        """ Converts bytes to native string
+        """
+        return b
+
     u = lambda s: unicode(s, "unicode_escape")
 
-    def n(s):
-        if isinstance(s, unicode):
-            return s.encode('latin1')
-        else:
-            return s
-
 
 if PY2 and PY_MINOR == 4:  # pragma: nocover
     __import__ = __import__

File src/wheezy/core/uuid.py

 from base64 import b64encode
 from binascii import Error
 
-from wheezy.core.comp import b
+from wheezy.core.comp import ntob
+from wheezy.core.comp import bton
 from wheezy.core.introspection import import_name
 
 
-BASE64_ALTCHARS = b('-_')
-BASE64_SUFFIX = b('==')
+BASE64_ALTCHARS = ntob('-_', 'latin1')
+BASE64_SUFFIX = ntob('==', 'latin1')
 UUID = import_name('uuid.UUID')
 UUID_EMPTY = UUID('00000000-0000-0000-0000-000000000000')
 
 
 def shrink_uuid(uuid):
     """
-        >>> from wheezy.core.comp import n
-        >>> n(shrink_uuid(UUID('a4af2f54-e988-4f5c-bfd6-351c79299b74')))
+        >>> shrink_uuid(UUID('a4af2f54-e988-4f5c-bfd6-351c79299b74'))
         'pK8vVOmIT1y_1jUceSmbdA'
-        >>> n(shrink_uuid(UUID('d17aba88-19c3-400e-adee-3ecf935db272')))
+        >>> shrink_uuid(UUID('d17aba88-19c3-400e-adee-3ecf935db272'))
         '0Xq6iBnDQA6t7j7Pk12ycg'
-        >>> n(shrink_uuid(UUID('39ae13ee-202a-42d1-9117-6fb6fdd169a4')))
+        >>> shrink_uuid(UUID('39ae13ee-202a-42d1-9117-6fb6fdd169a4'))
         'Oa4T7iAqQtGRF2-2_dFppA'
-        >>> n(shrink_uuid(UUID_EMPTY))
+        >>> shrink_uuid(UUID_EMPTY)
         'AAAAAAAAAAAAAAAAAAAAAA'
     """
     assert isinstance(uuid, UUID)
-    return b64encode(uuid.bytes, BASE64_ALTCHARS)[:22]
+    return bton(b64encode(uuid.bytes, BASE64_ALTCHARS)[:22], 'latin1')
 
 
 def parse_uuid(s):
     """
-        >>> from wheezy.core.comp import n
-        >>> n(parse_uuid(b('pK8vVOmIT1y_1jUceSmbdA')))
+        >>> parse_uuid('pK8vVOmIT1y_1jUceSmbdA')
         UUID('a4af2f54-e988-4f5c-bfd6-351c79299b74')
-        >>> n(parse_uuid(b('0Xq6iBnDQA6t7j7Pk12ycg')))
+        >>> parse_uuid('0Xq6iBnDQA6t7j7Pk12ycg')
         UUID('d17aba88-19c3-400e-adee-3ecf935db272')
-        >>> n(parse_uuid(b('Oa4T7iAqQtGRF2-2_dFppA')))
+        >>> parse_uuid('Oa4T7iAqQtGRF2-2_dFppA')
         UUID('39ae13ee-202a-42d1-9117-6fb6fdd169a4')
-        >>> n(parse_uuid(b('AAAAAAAAAAAAAAAAAAAAAA')))
+        >>> parse_uuid('AAAAAAAAAAAAAAAAAAAAAA')
         UUID('00000000-0000-0000-0000-000000000000')
 
         Return an empty uuid in case the string is empty of length
         not equal to 22.
 
-        >>> n(parse_uuid(b('')))
+        >>> parse_uuid('')
         UUID('00000000-0000-0000-0000-000000000000')
-        >>> n(parse_uuid(b('x')))
+        >>> parse_uuid('x')
         UUID('00000000-0000-0000-0000-000000000000')
 
         Incorrect base64 padding.
 
-        >>> n(parse_uuid(b('AAAAAAAAAA*AAAAAAAAAAA')))
+        >>> parse_uuid('AAAAAAAAAA*AAAAAAAAAAA')
         UUID('00000000-0000-0000-0000-000000000000')
     """
     if not s or len(s) != 22:
         return UUID_EMPTY
     try:
-        raw = b64decode(s + BASE64_SUFFIX, BASE64_ALTCHARS)
+        raw = b64decode(ntob(s, 'latin1') + BASE64_SUFFIX, BASE64_ALTCHARS)
     except (TypeError, Error):  # Incorrect padding
         return UUID_EMPTY
     assert len(raw) == 16