diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2011-12-06 17:49:43 -0500 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2011-12-06 17:49:43 -0500 |
| commit | 7645cfde280b228c537d19bcb4d4aab0fdde27ef (patch) | |
| tree | 916e74aace6e281a3f14b031d0f26c0528a12018 | |
| parent | f99c749bac68994be0b9fa3317735efca0924202 (diff) | |
| download | passlib-7645cfde280b228c537d19bcb4d4aab0fdde27ef.tar.gz | |
couple of minor tweaks - basic unittests pass 2.7 & 3.2
| -rw-r--r-- | passlib/apache.py | 2 | ||||
| -rw-r--r-- | passlib/context.py | 6 | ||||
| -rw-r--r-- | passlib/tests/test_ext_django.py | 3 | ||||
| -rw-r--r-- | passlib/tests/test_utils.py | 4 | ||||
| -rw-r--r-- | passlib/utils/compat.py | 27 | ||||
| -rw-r--r-- | passlib/utils/h64.py | 2 |
6 files changed, 29 insertions, 15 deletions
diff --git a/passlib/apache.py b/passlib/apache.py index 37b9024..f34d1d0 100644 --- a/passlib/apache.py +++ b/passlib/apache.py @@ -85,7 +85,7 @@ class _CommonFile(object): passwords will be loaded directly from this string, and any files will be ignored. """ - if instance(content, unicode): + if isinstance(content, unicode): content = content.encode(self.encoding or 'utf-8') self.mtime = 0 #XXX: replace this with iterator? diff --git a/passlib/context.py b/passlib/context.py index ea666e6..02b615c 100644 --- a/passlib/context.py +++ b/passlib/context.py @@ -180,7 +180,7 @@ class CryptPolicy(object): # so policy object's keys will be native str type (unicode). with open(path, "rt", encoding=encoding) as stream: return cls._from_stream(stream, section, path) - elif encoding == "utf-8": + elif encoding in ["utf-8", "ascii"]: # for python 2, provide utf-8 stream, # so policy object's keys will be native str type (utf-8 bytes) with open(path, "rb") as stream: @@ -189,7 +189,7 @@ class CryptPolicy(object): # for python 2, transcode to utf-8 stream, # so policy object's keys will be native str type (utf-8 bytes) with open(path, "rb") as fh: - stream = BytesIO(source.decode(encoding).encode("utf-8")) + stream = BytesIO(fh.read().decode(encoding).encode("utf-8")) return cls._from_stream(stream, section, path) @classmethod @@ -219,7 +219,7 @@ class CryptPolicy(object): "helper for from_string / from_path" p = SafeConfigParser() if PY_MIN_32: - # Py3.2 deprecated readfp + # python 3.2 deprecated readfp in favor of read_file p.read_file(stream, filename or "<???>") else: p.readfp(stream, filename or "<???>") diff --git a/passlib/tests/test_ext_django.py b/passlib/tests/test_ext_django.py index 7f7d6d6..801f03e 100644 --- a/passlib/tests/test_ext_django.py +++ b/passlib/tests/test_ext_django.py @@ -417,7 +417,8 @@ django_hash_tests = [ td.DjangoSaltedSha1Test, ] -default_hash_tests = django_hash_tests + [ td.Builtin_SHA512CryptTest ] +default_hash_tests = django_hash_tests + [ td.Builtin_SHA512CryptTest \ + or td.OsCrypt_SHA512CryptTest ] if has_django0: django_hash_tests.remove(td.DjangoDesCryptTest) diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py index f309523..26d6386 100644 --- a/passlib/tests/test_utils.py +++ b/passlib/tests/test_utils.py @@ -241,13 +241,13 @@ class CodecTest(TestCase): else: import __builtin__ as builtins self.assertIs(bytes, builtins.str) - + self.assertIs(native_str, builtins.str) self.assertIsInstance(b(''), bytes) self.assertIsInstance(b('\x00\xff'), bytes) if PY3: - self.assertEqual(b('\x00\xff').encode("latin-1"), "\x00\xff") + self.assertEqual(b('\x00\xff').decode("latin-1"), "\x00\xff") else: self.assertEqual(b('\x00\xff'), "\x00\xff") diff --git a/passlib/utils/compat.py b/passlib/utils/compat.py index f82f7d6..159aed4 100644 --- a/passlib/utils/compat.py +++ b/passlib/utils/compat.py @@ -3,10 +3,18 @@ # figure out what version we're running #============================================================================= import sys -PY3 = sys.version >= (3,0) -PY_MAX_25 = sys.version < (2,6) # py 2.5 or earlier -PY27 = sys.version[:2] == (2,7) # supports last 2.x release -PY_MIN_32 = sys.version >= (3,2) # py 3.2 or later +PY3 = sys.version_info >= (3,0) +PY_MAX_25 = sys.version_info < (2,6) # py 2.5 or earlier +PY27 = sys.version_info[:2] == (2,7) # supports last 2.x release +PY_MIN_32 = sys.version_info >= (3,2) # py 3.2 or later + +#============================================================================= +# common imports +#============================================================================= +if PY3: + import builtins +else: + import __builtin__ as builtins #============================================================================= # the default exported vars @@ -82,12 +90,14 @@ def is_mapping(obj): # non-exhaustive check, enough to distinguish from lists, etc return hasattr(obj, "items") -if (3,0) <= sys.version < (3,2): +if (3,0) <= sys.version_info < (3,2): # callable isn't dead, it's just resting from collections import Callable def callable(obj): return isinstance(obj, Callable) __all__.append("callable") +else: + callable = builtins.callable if PY3: int_types = (int,) @@ -104,6 +114,7 @@ if PY3: assert isinstance(s, str) return s.encode("latin-1") unicode = str + bytes = builtins.bytes __all__.append("unicode") # string_types = (unicode,) @@ -116,6 +127,9 @@ else: if PY_MAX_25: bytes = str __all__.append("bytes") + else: + bytes = builtins.bytes + unicode = builtins.unicode # string_types = (unicode, bytes) sb_types = (unicode, bytes) @@ -171,13 +185,12 @@ else: def _add_doc(obj, doc): """add docstring to an object""" - object.__doc__ = doc + obj.__doc__ = doc #============================================================================= # input/output #============================================================================= if PY3: - import builtins print_ = getattr(builtins, "print") else: def print_(*args, **kwds): diff --git a/passlib/utils/h64.py b/passlib/utils/h64.py index 0659732..cf889d3 100644 --- a/passlib/utils/h64.py +++ b/passlib/utils/h64.py @@ -42,7 +42,7 @@ _CHARIDX = dict((_encode_6bit(i),i) for i in irange(64)) _decode_6bit = _CHARIDX.__getitem__ # char -> int #for py3, enhance _CHARIDX to also support int value of bytes -if Py3: +if PY3: _CHARIDX.update((v,i) for i,v in enumerate(BCHARS)) #================================================================================= |
