diff options
-rw-r--r-- | paramiko/common.py | 8 | ||||
-rw-r--r-- | paramiko/file.py | 6 | ||||
-rw-r--r-- | paramiko/py3compat.py | 146 | ||||
-rw-r--r-- | paramiko/util.py | 4 | ||||
-rwxr-xr-x | test.py | 8 | ||||
-rw-r--r-- | tests/test_pkey.py | 2 | ||||
-rwxr-xr-x | tests/test_sftp.py | 4 |
7 files changed, 96 insertions, 82 deletions
diff --git a/paramiko/common.py b/paramiko/common.py index 223aac1a..e30df73a 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -131,12 +131,12 @@ cr_byte = byte_chr(13) linefeed_byte = byte_chr(10) crlf = cr_byte + linefeed_byte -if PY3: - cr_byte_value = 13 - linefeed_byte_value = 10 -else: +if PY2: cr_byte_value = cr_byte linefeed_byte_value = linefeed_byte +else: + cr_byte_value = 13 + linefeed_byte_value = 10 def asbytes(s): diff --git a/paramiko/file.py b/paramiko/file.py index a0d94ef2..c9f191a4 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -92,8 +92,8 @@ class BufferedFile (object): self._wbuffer = BytesIO() return - if PY3: - def __next__(self): + if PY2: + def next(self): """ Returns the next line from the input, or raises L{StopIteration} when EOF is hit. Unlike python file objects, it's okay to mix calls to @@ -109,7 +109,7 @@ class BufferedFile (object): raise StopIteration return line else: - def next(self): + def __next__(self): """ Returns the next line from the input, or raises L{StopIteration} when EOF is hit. Unlike python file objects, it's okay to mix calls to diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index 0aad3618..22285992 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,76 +1,13 @@ import sys import base64 -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', +__all__ = ['PY2', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', 'decodebytes', 'encodebytes', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] -PY3 = sys.version_info[0] >= 3 +PY2 = sys.version_info[0] < 3 -if PY3: - import collections - import struct - string_types = str - text_type = str - bytes = bytes - bytes_types = bytes - integer_types = int - class long(int): - pass - input = input - decodebytes = base64.decodebytes - encodebytes = base64.encodebytes - - def bytestring(s): - return s - - def byte_ord(c): - assert isinstance(c, int) - return c - - def byte_chr(c): - assert isinstance(c, int) - return struct.pack('B', c) - - def byte_mask(c, mask): - assert isinstance(c, int) - return struct.pack('B', c & mask) - - def b(s, encoding='utf8'): - """cast unicode or bytes to bytes""" - if isinstance(s, bytes): - return s - elif isinstance(s, str): - return s.encode(encoding) - else: - raise TypeError("Expected unicode or bytes, got %r" % s) - - def u(s, encoding='utf8'): - """cast bytes or unicode to unicode""" - if isinstance(s, bytes): - return s.decode(encoding) - elif isinstance(s, str): - return s - else: - raise TypeError("Expected unicode or bytes, got %r" % s) - - def b2s(s): - return s.decode() if isinstance(s, bytes) else s - - import io - StringIO = io.StringIO # NOQA - BytesIO = io.BytesIO # NOQA - - def is_callable(c): - return isinstance(c, collections.Callable) - - def get_next(c): - return c.__next__ - - next = next - - MAXSIZE = sys.maxsize # NOQA -else: +if PY2: string_types = basestring text_type = unicode bytes_types = str @@ -81,17 +18,21 @@ else: decodebytes = base64.decodestring encodebytes = base64.encodestring + def bytestring(s): # NOQA if isinstance(s, unicode): return s.encode('utf-8') return s + byte_ord = ord # NOQA byte_chr = chr # NOQA + def byte_mask(c, mask): return chr(ord(c) & mask) + def b(s, encoding='utf8'): # NOQA """cast unicode or bytes to bytes""" if isinstance(s, str): @@ -101,6 +42,7 @@ else: else: raise TypeError("Expected unicode or bytes, got %r" % s) + def u(s, encoding='utf8'): # NOQA """cast bytes or unicode to unicode""" if isinstance(s, str): @@ -110,24 +52,31 @@ else: else: raise TypeError("Expected unicode or bytes, got %r" % s) + def b2s(s): return s + try: import cStringIO + StringIO = cStringIO.StringIO # NOQA except ImportError: import StringIO + StringIO = StringIO.StringIO # NOQA BytesIO = StringIO + def is_callable(c): # NOQA return callable(c) + def get_next(c): # NOQA return c.next + def next(c): return c.next() @@ -135,6 +84,8 @@ else: class X(object): def __len__(self): return 1 << 31 + + try: len(X()) except OverflowError: @@ -144,3 +95,66 @@ else: # 64-bit MAXSIZE = int((1 << 63) - 1) # NOQA del X +else: + import collections + import struct + string_types = str + text_type = str + bytes = bytes + bytes_types = bytes + integer_types = int + class long(int): + pass + input = input + decodebytes = base64.decodebytes + encodebytes = base64.encodebytes + + def bytestring(s): + return s + + def byte_ord(c): + assert isinstance(c, int) + return c + + def byte_chr(c): + assert isinstance(c, int) + return struct.pack('B', c) + + def byte_mask(c, mask): + assert isinstance(c, int) + return struct.pack('B', c & mask) + + def b(s, encoding='utf8'): + """cast unicode or bytes to bytes""" + if isinstance(s, bytes): + return s + elif isinstance(s, str): + return s.encode(encoding) + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + def u(s, encoding='utf8'): + """cast bytes or unicode to unicode""" + if isinstance(s, bytes): + return s.decode(encoding) + elif isinstance(s, str): + return s + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + def b2s(s): + return s.decode() if isinstance(s, bytes) else s + + import io + StringIO = io.StringIO # NOQA + BytesIO = io.BytesIO # NOQA + + def is_callable(c): + return isinstance(c, collections.Callable) + + def get_next(c): + return c.__next__ + + next = next + + MAXSIZE = sys.maxsize # NOQA diff --git a/paramiko/util.py b/paramiko/util.py index 51fa6d66..71fc4673 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -63,8 +63,8 @@ def inflate_long(s, always_positive=False): out -= (long(1) << (8 * len(s))) return out -deflate_zero = 0 if PY3 else zero_byte -deflate_ff = 0xff if PY3 else max_byte +deflate_zero = zero_byte if PY2 else 0 +deflate_ff = max_byte if PY2 else 0xff def deflate_long(n, add_sign_padding=True): "turns a long-int into a normalized byte string (adapted from Crypto.Util.number)" @@ -29,7 +29,7 @@ import unittest from optparse import OptionParser import paramiko import threading -from paramiko.py3compat import PY3 +from paramiko.py3compat import PY2 sys.path.append('tests') @@ -149,10 +149,10 @@ def main(): # TODO: make that not a problem, jeez for thread in threading.enumerate(): if thread is not threading.currentThread(): - if PY3: - thread._stop() - else: + if PY2: thread._Thread__stop() + else: + thread._stop() # Exit correctly if not result.wasSuccessful(): sys.exit(1) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index f8549468..19c5c698 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -23,7 +23,7 @@ Some unit tests for public/private key objects. from binascii import hexlify, unhexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng, StringIO, byte_chr, b, PY3, bytes +from paramiko.common import rng, StringIO, byte_chr, b, bytes from tests.util import test_path # from openssh's ssh-keygen diff --git a/tests/test_sftp.py b/tests/test_sftp.py index b84b3fd6..4a412582 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -72,8 +72,8 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True -unicode_folder = eval(compile(r"'\u00fcnic\u00f8de'" if PY3 else r"u'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) -utf8_folder = eval(compile(r"b'/\xc3\xbcnic\xc3\xb8\x64\x65'" if PY3 else r"'/\xc3\xbcnic\xc3\xb8\x64\x65'", 'test_sftp.py', 'eval')) +unicode_folder = eval(compile(r"u'\u00fcnic\u00f8de'" if PY2 else r"'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) +utf8_folder = eval(compile(r"'/\xc3\xbcnic\xc3\xb8\x64\x65'" if PY2 else r"b'/\xc3\xbcnic\xc3\xb8\x64\x65'", 'test_sftp.py', 'eval')) def get_sftp(): global sftp |