summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2012-03-10 18:40:14 -0500
committerEli Collins <elic@assurancetechnologies.com>2012-03-10 18:40:14 -0500
commit29468d26fa3c68a56bd739d4dd9be50d8dd6725c (patch)
treee628b2bf9c62cfe493c241c846c5f74fa2e39cb6
parent2ec09c9b2efc3b5e63467151b12d24196ddf582d (diff)
downloadpasslib-29468d26fa3c68a56bd739d4dd9be50d8dd6725c.tar.gz
to_bytes, to_unicode, to_native_str: added special TypeErrors for None, other small tweaks
-rw-r--r--passlib/tests/test_utils.py2
-rw-r--r--passlib/utils/__init__.py24
2 files changed, 18 insertions, 8 deletions
diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py
index 734479b..eedb7e0 100644
--- a/passlib/tests/test_utils.py
+++ b/passlib/tests/test_utils.py
@@ -360,7 +360,7 @@ class CodecTest(TestCase):
b('\x00\xc3\xbf'))
#check bytes transcoding
- self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1", "utf-8"),
+ self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1", "", "utf-8"),
b('\x00\xff'))
#check other
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index 323b98f..b763898 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -270,14 +270,14 @@ def consteq(left, right):
# validate types
if isinstance(left, unicode):
if not isinstance(right, unicode):
- raise TypeError("inputs must be both unicode or bytes")
+ raise TypeError("inputs must be both unicode or both bytes")
is_py3_bytes = False
elif isinstance(left, bytes):
if not isinstance(right, bytes):
- raise TypeError("inputs must be both unicode or bytes")
+ raise TypeError("inputs must be both unicode or both bytes")
is_py3_bytes = PY3
else:
- raise TypeError("inputs must be both unicode or bytes")
+ raise TypeError("inputs must be both unicode or both bytes")
# do size comparison.
# NOTE: the double-if construction below is done deliberately, to ensure
@@ -496,7 +496,7 @@ def is_ascii_safe(source):
r = _B80 if isinstance(source, bytes) else _U80
return all(c < r for c in source)
-def to_bytes(source, encoding="utf-8", source_encoding=None, errname="value"):
+def to_bytes(source, encoding="utf-8", errname="value", source_encoding=None):
"""helper to normalize input to bytes.
:arg source:
@@ -505,12 +505,14 @@ def to_bytes(source, encoding="utf-8", source_encoding=None, errname="value"):
:arg encoding:
Target encoding (defaults to ``"utf-8"``).
- :param source_encoding:
- Source encoding (if known), used for transcoding.
-
:param errname:
Optional name of variable/noun to reference when raising errors
+ :param source_encoding:
+ If this is specified, and the source is bytes,
+ the source will be transcoded from *source_encoding* to *encoding*
+ (via unicode).
+
:raises TypeError: if source is not unicode or bytes.
:returns:
@@ -527,6 +529,8 @@ def to_bytes(source, encoding="utf-8", source_encoding=None, errname="value"):
return source
elif isinstance(source, unicode):
return source.encode(encoding)
+ elif source is None:
+ raise TypeError("no %s specified" % (errname,))
else:
raise TypeError("%s must be unicode or bytes, not %s" % (errname,
type(source)))
@@ -553,6 +557,8 @@ def to_unicode(source, source_encoding="utf-8", errname="value"):
return source
elif isinstance(source, bytes):
return source.decode(source_encoding)
+ elif source is None:
+ raise TypeError("no %s specified" % (errname,))
else:
raise TypeError("%s must be unicode or bytes, not %s" % (errname,
type(source)))
@@ -563,6 +569,8 @@ if PY3:
return source.decode(encoding)
elif isinstance(source, unicode):
return source
+ elif source is None:
+ raise TypeError("no %s specified" % (errname,))
else:
raise TypeError("%s must be unicode or bytes, not %s" %
(errname, type(source)))
@@ -572,6 +580,8 @@ else:
return source
elif isinstance(source, unicode):
return source.encode(encoding)
+ elif source is None:
+ raise TypeError("no %s specified" % (errname,))
else:
raise TypeError("%s must be unicode or bytes, not %s" %
(errname, type(source)))