diff options
author | Dwayne Litzenberger <dlitz@dlitz.net> | 2014-06-22 20:17:45 -0700 |
---|---|---|
committer | Dwayne Litzenberger <dlitz@dlitz.net> | 2014-06-22 20:17:45 -0700 |
commit | f070adf984c6bb3ef72a3bf3d05648ea304eaa61 (patch) | |
tree | 3ca2aa7a4be3bc10a7b70385445ebf7e970d8298 | |
parent | 9e171b794f102f5745076051202997df9eac254a (diff) | |
download | pycrypto-f070adf984c6bb3ef72a3bf3d05648ea304eaa61.tar.gz |
Fix tobytes() broken by previous commit.
Python 2.1 str objects don't have a .decode() method.
-rw-r--r-- | lib/Crypto/Util/py3compat.py | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/lib/Crypto/Util/py3compat.py b/lib/Crypto/Util/py3compat.py index 1ab2206..7aa2bee 100644 --- a/lib/Crypto/Util/py3compat.py +++ b/lib/Crypto/Util/py3compat.py @@ -63,6 +63,8 @@ __revision__ = "$Id$" import sys if sys.version_info[0] == 2: + from types import UnicodeType as _UnicodeType # In Python 2.1, 'unicode' is a function, not a type. + def b(s): return s def bchr(s): @@ -71,22 +73,13 @@ if sys.version_info[0] == 2: return str(s) def bord(s): return ord(s) - if sys.version_info[1] == 1: - def tobytes(s): - try: - return s.encode('latin-1') - except UnicodeError: - return ''.join(s) - def tostr(bs): - return unicode(bs, 'latin-1') - else: - def tobytes(s): - if isinstance(s, unicode): - return s.encode("latin-1") - else: - return ''.join(s) - def tostr(bs): - return bs.decode('latin-1') + def tobytes(s): + if isinstance(s, _UnicodeType): + return s.encode("latin-1") + else: + return ''.join(s) + def tostr(bs): + return unicode(bs, 'latin-1') # In Pyton 2.x, StringIO is a stand-alone module from StringIO import StringIO as BytesIO else: |