diff options
author | Eli Collins <elic@assurancetechnologies.com> | 2012-01-18 17:51:32 -0500 |
---|---|---|
committer | Eli Collins <elic@assurancetechnologies.com> | 2012-01-18 17:51:32 -0500 |
commit | 666aa14bf15ed898472463d2ec890de0b8c60923 (patch) | |
tree | f4ad79fb817cdca19e64dbb1abfe86c1a2c8ffc1 /passlib/tests/test_utils.py | |
parent | b6d904be92c77bce061c78034145958401bba381 (diff) | |
download | passlib-666aa14bf15ed898472463d2ec890de0b8c60923.tar.gz |
import cleanups
* moved bytes compat functions from utils to utils.compat
(bord, bjoin, bjoin_ints, bjoin_elems, ujoin)
* renamed bord -> belem_ord for clarify
* a bunch of to_native_str() always use ascii, and
have fixed input types (always bytes or always unicode).
these don't need overhead of to_native_str(), so replaced
those calls with two new funcs: compat.bascii_to_str() /
compat.uascii_to_str()
* cleaned up a lot of imports from utils/utils.compat to
pull from correct module
* simplified the to_string() logic of a bunch of handlers
to reduce unicode<->byte transitions
Diffstat (limited to 'passlib/tests/test_utils.py')
-rw-r--r-- | passlib/tests/test_utils.py | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py index e04d65f..a4e021d 100644 --- a/passlib/tests/test_utils.py +++ b/passlib/tests/test_utils.py @@ -11,8 +11,8 @@ import warnings #site #pkg #module -from passlib.utils import bytes, b, to_native_str -from passlib.utils.compat import unicode, PY3, u +from passlib.utils.compat import b, bytes, bascii_to_str, irange, PY2, PY3, u, \ + unicode from passlib.tests.utils import TestCase, Params as ak, enable_option, catch_warnings def hb(source): @@ -387,30 +387,32 @@ class CodecTest(TestCase): "test to_native_str()" from passlib.utils import to_native_str - # ascii goes to native string w/o problems - self.assertEqual(to_native_str(u('abc')), 'abc') - self.assertEqual(to_native_str(b('abc')), 'abc') + # test plain ascii + self.assertEqual(to_native_str(u('abc', 'ascii')), 'abc') + self.assertEqual(to_native_str(b('abc', 'ascii')), 'abc') - # other types rejected - self.assertRaises(TypeError, to_native_str, None) - - # non-ascii unicode should throw error under py2, unless codec specified + # test invalid ascii if PY3: - self.assertEqual(to_native_str(u('\x00\xff')), '\x00\xff') + self.assertEqual(to_native_str(u('\xE0'), 'ascii'), '\xE0') + self.assertRaises(UnicodeDecodeError, to_native_str, b('\xC3\xA0'), + 'ascii') else: - self.assertEqual(to_native_str(u('\x00\xff')), '\x00\xc3\xbf') - self.assertRaises(UnicodeEncodeError, to_native_str, u('\x00\xff'), + self.assertRaises(UnicodeEncodeError, to_native_str, u('\xE0'), 'ascii') + self.assertEqual(to_native_str(b('\xC3\xA0'), 'ascii'), '\xC3\xA0') - # non-ascii bytes should throw error under py3, unless codec specified - if PY3: - self.assertRaises(UnicodeDecodeError, to_native_str, b('\x00\xff')) - else: - self.assertEqual(to_native_str(b('\x00\xff')), '\x00\xff') + # test latin-1 + self.assertEqual(to_native_str(u('\xE0'), 'latin-1'), '\xE0') + self.assertEqual(to_native_str(b('\xE0'), 'latin-1'), '\xE0') + + # test utf-8 + self.assertEqual(to_native_str(u('\xE0'), 'utf-8'), + '\xE0' if PY3 else '\xC3\xA0') + self.assertEqual(to_native_str(b('\xC3\xA0'), 'utf-8'), + '\xE0' if PY3 else '\xC3\xA0') - # latin-1 should work if explicitly chosen - self.assertEqual(to_native_str(u('\x00\xff'), 'latin-1'), '\x00\xff') - self.assertEqual(to_native_str(b('\x00\xff'), 'latin-1'), '\x00\xff') + # other types rejected + self.assertRaises(TypeError, to_native_str, None, 'ascii') def test_is_ascii_safe(self): "test is_ascii_safe()" @@ -894,7 +896,7 @@ class _MD4_Test(TestCase): "test md4 digest()" md4 = self.hash for input, hex in self.vectors: - out = to_native_str(hexlify(md4(input).digest())) + out = bascii_to_str(hexlify(md4(input).digest())) self.assertEqual(out, hex) def test_md4_copy(self): |