From 3ce336c88b7bfbfad03fab17bff8cb3c3a77176c Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 07:30:45 -0800 Subject: [PATCH] Change conditional from PY3 to PY2 to be better prepared for a possible Py4. --- paramiko/common.py | 8 +- paramiko/file.py | 6 +- paramiko/py3compat.py | 168 +++++++++++++++++++++++------------------- paramiko/util.py | 4 +- test.py | 8 +- tests/test_pkey.py | 2 +- tests/test_sftp.py | 4 +- 7 files changed, 107 insertions(+), 93 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 223aac1..e30df73 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 a0d94ef..c9f191a 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 0aad361..2228599 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,13 +1,101 @@ 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: +if PY2: + string_types = basestring + text_type = unicode + bytes_types = str + bytes = str + integer_types = (int, long) + long = long + input = raw_input + 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): + return s + elif isinstance(s, unicode): + return s.encode(encoding) + 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): + return s.decode(encoding) + elif isinstance(s, unicode): + return s + 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() + + # It's possible to have sizeof(long) != sizeof(Py_ssize_t). + class X(object): + def __len__(self): + return 1 << 31 + + + try: + len(X()) + except OverflowError: + # 32-bit + MAXSIZE = int((1 << 31) - 1) # NOQA + else: + # 64-bit + MAXSIZE = int((1 << 63) - 1) # NOQA + del X +else: import collections import struct string_types = str @@ -70,77 +158,3 @@ if PY3: next = next MAXSIZE = sys.maxsize # NOQA -else: - string_types = basestring - text_type = unicode - bytes_types = str - bytes = str - integer_types = (int, long) - long = long - input = raw_input - 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): - return s - elif isinstance(s, unicode): - return s.encode(encoding) - 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): - return s.decode(encoding) - elif isinstance(s, unicode): - return s - 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() - - # It's possible to have sizeof(long) != sizeof(Py_ssize_t). - class X(object): - def __len__(self): - return 1 << 31 - try: - len(X()) - except OverflowError: - # 32-bit - MAXSIZE = int((1 << 31) - 1) # NOQA - else: - # 64-bit - MAXSIZE = int((1 << 63) - 1) # NOQA - del X diff --git a/paramiko/util.py b/paramiko/util.py index 51fa6d6..71fc467 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)" diff --git a/test.py b/test.py index c8ac6bc..bd966d1 100755 --- a/test.py +++ b/test.py @@ -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 f854946..19c5c69 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 b84b3fd..4a41258 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