diff options
-rw-r--r-- | paramiko/buffered_pipe.py | 20 | ||||
-rw-r--r-- | paramiko/file.py | 45 | ||||
-rw-r--r-- | paramiko/rsakey.py | 14 | ||||
-rw-r--r-- | paramiko/sftp_attr.py | 12 | ||||
-rw-r--r-- | paramiko/transport.py | 34 | ||||
-rw-r--r-- | paramiko/util.py | 10 | ||||
-rw-r--r-- | tests/test_pkey.py | 7 | ||||
-rw-r--r-- | tests/test_sftp.py | 2 | ||||
-rw-r--r-- | tests/util.py | 11 |
9 files changed, 49 insertions, 106 deletions
diff --git a/paramiko/buffered_pipe.py b/paramiko/buffered_pipe.py index aa7c06ce..d825efdf 100644 --- a/paramiko/buffered_pipe.py +++ b/paramiko/buffered_pipe.py @@ -25,7 +25,7 @@ read operations are blocking and can have a timeout set. import array import threading import time -from paramiko.py3compat import PY2, b +from paramiko.py3compat import b class PipeTimeout(IOError): @@ -50,21 +50,11 @@ class BufferedPipe(object): self._buffer = array.array("B") self._closed = False - if PY2: + def _buffer_frombytes(self, data): + self._buffer.frombytes(data) - def _buffer_frombytes(self, data): - self._buffer.fromstring(data) - - def _buffer_tobytes(self, limit=None): - return self._buffer[:limit].tostring() - - else: - - def _buffer_frombytes(self, data): - self._buffer.frombytes(data) - - def _buffer_tobytes(self, limit=None): - return self._buffer[:limit].tobytes() + def _buffer_tobytes(self, limit=None): + return self._buffer[:limit].tobytes() def set_event(self, event): """ diff --git a/paramiko/file.py b/paramiko/file.py index 90f4a7b9..91579f60 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -22,7 +22,7 @@ from paramiko.common import ( linefeed_byte, cr_byte_value, ) -from paramiko.py3compat import BytesIO, PY2, u, bytes_types, text_type +from paramiko.py3compat import BytesIO, u, bytes_types, text_type from paramiko.util import ClosingContextManager @@ -93,39 +93,20 @@ class BufferedFile(ClosingContextManager): self._wbuffer = BytesIO() return - if PY2: - - def next(self): - """ - Returns the next line from the input, or raises - ``StopIteration`` when EOF is hit. Unlike Python file - objects, it's okay to mix calls to `next` and `readline`. - - :raises: ``StopIteration`` -- when the end of the file is reached. - - :returns: a line (`str`) read from the file. - """ - line = self.readline() - if not line: - raise StopIteration - return line - - else: - - def __next__(self): - """ - Returns the next line from the input, or raises ``StopIteration`` - when EOF is hit. Unlike python file objects, it's okay to mix - calls to `.next` and `.readline`. + def __next__(self): + """ + Returns the next line from the input, or raises ``StopIteration`` + when EOF is hit. Unlike python file objects, it's okay to mix + calls to `.next` and `.readline`. - :raises: ``StopIteration`` -- when the end of the file is reached. + :raises: ``StopIteration`` -- when the end of the file is reached. - :returns: a line (`str`) read from the file. - """ - line = self.readline() - if not line: - raise StopIteration - return line + :returns: a line (`str`) read from the file. + """ + line = self.readline() + if not line: + raise StopIteration + return line def readable(self): """ diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index a53daea4..9ea00e95 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -27,7 +27,6 @@ from cryptography.hazmat.primitives.asymmetric import rsa, padding from paramiko.message import Message from paramiko.pkey import PKey -from paramiko.py3compat import PY2 from paramiko.ssh_exception import SSHException @@ -98,16 +97,9 @@ class RSAKey(PKey): return m.asbytes() def __str__(self): - # NOTE: as per inane commentary in #853, this appears to be the least - # crummy way to get a representation that prints identical to Python - # 2's previous behavior, on both interpreters. - # TODO: replace with a nice clean fingerprint display or something - if PY2: - # Can't just return the .decode below for Py2 because stuff still - # tries stuffing it into ASCII for whatever godforsaken reason - return self.asbytes() - else: - return self.asbytes().decode("utf8", errors="ignore") + # NOTE: see #853 to explain some legacy behavior. + # TODO 4.0: replace with a nice clean fingerprint display or something + return self.asbytes().decode("utf8", errors="ignore") @property def _fields(self): diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index 28a196b1..b1e3664e 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -19,7 +19,7 @@ import stat import time from paramiko.common import x80000000, o700, o70, xffffffff -from paramiko.py3compat import long, PY2, strftime +from paramiko.py3compat import long, strftime class SFTPAttributes(object): @@ -169,7 +169,7 @@ class SFTPAttributes(object): out += "-xSs"[suid + (n & 1)] return out - def _as_text(self): + def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) @@ -237,10 +237,4 @@ class SFTPAttributes(object): ) def asbytes(self): - return self._as_text().encode("utf-8") - - if PY2: - __unicode__ = _as_text - __str__ = asbytes - else: - __str__ = _as_text + return str(self).encode() diff --git a/paramiko/transport.py b/paramiko/transport.py index 930ba4d4..23eda83b 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -101,7 +101,7 @@ from paramiko.kex_gss import KexGSSGex, KexGSSGroup1, KexGSSGroup14 from paramiko.message import Message from paramiko.packet import Packetizer, NeedRekeyException from paramiko.primes import ModulusPack -from paramiko.py3compat import string_types, long, b, input, PY2 +from paramiko.py3compat import string_types, long, b, input from paramiko.rsakey import RSAKey from paramiko.ecdsakey import ECDSAKey from paramiko.server import ServerInterface @@ -1845,25 +1845,19 @@ class Transport(threading.Thread, ClosingContextManager): def stop_thread(self): self.active = False self.packetizer.close() - if PY2: - # Original join logic; #520 doesn't appear commonly present under - # Python 2. - while self.is_alive() and self is not threading.current_thread(): - self.join(10) - else: - # Keep trying to join() our main thread, quickly, until: - # * We join()ed successfully (self.is_alive() == False) - # * Or it looks like we've hit issue #520 (socket.recv hitting some - # race condition preventing it from timing out correctly), wherein - # our socket and packetizer are both closed (but where we'd - # otherwise be sitting forever on that recv()). - while ( - self.is_alive() - and self is not threading.current_thread() - and not self.sock._closed - and not self.packetizer.closed - ): - self.join(0.1) + # Keep trying to join() our main thread, quickly, until: + # * We join()ed successfully (self.is_alive() == False) + # * Or it looks like we've hit issue #520 (socket.recv hitting some + # race condition preventing it from timing out correctly), wherein + # our socket and packetizer are both closed (but where we'd + # otherwise be sitting forever on that recv()). + while ( + self.is_alive() + and self is not threading.current_thread() + and not self.sock._closed + and not self.packetizer.closed + ): + self.join(0.1) # internals... diff --git a/paramiko/util.py b/paramiko/util.py index 096b74ff..c397a815 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -54,10 +54,6 @@ def inflate_long(s, always_positive=False): return out -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)""" @@ -69,9 +65,9 @@ def deflate_long(n, add_sign_padding=True): n >>= 32 # strip off leading zeros, FFs for i in enumerate(s): - if (n == 0) and (i[1] != deflate_zero): + if (n == 0) and (i[1] != 0): break - if (n == -1) and (i[1] != deflate_ff): + if (n == -1) and (i[1] != 0xff): break else: # degenerate case, n was either 0 or -1 @@ -286,7 +282,7 @@ def constant_time_bytes_eq(a, b): return False res = 0 # noinspection PyUnresolvedReferences - for i in (xrange if PY2 else range)(len(a)): # noqa: F821 + for i in range(len(a)): # noqa: F821 res |= byte_ord(a[i]) ^ byte_ord(b[i]) return res == 0 diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 45d1ee96..1ed021f9 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -37,7 +37,7 @@ from paramiko import ( util, SSHException, ) -from paramiko.py3compat import b, bytes, PY2 +from paramiko.py3compat import b, bytes from paramiko.common import o600, byte_chr from cryptography.exceptions import UnsupportedAlgorithm @@ -134,8 +134,7 @@ L4QLcT5aND0EHZLB2fAUDXiWIb2j4rg1mwPlBMiBXA== x1234 = b"\x01\x02\x03\x04" -TEST_KEY_BYTESTR_2 = "\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x81\x00\xd3\x8fV\xea\x07\x85\xa6k%\x8d<\x1f\xbc\x8dT\x98\xa5\x96$\xf3E#\xbe>\xbc\xd2\x93\x93\x87f\xceD\x18\xdb \x0c\xb3\xa1a\x96\xf8e#\xcc\xacS\x8a#\xefVlE\x83\x1epv\xc1o\x17M\xef\xdf\x89DUXL\xa6\x8b\xaa<\x06\x10\xd7\x93w\xec\xaf\xe2\xaf\x95\xd8\xfb\xd9\xbfw\xcb\x9f0)#y{\x10\x90\xaa\x85l\tPru\x8c\t\x19\xce\xa0\xf1\xd2\xdc\x8e/\x8b\xa8f\x9c0\xdey\x84\xd2F\xf7\xcbmm\x1f\x87" # noqa -TEST_KEY_BYTESTR_3 = "\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x00ӏV\x07k%<\x1fT$E#>ғfD\x18 \x0cae#̬S#VlE\x1epvo\x17M߉DUXL<\x06\x10דw\u2bd5ٿw˟0)#y{\x10l\tPru\t\x19Π\u070e/f0yFmm\x1f" # noqa +TEST_KEY_BYTESTR = "\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x00ӏV\x07k%<\x1fT$E#>ғfD\x18 \x0cae#̬S#VlE\x1epvo\x17M߉DUXL<\x06\x10דw\u2bd5ٿw˟0)#y{\x10l\tPru\t\x19Π\u070e/f0yFmm\x1f" # noqa class KeyTest(unittest.TestCase): @@ -571,7 +570,7 @@ class KeyTest(unittest.TestCase): def test_stringification(self): key = RSAKey.from_private_key_file(_support("test_rsa.key")) - comparable = TEST_KEY_BYTESTR_2 if PY2 else TEST_KEY_BYTESTR_3 + comparable = TEST_KEY_BYTESTR self.assertEqual(str(key), comparable) def test_ed25519(self): diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 98292d26..c273feaa 100644 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -87,7 +87,7 @@ decreased compared with chicken. # byte" NON_UTF8_DATA = b"\xC3\xC3" -unicode_folder = u"\u00fcnic\u00f8de" if PY2 else "\u00fcnic\u00f8de" +unicode_folder = "\u00fcnic\u00f8de" utf8_folder = b"/\xc3\xbcnic\xc3\xb8\x64\x65" diff --git a/tests/util.py b/tests/util.py index 3ec5d092..951b86ce 100644 --- a/tests/util.py +++ b/tests/util.py @@ -6,7 +6,7 @@ import unittest import pytest -from paramiko.py3compat import builtins, PY2 +from paramiko.py3compat import builtins from paramiko.ssh_gss import GSS_AUTH_AVAILABLE from cryptography.exceptions import UnsupportedAlgorithm, _Reasons @@ -142,12 +142,9 @@ def is_low_entropy(): the hash seed set to zero under Python 3. """ is_32bit = struct.calcsize("P") == 32 / 8 - if PY2: - return is_32bit - else: - # I don't see a way to tell internally if the hash seed was set this - # way, but env should be plenty sufficient, this is only for testing. - return is_32bit and os.environ.get("PYTHONHASHSEED", None) == "0" + # I don't see a way to tell internally if the hash seed was set this + # way, but env should be plenty sufficient, this is only for testing. + return is_32bit and os.environ.get("PYTHONHASHSEED", None) == "0" def sha1_signing_unsupported(): |