From 83f44878eaacce5ee2bab0aa7f03a36743fea044 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Fri, 27 Sep 2013 21:29:18 -0700 Subject: Fixed a typo in the license header of most files Conflicts: paramiko/proxy.py --- tests/stub_sftp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/stub_sftp.py') diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 7f1ecc7e..3021d816 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -7,7 +7,7 @@ # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # -# Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY +# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. -- cgit v1.2.3 From 66cfa97cce92b1d60383d178887b18dddb999fc1 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:19:30 -0700 Subject: Fix imports --- demos/demo.py | 5 ++++- paramiko/__init__.py | 48 ++++++++++++++++++++++----------------------- paramiko/_winapi.py | 6 +++++- paramiko/agent.py | 2 +- paramiko/ber.py | 3 ++- paramiko/file.py | 2 +- paramiko/hostkeys.py | 34 +++++++++++++++++++++++++++++--- paramiko/message.py | 2 +- paramiko/pipe.py | 1 + paramiko/primes.py | 1 + paramiko/proxy.py | 1 + paramiko/transport.py | 2 +- test.py | 26 ++++++++++++------------ tests/loop.py | 1 + tests/stub_sftp.py | 2 ++ tests/test_auth.py | 3 ++- tests/test_buffered_pipe.py | 2 +- tests/test_client.py | 3 ++- tests/test_kex.py | 1 + tests/test_message.py | 1 + tests/test_packetizer.py | 3 ++- tests/test_pkey.py | 3 +-- tests/test_sftp.py | 7 ++++--- tests/test_sftp_big.py | 7 ++++--- tests/test_transport.py | 4 ++-- tests/test_util.py | 4 ++-- tests/util.py | 1 + 27 files changed, 112 insertions(+), 63 deletions(-) (limited to 'tests/stub_sftp.py') diff --git a/demos/demo.py b/demos/demo.py index cbd7730e..3890eda7 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -30,7 +30,10 @@ import time import traceback import paramiko -import interactive +try: + import interactive +except ImportError: + from . import interactive def agent_auth(transport, username): diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 32ccfcdb..a12ee04c 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -62,32 +62,32 @@ __version_info__ = tuple([ int(d) for d in __version__.split(".") ]) __license__ = "GNU Lesser General Public License (LGPL)" -from transport import SecurityOptions, Transport -from client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy -from auth_handler import AuthHandler -from channel import Channel, ChannelFile -from ssh_exception import SSHException, PasswordRequiredException, \ +from paramiko.transport import SecurityOptions, Transport +from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy +from paramiko.auth_handler import AuthHandler +from paramiko.channel import Channel, ChannelFile +from paramiko.ssh_exception import SSHException, PasswordRequiredException, \ BadAuthenticationType, ChannelException, BadHostKeyException, \ AuthenticationException, ProxyCommandFailure -from server import ServerInterface, SubsystemHandler, InteractiveQuery -from rsakey import RSAKey -from dsskey import DSSKey -from ecdsakey import ECDSAKey -from sftp import SFTPError, BaseSFTP -from sftp_client import SFTP, SFTPClient -from sftp_server import SFTPServer -from sftp_attr import SFTPAttributes -from sftp_handle import SFTPHandle -from sftp_si import SFTPServerInterface -from sftp_file import SFTPFile -from message import Message -from packet import Packetizer -from file import BufferedFile -from agent import Agent, AgentKey -from pkey import PKey -from hostkeys import HostKeys -from config import SSHConfig -from proxy import ProxyCommand +from paramiko.server import ServerInterface, SubsystemHandler, InteractiveQuery +from paramiko.rsakey import RSAKey +from paramiko.dsskey import DSSKey +from paramiko.ecdsakey import ECDSAKey +from paramiko.sftp import SFTPError, BaseSFTP +from paramiko.sftp_client import SFTP, SFTPClient +from paramiko.sftp_server import SFTPServer +from paramiko.sftp_attr import SFTPAttributes +from paramiko.sftp_handle import SFTPHandle +from paramiko.sftp_si import SFTPServerInterface +from paramiko.sftp_file import SFTPFile +from paramiko.message import Message +from paramiko.packet import Packetizer +from paramiko.file import BufferedFile +from paramiko.agent import Agent, AgentKey +from paramiko.pkey import PKey +from paramiko.hostkeys import HostKeys +from paramiko.config import SSHConfig +from paramiko.proxy import ProxyCommand # fix module names for epydoc for c in locals().values(): diff --git a/paramiko/_winapi.py b/paramiko/_winapi.py index f141b005..43d97511 100644 --- a/paramiko/_winapi.py +++ b/paramiko/_winapi.py @@ -8,7 +8,11 @@ in jaraco.windows and asking the author to port the fixes back here. import ctypes import ctypes.wintypes -import __builtin__ +from paramiko.py3compat import u +try: + import builtins +except ImportError: + import __builtin__ as builtins ###################### # jaraco.windows.error diff --git a/paramiko/agent.py b/paramiko/agent.py index 23a5a2e4..67bb0671 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -34,7 +34,7 @@ from paramiko.ssh_exception import SSHException from paramiko.message import Message from paramiko.pkey import PKey from paramiko.channel import Channel -from paramiko.common import io_sleep +from paramiko.common import * from paramiko.util import retry_on_signal SSH2_AGENTC_REQUEST_IDENTITIES, SSH2_AGENT_IDENTITIES_ANSWER, \ diff --git a/paramiko/ber.py b/paramiko/ber.py index 3941581c..f3b4b37e 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -17,7 +17,8 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -import util +import paramiko.util as util +from paramiko.common import * class BERException (Exception): diff --git a/paramiko/file.py b/paramiko/file.py index 5fd81cfe..d1779130 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -20,7 +20,7 @@ BufferedFile. """ -from cStringIO import StringIO +from paramiko.common import * class BufferedFile (object): diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 9bcf0d55..c0e58b0e 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -23,7 +23,10 @@ L{HostKeys} import base64 import binascii from Crypto.Hash import SHA, HMAC -import UserDict +try: + from collections import MutableMapping +except ImportError: + from UserDict import DictMixin as MutableMapping from paramiko.common import * from paramiko.dsskey import DSSKey @@ -109,7 +112,7 @@ class HostKeyEntry: return '' % (self.hostnames, self.key) -class HostKeys (UserDict.DictMixin): +class HostKeys (MutableMapping): """ Representation of an openssh-style "known hosts" file. Host keys can be read from one or more files, and then individual hosts can be looked up to @@ -215,12 +218,26 @@ class HostKeys (UserDict.DictMixin): @return: keys associated with this host (or C{None}) @rtype: dict(str, L{PKey}) """ - class SubDict (UserDict.DictMixin): + class SubDict (MutableMapping): def __init__(self, hostname, entries, hostkeys): self._hostname = hostname self._entries = entries self._hostkeys = hostkeys + def __iter__(self): + for k in self.keys(): + yield k + + def __len__(self): + return len(self.keys()) + + def __delitem__(self, key): + for e in list(self._entries): + if e.key.get_name() == key: + self._entries.remove(e) + else: + raise KeyError(key) + def __getitem__(self, key): for e in self._entries: if e.key.get_name() == key: @@ -280,6 +297,17 @@ class HostKeys (UserDict.DictMixin): """ self._entries = [] + def __iter__(self): + for k in self.keys(): + yield k + + def __len__(self): + return len(self.keys()) + + def __delitem__(self, key): + k = self[key] + pass + def __getitem__(self, key): ret = self.lookup(key) if ret is None: diff --git a/paramiko/message.py b/paramiko/message.py index c0e8692b..d579a167 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -21,9 +21,9 @@ Implementation of an SSH2 "message". """ import struct -import cStringIO from paramiko import util +from paramiko.common import * class Message (object): diff --git a/paramiko/pipe.py b/paramiko/pipe.py index db43d549..e64547bd 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -27,6 +27,7 @@ will trigger as readable in select(). import sys import os import socket +from paramiko.py3compat import b def make_pipe (): diff --git a/paramiko/primes.py b/paramiko/primes.py index 9419cd6b..bf2b810c 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -24,6 +24,7 @@ from Crypto.Util import number from paramiko import util from paramiko.ssh_exception import SSHException +from paramiko.common import * def _generate_prime(bits, rng): diff --git a/paramiko/proxy.py b/paramiko/proxy.py index 218b76e2..a10feb01 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -21,6 +21,7 @@ L{ProxyCommand}. """ import os +import sys from shlex import split as shlsplit import signal from subprocess import Popen, PIPE diff --git a/paramiko/transport.py b/paramiko/transport.py index 3155d3f8..c6ab1272 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -79,7 +79,7 @@ class SecurityOptions (object): C{ValueError} will be raised. If you try to assign something besides a tuple to one of the fields, C{TypeError} will be raised. """ - __slots__ = [ 'ciphers', 'digests', 'key_types', 'kex', 'compression', '_transport' ] + #__slots__ = [ 'ciphers', 'digests', 'key_types', 'kex', 'compression', '_transport' ] def __init__(self, transport): self._transport = transport diff --git a/test.py b/test.py index 6702e53a..159794c5 100755 --- a/test.py +++ b/test.py @@ -32,19 +32,19 @@ import threading sys.path.append('tests') -from test_message import MessageTest -from test_file import BufferedFileTest -from test_buffered_pipe import BufferedPipeTest -from test_util import UtilTest -from test_hostkeys import HostKeysTest -from test_pkey import KeyTest -from test_kex import KexTest -from test_packetizer import PacketizerTest -from test_auth import AuthTest -from test_transport import TransportTest -from test_sftp import SFTPTest -from test_sftp_big import BigSFTPTest -from test_client import SSHClientTest +from tests.test_message import MessageTest +from tests.test_file import BufferedFileTest +from tests.test_buffered_pipe import BufferedPipeTest +from tests.test_util import UtilTest +from tests.test_hostkeys import HostKeysTest +from tests.test_pkey import KeyTest +from tests.test_kex import KexTest +from tests.test_packetizer import PacketizerTest +from tests.test_auth import AuthTest +from tests.test_transport import TransportTest +from tests.test_sftp import SFTPTest +from tests.test_sftp_big import BigSFTPTest +from tests.test_client import SSHClientTest default_host = 'localhost' default_user = os.environ.get('USER', 'nobody') diff --git a/tests/loop.py b/tests/loop.py index 91c216d2..2f3f5dfc 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -21,6 +21,7 @@ """ import threading, socket +from paramiko.py3compat import * class LoopSocket (object): diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 3021d816..e5f44543 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -21,8 +21,10 @@ A stub SFTP server for loopback SFTP testing. """ import os +import sys from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED +from paramiko.common import * class StubServer (ServerInterface): diff --git a/tests/test_auth.py b/tests/test_auth.py index 61fe63f4..1e247d70 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -29,7 +29,8 @@ from paramiko import Transport, ServerInterface, RSAKey, DSSKey, \ AuthenticationException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from loop import LoopSocket +from tests.loop import LoopSocket +from tests.util import test_path class NullServer (ServerInterface): diff --git a/tests/test_buffered_pipe.py b/tests/test_buffered_pipe.py index 47ece936..04d665c4 100644 --- a/tests/test_buffered_pipe.py +++ b/tests/test_buffered_pipe.py @@ -26,7 +26,7 @@ import unittest from paramiko.buffered_pipe import BufferedPipe, PipeTimeout from paramiko import pipe -from util import ParamikoTest +from tests.util import ParamikoTest def delay_thread(pipe): diff --git a/tests/test_client.py b/tests/test_client.py index e5352278..7d1e6729 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -20,13 +20,14 @@ Some unit tests for SSHClient. """ +import os import socket import threading import time import unittest import weakref from binascii import hexlify - +from tests.util import test_path import paramiko diff --git a/tests/test_kex.py b/tests/test_kex.py index 39d2e17e..be8d7f01 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -26,6 +26,7 @@ import paramiko.util from paramiko.kex_group1 import KexGroup1 from paramiko.kex_gex import KexGex from paramiko import Message +from paramiko.common import * class FakeRng (object): diff --git a/tests/test_message.py b/tests/test_message.py index ad622a27..d0e604e3 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -22,6 +22,7 @@ Some unit tests for ssh protocol message blocks. import unittest from paramiko.message import Message +from paramiko.common import * class MessageTest (unittest.TestCase): diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 1f5bec05..c39fc455 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -21,10 +21,11 @@ Some unit tests for the ssh2 protocol in Transport. """ import unittest -from loop import LoopSocket +from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC from paramiko import Message, Packetizer, util +from paramiko.py3compat import byte_chr class PacketizerTest (unittest.TestCase): diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 8e8c4aa7..fe823a77 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -21,10 +21,9 @@ Some unit tests for public/private key objects. """ from binascii import hexlify, unhexlify -import StringIO import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng +from paramiko.common import rng, StringIO, byte_chr # from openssh's ssh-keygen PUB_RSA = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA049W6geFpmsljTwfvI1UmKWWJPNFI74+vNKTk4dmzkQY2yAMs6FhlvhlI8ysU4oj71ZsRYMecHbBbxdN79+JRFVYTKaLqjwGENeTd+yv4q+V2PvZv3fLnzApI3l7EJCqhWwJUHJ1jAkZzqDx0tyOL4uoZpww3nmE0kb3y21tH4c=' diff --git a/tests/test_sftp.py b/tests/test_sftp.py index cc512c18..3c1fcd52 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -31,11 +31,12 @@ import warnings import sys import threading import unittest -import StringIO import paramiko -from stub_sftp import StubServer, StubSFTPServer -from loop import LoopSocket +from paramiko.common import * +from tests.stub_sftp import StubServer, StubSFTPServer +from tests.loop import LoopSocket +from tests.util import test_path from paramiko.sftp_attr import SFTPAttributes ARTICLE = ''' diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 04b15b0d..9a4ea311 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -33,9 +33,10 @@ import time import unittest import paramiko -from stub_sftp import StubServer, StubSFTPServer -from loop import LoopSocket -from test_sftp import get_sftp +from paramiko.common import * +from tests.stub_sftp import StubServer, StubSFTPServer +from tests.loop import LoopSocket +from tests.test_sftp import get_sftp FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') diff --git a/tests/test_transport.py b/tests/test_transport.py index e8f7f366..ed8ebb42 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -35,8 +35,8 @@ from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST from paramiko.message import Message -from loop import LoopSocket -from util import ParamikoTest +from tests.loop import LoopSocket +from tests.util import ParamikoTest, test_path LONG_BANNER = """\ diff --git a/tests/test_util.py b/tests/test_util.py index 12677a9b..12575f84 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -21,15 +21,15 @@ Some unit tests for utility functions. """ from binascii import hexlify -import cStringIO import errno import os import unittest from Crypto.Hash import SHA import paramiko.util from paramiko.util import lookup_ssh_host_config as host_config +from paramiko.py3compat import StringIO, byte_ord -from util import ParamikoTest +from tests.util import ParamikoTest test_config_file = """\ Host * diff --git a/tests/util.py b/tests/util.py index 2e0be087..1b380b75 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,3 +1,4 @@ +import os import unittest -- cgit v1.2.3 From 2ea352b8baf1037bd22fc79146ef646c163a7692 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:46:33 -0700 Subject: Fix dict iters, sorts, exceptions, bytes renames and tuple args --- paramiko/__init__.py | 2 +- paramiko/auth_handler.py | 3 +- paramiko/channel.py | 4 +- paramiko/client.py | 26 ++++++------- paramiko/config.py | 2 +- paramiko/dsskey.py | 4 +- paramiko/file.py | 6 +-- paramiko/hostkeys.py | 4 +- paramiko/kex_gex.py | 4 +- paramiko/packet.py | 9 +++-- paramiko/pkey.py | 4 +- paramiko/primes.py | 7 ++-- paramiko/proxy.py | 10 +++-- paramiko/server.py | 3 +- paramiko/sftp_attr.py | 4 +- paramiko/sftp_client.py | 8 ++-- paramiko/sftp_file.py | 6 +-- paramiko/sftp_handle.py | 6 ++- paramiko/sftp_server.py | 10 +++-- paramiko/transport.py | 96 ++++++++++++++++++++++++++---------------------- paramiko/util.py | 5 ++- tests/stub_sftp.py | 42 ++++++++++++++------- tests/test_transport.py | 14 ++++--- tests/test_util.py | 6 +-- 24 files changed, 160 insertions(+), 125 deletions(-) (limited to 'tests/stub_sftp.py') diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 86e0bc69..160ed393 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -90,7 +90,7 @@ from paramiko.config import SSHConfig from paramiko.proxy import ProxyCommand # fix module names for epydoc -for c in locals().values(): +for c in list(locals().values()): if issubclass(type(c), type) or type(c).__name__ == 'classobj': # classobj for exceptions :/ c.__module__ = __name__ diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index acb7c8b8..de0b2057 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -308,7 +308,8 @@ class AuthHandler (object): keyblob = m.get_string() try: key = self.transport._key_info[keytype](Message(keyblob)) - except SSHException, e: + except SSHException: + e = sys.exc_info()[1] self.transport._log(INFO, 'Auth rejected: public key: %s' % str(e)) key = None except: diff --git a/paramiko/channel.py b/paramiko/channel.py index d1e6333c..d3517680 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -615,7 +615,7 @@ class Channel (object): """ try: out = self.in_buffer.read(nbytes, self.timeout) - except PipeTimeout, e: + except PipeTimeout: raise socket.timeout() ack = self._check_add_window(len(out)) @@ -665,7 +665,7 @@ class Channel (object): """ try: out = self.in_stderr_buffer.read(nbytes, self.timeout) - except PipeTimeout, e: + except PipeTimeout: raise socket.timeout() ack = self._check_add_window(len(out)) diff --git a/paramiko/client.py b/paramiko/client.py index c5a2d1ac..98bb47f2 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -193,8 +193,8 @@ class SSHClient (object): self.load_host_keys(self.known_hosts) f = open(filename, 'w') - for hostname, keys in self._host_keys.iteritems(): - for keytype, key in keys.iteritems(): + for hostname, keys in self._host_keys.items(): + for keytype, key in keys.items(): f.write('%s %s %s\n' % (hostname, keytype, key.get_base64())) f.close() @@ -452,8 +452,8 @@ class SSHClient (object): two_factor = (allowed_types == ['password']) if not two_factor: return - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] if not two_factor: for key_filename in key_filenames: @@ -466,8 +466,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] if not two_factor and allow_agent: if self._agent == None: @@ -482,8 +482,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] if not two_factor: keyfiles = [] @@ -514,17 +514,15 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: - saved_exception = e - except IOError, e: - saved_exception = e + except (SSHException, IOError): + saved_exception = sys.exc_info()[1] if password is not None: try: self._transport.auth_password(username, password) return - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] elif two_factor: raise SSHException('Two-factor authentication requires a password') diff --git a/paramiko/config.py b/paramiko/config.py index 1705de76..51415be1 100644 --- a/paramiko/config.py +++ b/paramiko/config.py @@ -173,7 +173,7 @@ class SSHConfig (object): ret = {} for match in matches: - for key, value in match['config'].iteritems(): + for key, value in match['config'].items(): if key not in ret: # Create a copy of the original value, # else it will reference the original list diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index f6ecb2a7..ff744888 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -184,8 +184,8 @@ class DSSKey (PKey): # DSAPrivateKey = { version = 0, p, q, g, y, x } try: keylist = BER(data).decode() - except BERException, x: - raise SSHException('Unable to parse key file: ' + str(x)) + except BERException: + raise SSHException('Unable to parse key file: ' + str(sys.exc_info()[1])) if (type(keylist) is not list) or (len(keylist) < 6) or (keylist[0] != 0): raise SSHException('not a valid DSA private key file (bad ber encoding)') self.p = keylist[1] diff --git a/paramiko/file.py b/paramiko/file.py index d1779130..1a8d3ee1 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -254,14 +254,14 @@ class BufferedFile (object): @rtype: list """ lines = [] - bytes = 0 + byte_count = 0 while True: line = self.readline() if len(line) == 0: break lines.append(line) - bytes += len(line) - if (sizehint is not None) and (bytes >= sizehint): + byte_count += len(line) + if (sizehint is not None) and (byte_count >= sizehint): break return lines diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index c0e58b0e..f548263c 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -91,8 +91,8 @@ class HostKeyEntry: log.info("Unable to handle key of type %s" % (keytype,)) return None - except binascii.Error, e: - raise InvalidHostKey(line, e) + except binascii.Error: + raise InvalidHostKey(line, sys.exc_info()[1]) return cls(names, key) from_line = classmethod(from_line) diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index c0455a1f..669bd34e 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -95,14 +95,14 @@ class KexGex (object): q = (self.p - 1) // 2 qnorm = util.deflate_long(q, 0) qhbyte = ord(qnorm[0]) - bytes = len(qnorm) + byte_count = len(qnorm) qmask = 0xff while not (qhbyte & 0x80): qhbyte <<= 1 qmask >>= 1 while True: - x_bytes = self.transport.rng.read(bytes) x_bytes = chr(ord(x_bytes[0]) & qmask) + x_bytes[1:] + x_bytes = self.transport.rng.read(byte_count) x = util.inflate_long(x_bytes, 1) if (x > 1) and (x < q): break diff --git a/paramiko/packet.py b/paramiko/packet.py index 6ab7363d..3a26b6bc 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -214,7 +214,8 @@ class Packetizer (object): n -= len(x) except socket.timeout: got_timeout = True - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. @@ -243,7 +244,8 @@ class Packetizer (object): n = self.__socket.send(out) except socket.timeout: retry_write = True - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): retry_write = True elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): @@ -468,7 +470,8 @@ class Packetizer (object): break except socket.timeout: pass - except EnvironmentError, e: + except EnvironmentError: + e = sys.exc_info()[1] if ((type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR)): pass diff --git a/paramiko/pkey.py b/paramiko/pkey.py index b1199df8..d61809f1 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -304,8 +304,8 @@ class PKey (object): # if we trudged to the end of the file, just try to cope. try: data = base64.decodestring(''.join(lines[start:end])) - except base64.binascii.Error, e: - raise SSHException('base64 decoding error: ' + str(e)) + except base64.binascii.Error: + raise SSHException('base64 decoding error: ' + str(sys.exc_info()[1])) if 'proc-type' not in headers: # unencryped: done return data diff --git a/paramiko/primes.py b/paramiko/primes.py index bf2b810c..1dd87daf 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -47,7 +47,7 @@ def _generate_prime(bits, rng): def _roll_random(rng, n): "returns a random # from 0 to N-1" bits = util.bit_length(n-1) - bytes = (bits + 7) // 8 + byte_count = (bits + 7) // 8 hbyte_mask = pow(2, bits % 8) - 1 # so here's the plan: @@ -57,7 +57,7 @@ def _roll_random(rng, n): # fits, so i can't guarantee that this loop will ever finish, but the odds # of it looping forever should be infinitesimal. while True: - x = rng.read(bytes) + x = rng.read(byte_count) if hbyte_mask > 0: x = chr(ord(x[0]) & hbyte_mask) + x[1:] num = util.inflate_long(x, 1) @@ -125,8 +125,7 @@ class ModulusPack (object): f.close() def get_modulus(self, min, prefer, max): - bitsizes = self.pack.keys() - bitsizes.sort() + bitsizes = sorted(self.pack.keys(), key=hash) if len(bitsizes) == 0: raise SSHException('no moduli available') good = -1 diff --git a/paramiko/proxy.py b/paramiko/proxy.py index a10feb01..43c46665 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -60,12 +60,13 @@ class ProxyCommand(object): """ try: self.process.stdin.write(content) - except IOError, e: + except IOError: + e = sys.exc_info()[1] # There was a problem with the child process. It probably # died and we can't proceed. The best option here is to # raise an exception informing the user that the informed # ProxyCommand is not working. - raise BadProxyCommand(' '.join(self.cmd), e.strerror) + raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) return len(content) def recv(self, size): @@ -80,8 +81,9 @@ class ProxyCommand(object): """ try: return os.read(self.process.stdout.fileno(), size) - except IOError, e: - raise BadProxyCommand(' '.join(self.cmd), e.strerror) + except IOError: + e = sys.exc_info()[1] + raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) def close(self): os.kill(self.process.pid, signal.SIGTERM) diff --git a/paramiko/server.py b/paramiko/server.py index fdb40942..4b6e8b18 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -623,7 +623,8 @@ class SubsystemHandler (threading.Thread): try: self.__transport._log(DEBUG, 'Starting handler for subsystem %s' % self.__name) self.start_subsystem(self.__name, self.__transport, self.__channel) - except Exception, e: + except Exception: + e = sys.exc_info()[1] self.__transport._log(ERROR, 'Exception in subsystem handler for "%s": %s' % (self.__name, str(e))) self.__transport._log(ERROR, util.tb_strings()) diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index b459b04b..84c83929 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -143,7 +143,7 @@ class SFTPAttributes (object): msg.add_int(long(self.st_mtime)) if self._flags & self.FLAG_EXTENDED: msg.add_int(len(self.attr)) - for key, val in self.attr.iteritems(): + for key, val in self.attr.items(): msg.add_string(key) msg.add_string(val) return @@ -158,7 +158,7 @@ class SFTPAttributes (object): out += 'mode=' + oct(self.st_mode) + ' ' if (self.st_atime is not None) and (self.st_mtime is not None): out += 'atime=%d mtime=%d ' % (self.st_atime, self.st_mtime) - for k, v in self.attr.iteritems(): + for k, v in self.attr.items(): out += '"%s"=%r ' % (str(k), v) out += ']' return out diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index d9215743..954cfa0d 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -85,7 +85,7 @@ class SFTPClient (BaseSFTP): self.ultra_debug = transport.get_hexdump() try: server_version = self._send_version() - except EOFError, x: + except EOFError: raise SSHException('EOF during negotiation') self._log(INFO, 'Opened sftp connection (server version %d)' % server_version) @@ -178,7 +178,7 @@ class SFTPClient (BaseSFTP): while True: try: t, msg = self._request(CMD_READDIR, handle) - except EOFError, e: + except EOFError: # done with handle break if t != CMD_NAME: @@ -717,8 +717,8 @@ class SFTPClient (BaseSFTP): while True: try: t, data = self._read_packet() - except EOFError, e: - raise SSHException('Server connection dropped: %s' % (str(e),)) + except EOFError: + raise SSHException('Server connection dropped: %s' % str(sys.exc_info()[1])) msg = Message(data) num = msg.get_int() if num not in self._expecting: diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 4ec936d0..c0bc898f 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -94,7 +94,7 @@ class SFTPFile (BufferedFile): k = [i for i in self._prefetch_reads if i[0] <= offset] if len(k) == 0: return False - k.sort(lambda x, y: cmp(x[0], y[0])) + k.sort(key=hash) buf_offset, buf_size = k[-1] if buf_offset + buf_size <= offset: # prefetch request ends before this one begins @@ -464,8 +464,8 @@ class SFTPFile (BufferedFile): # save exception and re-raise it on next file operation try: self.sftp._convert_status(msg) - except Exception, x: - self._saved_exception = x + except Exception: + self._saved_exception = sys.exc_info()[1] return if t != CMD_DATA: raise SFTPError('Expected data') diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index 29d3d0d8..8b78063b 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -100,7 +100,8 @@ class SFTPHandle (object): readfile.seek(offset) self.__tell = offset data = readfile.read(length) - except IOError, e: + except IOError: + e = sys.exc_info()[1] self.__tell = None return SFTPServer.convert_errno(e.errno) self.__tell += len(data) @@ -139,7 +140,8 @@ class SFTPHandle (object): self.__tell = offset writefile.write(data) writefile.flush() - except IOError, e: + except IOError: + e = sys.exc_info()[1] self.__tell = None return SFTPServer.convert_errno(e.errno) if self.__tell is not None: diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 1833c2e8..ec340089 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -92,7 +92,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): except EOFError: self._log(DEBUG, 'EOF -- end of session') return - except Exception, e: + except Exception: + e = sys.exc_info()[1] self._log(DEBUG, 'Exception on channel: ' + str(e)) self._log(DEBUG, util.tb_strings()) return @@ -100,7 +101,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): request_number = msg.get_int() try: self._process(t, request_number, msg) - except Exception, e: + except Exception: + e = sys.exc_info()[1] self._log(DEBUG, 'Exception in server processing: ' + str(e)) self._log(DEBUG, util.tb_strings()) # send some kind of failure message, at least @@ -113,9 +115,9 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self.server.session_ended() super(SFTPServer, self).finish_subsystem() # close any file handles that were left open (so we can return them to the OS quickly) - for f in self.file_table.itervalues(): + for f in self.file_table.values(): f.close() - for f in self.folder_table.itervalues(): + for f in self.folder_table.values(): f.close() self.file_table = {} self.folder_table = {} diff --git a/paramiko/transport.py b/paramiko/transport.py index c6ab1272..c8a693ed 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -177,7 +177,7 @@ class ChannelMap (object): def values(self): self._lock.acquire() try: - return self._map.values() + return list(self._map.values()) finally: self._lock.release() @@ -294,8 +294,8 @@ class Transport (threading.Thread): sock = socket.socket(af, socket.SOCK_STREAM) try: retry_on_signal(lambda: sock.connect((hostname, port))) - except socket.error, e: - reason = str(e) + except socket.error: + reason = str(sys.exc_info()[1]) else: break else: @@ -616,7 +616,7 @@ class Transport (threading.Thread): if not self.active: return self.stop_thread() - for chan in self._channels.values(): + for chan in list(self._channels.values()): chan._unlink() self.sock.close() @@ -691,7 +691,7 @@ class Transport (threading.Thread): """ return self.open_channel('auth-agent@openssh.com') - def open_forwarded_tcpip_channel(self, (src_addr, src_port), (dest_addr, dest_port)): + def open_forwarded_tcpip_channel(self, src_addr_port, dest_addr_port): """ Request a new channel back to the client, of type C{"forwarded-tcpip"}. This is used after a client has requested port forwarding, for sending @@ -702,6 +702,8 @@ class Transport (threading.Thread): @param dest_addr: local (server) connected address @param dest_port: local (server) connected port """ + src_addr, src_port = src_addr_port + dest_addr, dest_port = dest_addr_port return self.open_channel('forwarded-tcpip', (dest_addr, dest_port), (src_addr, src_port)) def open_channel(self, kind, dest_addr=None, src_addr=None): @@ -811,7 +813,9 @@ class Transport (threading.Thread): if port == 0: port = response.get_int() if handler is None: - def default_handler(channel, (src_addr, src_port), (dest_addr, dest_port)): + def default_handler(channel, src_addr_port, dest_addr_port): + #src_addr, src_port = src_addr_port + #dest_addr, dest_port = dest_addr_port self._queue_incoming_channel(channel) handler = default_handler self._tcp_handler = handler @@ -845,22 +849,22 @@ class Transport (threading.Thread): """ return SFTPClient.from_transport(self) - def send_ignore(self, bytes=None): + def send_ignore(self, byte_count=None): """ Send a junk packet across the encrypted link. This is sometimes used to add "noise" to a connection to confuse would-be attackers. It can also be used as a keep-alive for long lived connections traversing firewalls. - @param bytes: the number of random bytes to send in the payload of the + @param byte_count: the number of random bytes to send in the payload of the ignored packet -- defaults to a random number from 10 to 41. - @type bytes: int + @type byte_count: int """ m = Message() m.add_byte(chr(MSG_IGNORE)) - if bytes is None: - bytes = (ord(rng.read(1)) % 32) + 10 - m.add_bytes(rng.read(bytes)) + if byte_count is None: + byte_count = (byte_ord(rng.read(1)) % 32) + 10 + m.add_bytes(rng.read(byte_count)) self._send_user_message(m) def renegotiate_keys(self): @@ -1181,7 +1185,8 @@ class Transport (threading.Thread): return [] try: return self.auth_handler.wait_for_response(my_event) - except BadAuthenticationType, x: + except BadAuthenticationType: + x = sys.exc_info()[1] # if password auth isn't allowed, but keyboard-interactive *is*, try to fudge it if not fallback or ('keyboard-interactive' not in x.allowed_types): raise @@ -1197,7 +1202,7 @@ class Transport (threading.Thread): return [] return [ password ] return self.auth_interactive(username, handler) - except SSHException, ignored: + except SSHException: # attempt failed; just raise the original exception raise x return None @@ -1513,7 +1518,7 @@ class Transport (threading.Thread): # only called if a channel has turned on x11 forwarding if handler is None: # by default, use the same mechanism as accept() - def default_handler(channel, (src_addr, src_port)): + def default_handler(channel, src_addr_port): self._queue_incoming_channel(channel) self._x11_handler = default_handler else: @@ -1604,22 +1609,26 @@ class Transport (threading.Thread): msg.add_byte(chr(MSG_UNIMPLEMENTED)) msg.add_int(m.seqno) self._send_message(msg) - except SSHException, e: + except SSHException: + e = sys.exc_info()[1] self._log(ERROR, 'Exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e - except EOFError, e: + except EOFError: + e = sys.exc_info()[1] self._log(DEBUG, 'EOF in transport thread') #self._log(DEBUG, util.tb_strings()) self.saved_exception = e - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] if type(e.args) is tuple: emsg = '%s (%d)' % (e.args[1], e.args[0]) else: emsg = e.args self._log(ERROR, 'Socket exception: ' + emsg) self.saved_exception = e - except Exception, e: + except Exception: + e = sys.exc_info()[1] self._log(ERROR, 'Unknown exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e @@ -1679,7 +1688,8 @@ class Transport (threading.Thread): buf = self.packetizer.readline(timeout) except ProxyCommandFailure: raise - except Exception, x: + except Exception: + x = sys.exc_info()[1] raise SSHException('Error reading SSH protocol banner' + str(x)) if buf[:4] == 'SSH-': break @@ -1690,7 +1700,7 @@ class Transport (threading.Thread): self.remote_version = buf # pull off any attached comment comment = '' - i = string.find(buf, ' ') + i = buf.find(' ') if i >= 0: comment = buf[i+1:] buf = buf[:i] @@ -1774,19 +1784,19 @@ class Transport (threading.Thread): # as a server, we pick the first item in the client's list that we support. # as a client, we pick the first item in our list that the server supports. if self.server_mode: - agreed_kex = filter(self._preferred_kex.__contains__, kex_algo_list) + agreed_kex = list(filter(self._preferred_kex.__contains__, kex_algo_list)) else: - agreed_kex = filter(kex_algo_list.__contains__, self._preferred_kex) + agreed_kex = list(filter(kex_algo_list.__contains__, self._preferred_kex)) if len(agreed_kex) == 0: raise SSHException('Incompatible ssh peer (no acceptable kex algorithm)') self.kex_engine = self._kex_info[agreed_kex[0]](self) if self.server_mode: - available_server_keys = filter(self.server_key_dict.keys().__contains__, - self._preferred_keys) - agreed_keys = filter(available_server_keys.__contains__, server_key_algo_list) + available_server_keys = list(filter(list(self.server_key_dict.keys()).__contains__, + self._preferred_keys)) + agreed_keys = list(filter(available_server_keys.__contains__, server_key_algo_list)) else: - agreed_keys = filter(server_key_algo_list.__contains__, self._preferred_keys) + agreed_keys = list(filter(server_key_algo_list.__contains__, self._preferred_keys)) if len(agreed_keys) == 0: raise SSHException('Incompatible ssh peer (no acceptable host key)') self.host_key_type = agreed_keys[0] @@ -1794,15 +1804,15 @@ class Transport (threading.Thread): raise SSHException('Incompatible ssh peer (can\'t match requested host key type)') if self.server_mode: - agreed_local_ciphers = filter(self._preferred_ciphers.__contains__, - server_encrypt_algo_list) - agreed_remote_ciphers = filter(self._preferred_ciphers.__contains__, - client_encrypt_algo_list) + agreed_local_ciphers = list(filter(self._preferred_ciphers.__contains__, + server_encrypt_algo_list)) + agreed_remote_ciphers = list(filter(self._preferred_ciphers.__contains__, + client_encrypt_algo_list)) else: - agreed_local_ciphers = filter(client_encrypt_algo_list.__contains__, - self._preferred_ciphers) - agreed_remote_ciphers = filter(server_encrypt_algo_list.__contains__, - self._preferred_ciphers) + agreed_local_ciphers = list(filter(client_encrypt_algo_list.__contains__, + self._preferred_ciphers)) + agreed_remote_ciphers = list(filter(server_encrypt_algo_list.__contains__, + self._preferred_ciphers)) if (len(agreed_local_ciphers) == 0) or (len(agreed_remote_ciphers) == 0): raise SSHException('Incompatible ssh server (no acceptable ciphers)') self.local_cipher = agreed_local_ciphers[0] @@ -1810,22 +1820,22 @@ class Transport (threading.Thread): self._log(DEBUG, 'Ciphers agreed: local=%s, remote=%s' % (self.local_cipher, self.remote_cipher)) if self.server_mode: - agreed_remote_macs = filter(self._preferred_macs.__contains__, client_mac_algo_list) - agreed_local_macs = filter(self._preferred_macs.__contains__, server_mac_algo_list) + agreed_remote_macs = list(filter(self._preferred_macs.__contains__, client_mac_algo_list)) + agreed_local_macs = list(filter(self._preferred_macs.__contains__, server_mac_algo_list)) else: - agreed_local_macs = filter(client_mac_algo_list.__contains__, self._preferred_macs) - agreed_remote_macs = filter(server_mac_algo_list.__contains__, self._preferred_macs) + agreed_local_macs = list(filter(client_mac_algo_list.__contains__, self._preferred_macs)) + agreed_remote_macs = list(filter(server_mac_algo_list.__contains__, self._preferred_macs)) if (len(agreed_local_macs) == 0) or (len(agreed_remote_macs) == 0): raise SSHException('Incompatible ssh server (no acceptable macs)') self.local_mac = agreed_local_macs[0] self.remote_mac = agreed_remote_macs[0] if self.server_mode: - agreed_remote_compression = filter(self._preferred_compression.__contains__, client_compress_algo_list) - agreed_local_compression = filter(self._preferred_compression.__contains__, server_compress_algo_list) + agreed_remote_compression = list(filter(self._preferred_compression.__contains__, client_compress_algo_list)) + agreed_local_compression = list(filter(self._preferred_compression.__contains__, server_compress_algo_list)) else: - agreed_local_compression = filter(client_compress_algo_list.__contains__, self._preferred_compression) - agreed_remote_compression = filter(server_compress_algo_list.__contains__, self._preferred_compression) + agreed_local_compression = list(filter(client_compress_algo_list.__contains__, self._preferred_compression)) + agreed_remote_compression = list(filter(server_compress_algo_list.__contains__, self._preferred_compression)) if (len(agreed_local_compression) == 0) or (len(agreed_remote_compression) == 0): raise SSHException('Incompatible ssh server (no acceptable compression) %r %r %r' % (agreed_local_compression, agreed_remote_compression, self._preferred_compression)) self.local_compression = agreed_local_compression[0] diff --git a/paramiko/util.py b/paramiko/util.py index 85ee6b06..299f44a6 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -276,13 +276,14 @@ def retry_on_signal(function): while True: try: return function() - except EnvironmentError, e: + except EnvironmentError: + e = sys.exc_info()[1] if e.errno != errno.EINTR: raise class Counter (object): """Stateful counter for CTR mode crypto""" - def __init__(self, nbits, initial_value=1L, overflow=0L): + def __init__(self, nbits, initial_value=long_one, overflow=long_zero): self.blocksize = nbits / 8 self.overflow = overflow # start with value - 1 so we don't have to store intermediate values when counting diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index e5f44543..b8bea9b5 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -40,7 +40,8 @@ class StubSFTPHandle (SFTPHandle): def stat(self): try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def chattr(self, attr): @@ -49,7 +50,8 @@ class StubSFTPHandle (SFTPHandle): try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) @@ -71,21 +73,24 @@ class StubSFTPServer (SFTPServerInterface): attr.filename = fname out.append(attr) return out - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def stat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.stat(path)) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def lstat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def open(self, path, flags, attr): @@ -100,7 +105,8 @@ class StubSFTPServer (SFTPServerInterface): # os.open() defaults to 0777 which is # an odd default mode for files fd = os.open(path, flags, 0666) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) if (flags & os.O_CREAT) and (attr is not None): attr._flags &= ~attr.FLAG_PERMISSIONS @@ -120,7 +126,8 @@ class StubSFTPServer (SFTPServerInterface): fstr = 'rb' try: f = os.fdopen(fd, fstr) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) fobj = StubSFTPHandle(flags) fobj.filename = path @@ -132,7 +139,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.remove(path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -141,7 +149,8 @@ class StubSFTPServer (SFTPServerInterface): newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -151,7 +160,8 @@ class StubSFTPServer (SFTPServerInterface): os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -159,7 +169,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.rmdir(path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -167,7 +178,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -187,7 +199,8 @@ class StubSFTPServer (SFTPServerInterface): target_path = '' try: os.symlink(target_path, path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -195,7 +208,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: symlink = os.readlink(path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) # if it's absolute, remove the root if os.path.isabs(symlink): diff --git a/tests/test_transport.py b/tests/test_transport.py index ccd3f0c8..d8cd10d5 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -249,7 +249,7 @@ class TransportTest(ParamikoTest): try: chan.exec_command('no') self.assert_(False) - except SSHException, x: + except SSHException: pass chan = self.tc.open_session() @@ -302,7 +302,8 @@ class TransportTest(ParamikoTest): try: chan = self.tc.open_channel('bogus') self.fail('expected exception') - except ChannelException, x: + except ChannelException: + x = sys.exc_info()[1] self.assert_(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) def test_9_exit_status(self): @@ -444,7 +445,8 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) requested = [] - def handler(c, (addr, port)): + def handler(c, addr_port): + addr, port = addr_port requested.append((addr, port)) self.tc._queue_incoming_channel(c) @@ -479,9 +481,9 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) requested = [] - def handler(c, (origin_addr, origin_port), (server_addr, server_port)): - requested.append((origin_addr, origin_port)) - requested.append((server_addr, server_port)) + def handler(c, origin_addr_port, server_addr_port): + requested.append(origin_addr_port) + requested.append(server_addr_port) self.tc._queue_incoming_channel(c) port = self.tc.request_port_forward('127.0.0.1', 0, handler) diff --git a/tests/test_util.py b/tests/test_util.py index 12575f84..7e656df8 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -65,7 +65,7 @@ class UtilTest(ParamikoTest): """ verify that all the classes can be imported from paramiko. """ - symbols = globals().keys() + symbols = list(globals().keys()) self.assertTrue('Transport' in symbols) self.assertTrue('SSHClient' in symbols) self.assertTrue('MissingHostKeyPolicy' in symbols) @@ -148,8 +148,8 @@ class UtilTest(ParamikoTest): try: hostdict = paramiko.util.load_host_keys('hostfile.temp') self.assertEquals(2, len(hostdict)) - self.assertEquals(1, len(hostdict.values()[0])) - self.assertEquals(1, len(hostdict.values()[1])) + self.assertEquals(1, len(list(hostdict.values())[0])) + self.assertEquals(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() self.assertEquals('E6684DB30E109B67B70FF1DC5C7F1363', fp) finally: -- cgit v1.2.3 From 0b7d0cf0a23e4f16f8552ae05a66539119e2e920 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:14:52 -0700 Subject: Convert and detect types properly, use helper constants, use StringIO and range --- paramiko/ber.py | 12 +++++----- paramiko/channel.py | 2 +- paramiko/client.py | 4 ++-- paramiko/kex_gex.py | 6 ++--- paramiko/kex_group1.py | 11 +++++---- paramiko/packet.py | 31 +++++++++++++------------ paramiko/pipe.py | 4 ++-- paramiko/pkey.py | 4 ++-- paramiko/primes.py | 4 ++-- paramiko/server.py | 2 +- paramiko/sftp.py | 4 ++-- paramiko/sftp_attr.py | 8 +++---- paramiko/sftp_client.py | 4 ++-- paramiko/sftp_server.py | 4 ++-- paramiko/transport.py | 8 +++---- paramiko/util.py | 61 ++++++++++++++++++++++--------------------------- tests/loop.py | 4 ++-- tests/stub_sftp.py | 2 +- tests/test_auth.py | 9 ++++++-- tests/test_kex.py | 6 ++--- tests/test_pkey.py | 6 ++--- tests/test_sftp.py | 45 +++++++++++++++++++++--------------- tests/test_sftp_big.py | 32 +++++++++++++------------- tests/test_transport.py | 6 ++--- tests/test_util.py | 20 ++++++++-------- 25 files changed, 154 insertions(+), 145 deletions(-) (limited to 'tests/stub_sftp.py') diff --git a/paramiko/ber.py b/paramiko/ber.py index 45372fc4..f4d2acc3 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -49,13 +49,13 @@ class BER(object): def decode_next(self): if self.idx >= len(self.content): return None - ident = ord(self.content[self.idx]) + ident = byte_ord(self.content[self.idx]) self.idx += 1 if (ident & 31) == 31: # identifier > 30 ident = 0 while self.idx < len(self.content): - t = ord(self.content[self.idx]) + t = byte_ord(self.content[self.idx]) self.idx += 1 ident = (ident << 7) | (t & 0x7f) if not (t & 0x80): @@ -63,7 +63,7 @@ class BER(object): if self.idx >= len(self.content): return None # now fetch length - size = ord(self.content[self.idx]) + size = byte_ord(self.content[self.idx]) self.idx += 1 if size & 0x80: # more complimicated... @@ -102,12 +102,12 @@ class BER(object): def encode_tlv(self, ident, val): # no need to support ident > 31 here - self.content += chr(ident) + self.content += byte_chr(ident) if len(val) > 0x7f: lenstr = util.deflate_long(len(val)) - self.content += chr(0x80 + len(lenstr)) + lenstr + self.content += byte_chr(0x80 + len(lenstr)) + lenstr else: - self.content += chr(len(val)) + self.content += byte_chr(len(val)) self.content += val def encode(self, x): diff --git a/paramiko/channel.py b/paramiko/channel.py index 422986dd..d686c6d6 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -969,7 +969,7 @@ class Channel (object): self.transport._send_user_message(m) def _feed(self, m): - if type(m) is str: + if isinstance(m, bytes_type): # passed from _feed_extended s = m else: diff --git a/paramiko/client.py b/paramiko/client.py index 98bb47f2..3881f8d4 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -335,7 +335,7 @@ class SSHClient (object): if key_filename is None: key_filenames = [] - elif isinstance(key_filename, (str, unicode)): + elif isinstance(key_filename, string_types): key_filenames = [ key_filename ] else: key_filenames = key_filename @@ -383,7 +383,7 @@ class SSHClient (object): return stdin, stdout, stderr def invoke_shell(self, term='vt100', width=80, height=24, width_pixels=0, - height_pixels=0): + height_pixels=0): """ Start an interactive shell session on the SSH server. A new L{Channel} is opened and connected to a pseudo-terminal using the requested diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index d2ef15ca..02494539 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -91,20 +91,20 @@ class KexGex (object): ### internals... - + def _generate_x(self): # generate an "x" (1 < x < (p-1)/2). q = (self.p - 1) // 2 qnorm = util.deflate_long(q, 0) - qhbyte = ord(qnorm[0]) + qhbyte = byte_ord(qnorm[0]) byte_count = len(qnorm) qmask = 0xff while not (qhbyte & 0x80): qhbyte <<= 1 qmask >>= 1 while True: - x_bytes = chr(ord(x_bytes[0]) & qmask) + x_bytes[1:] x_bytes = self.transport.rng.read(byte_count) + x_bytes = byte_mask(x_bytes[0], qmask) + x_bytes[1:] x = util.inflate_long(x_bytes, 1) if (x > 1) and (x < q): break diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py index 83fb87de..ea452b34 100644 --- a/paramiko/kex_group1.py +++ b/paramiko/kex_group1.py @@ -30,9 +30,10 @@ from paramiko.ssh_exception import SSHException _MSG_KEXDH_INIT, _MSG_KEXDH_REPLY = range(30, 32) +c_MSG_KEXDH_INIT, c_MSG_KEXDH_REPLY = [byte_chr(c) for c in range(30, 32)] # draft-ietf-secsh-transport-09.txt, page 17 -P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL +P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF G = 2 @@ -42,9 +43,9 @@ class KexGroup1(object): def __init__(self, transport): self.transport = transport - self.x = 0L - self.e = 0L - self.f = 0L + self.x = long_zero + self.e = long_zero + self.f = long_zero def start_kex(self): self._generate_x() @@ -80,7 +81,7 @@ class KexGroup1(object): # larger than q (but this is a tiny tiny subset of potential x). while 1: x_bytes = self.transport.rng.read(128) - x_bytes = chr(ord(x_bytes[0]) & 0x7f) + x_bytes[1:] + x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] if (x_bytes[:8] != '\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF') and \ (x_bytes[:8] != '\x00\x00\x00\x00\x00\x00\x00\x00'): break diff --git a/paramiko/packet.py b/paramiko/packet.py index 3a26b6bc..193acf69 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -38,6 +38,7 @@ try: except ImportError: from Crypto.Hash.HMAC import HMAC + def compute_hmac(key, message, digest_class): return HMAC(key, message, digest_class).digest() @@ -66,7 +67,7 @@ class Packetizer (object): self.__dump_packets = False self.__need_rekey = False self.__init_count = 0 - self.__remainder = '' + self.__remainder = bytes() # used for noticing when to re-key: self.__sent_bytes = 0 @@ -90,8 +91,8 @@ class Packetizer (object): self.__mac_key_in = '' self.__compress_engine_out = None self.__compress_engine_in = None - self.__sequence_number_out = 0L - self.__sequence_number_in = 0L + self.__sequence_number_out = long_zero + self.__sequence_number_in = long_zero # lock around outbound writes (packet computation) self.__write_lock = threading.RLock() @@ -196,7 +197,7 @@ class Packetizer (object): @raise EOFError: if the socket was closed before all the bytes could be read """ - out = '' + out = bytes() # handle over-reading from reading the banner line if len(self.__remainder) > 0: out = self.__remainder[:n] @@ -275,22 +276,22 @@ class Packetizer (object): line, so it's okay to attempt large reads. """ buf = self.__remainder - while not '\n' in buf: + while not newline_byte in buf: buf += self._read_timeout(timeout) - n = buf.index('\n') + n = buf.index(newline_byte) self.__remainder = buf[n+1:] buf = buf[:n] - if (len(buf) > 0) and (buf[-1] == '\r'): + if (len(buf) > 0) and (buf[-1] == cr_byte): buf = buf[:-1] - return buf + return u(buf) def send_message(self, data): """ Write a block of data using the current cipher, as an SSH block. """ # encrypt this sucka - data = str(data) - cmd = ord(data[0]) + data = asbytes(data) + cmd = byte_ord(data[0]) if cmd in MSG_NAMES: cmd_name = MSG_NAMES[cmd] else: @@ -312,7 +313,7 @@ class Packetizer (object): if self.__block_engine_out != None: payload = struct.pack('>I', self.__sequence_number_out) + packet out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out] - self.__sequence_number_out = (self.__sequence_number_out + 1) & 0xffffffffL + self.__sequence_number_out = (self.__sequence_number_out + 1) & xffffffff self.write_all(out) self.__sent_bytes += len(out) @@ -361,7 +362,7 @@ class Packetizer (object): my_mac = compute_hmac(self.__mac_key_in, mac_payload, self.__mac_engine_in)[:self.__mac_size_in] if my_mac != mac: raise SSHException('Mismatched MAC') - padding = ord(packet[0]) + padding = byte_ord(packet[0]) payload = packet[1:packet_size - padding] if self.__dump_packets: @@ -372,7 +373,7 @@ class Packetizer (object): msg = Message(payload[1:]) msg.seqno = self.__sequence_number_in - self.__sequence_number_in = (self.__sequence_number_in + 1) & 0xffffffffL + self.__sequence_number_in = (self.__sequence_number_in + 1) & xffffffff # check for rekey raw_packet_size = packet_size + self.__mac_size_in + 4 @@ -395,7 +396,7 @@ class Packetizer (object): self.__received_packets_overflow = 0 self._trigger_rekey() - cmd = ord(payload[0]) + cmd = byte_ord(payload[0]) if cmd in MSG_NAMES: cmd_name = MSG_NAMES[cmd] else: @@ -493,7 +494,7 @@ class Packetizer (object): if self.__sdctr_out or self.__block_engine_out is None: # cute trick i caught openssh doing: if we're not encrypting or SDCTR mode (RFC4344), # don't waste random bytes for the padding - packet += (chr(0) * padding) + packet += (zero_byte * padding) else: packet += rng.read(padding) return packet diff --git a/paramiko/pipe.py b/paramiko/pipe.py index e64547bd..4c965465 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -64,7 +64,7 @@ class PosixPipe (object): if self._set or self._closed: return self._set = True - os.write(self._wfd, '*') + os.write(self._wfd, b('*')) def set_forever (self): self._forever = True @@ -110,7 +110,7 @@ class WindowsPipe (object): if self._set or self._closed: return self._set = True - self._wsock.send('*') + self._wsock.send(b('*')) def set_forever (self): self._forever = True diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 9c59dad4..c88e5c85 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -352,9 +352,9 @@ class PKey (object): @raise IOError: if there was an error writing the file. """ - f = open(filename, 'w', 0600) + f = open(filename, 'w', o600) # grrr... the mode doesn't always take hold - os.chmod(filename, 0600) + os.chmod(filename, o600) self._write_private_key(tag, f, data, password) f.close() diff --git a/paramiko/primes.py b/paramiko/primes.py index 1dd87daf..144454aa 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -34,7 +34,7 @@ def _generate_prime(bits, rng): # loop catches the case where we increment n into a higher bit-range x = rng.read((bits+7) // 8) if hbyte_mask > 0: - x = chr(ord(x[0]) & hbyte_mask) + x[1:] + x = byte_mask(x[0], hbyte_mask) + x[1:] n = util.inflate_long(x, 1) n |= 1 n |= (1 << (bits - 1)) @@ -59,7 +59,7 @@ def _roll_random(rng, n): while True: x = rng.read(byte_count) if hbyte_mask > 0: - x = chr(ord(x[0]) & hbyte_mask) + x[1:] + x = byte_mask(x[0], hbyte_mask) + x[1:] num = util.inflate_long(x, 1) if num < n: break diff --git a/paramiko/server.py b/paramiko/server.py index 4b6e8b18..f3383add 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -48,7 +48,7 @@ class InteractiveQuery (object): self.instructions = instructions self.prompts = [] for x in prompts: - if (type(x) is str) or (type(x) is unicode): + if isinstance(x, string_types): self.add_prompt(x) else: self.add_prompt(x[0], x[1]) diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 0d1287a5..11aa3cb4 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -166,8 +166,8 @@ class BaseSFTP (object): def _send_packet(self, t, packet): #self._log(DEBUG2, 'write: %s (len=%d)' % (CMD_NAMES.get(t, '0x%02x' % t), len(packet))) - out = struct.pack('>I', len(packet) + 1) + chr(t) + packet packet = asbytes(packet) + out = struct.pack('>I', len(packet) + 1) + byte_chr(t) + packet if self.ultra_debug: self._log(DEBUG, util.format_binary(out, 'OUT: ')) self._write_all(out) @@ -183,7 +183,7 @@ class BaseSFTP (object): if self.ultra_debug: self._log(DEBUG, util.format_binary(data, 'IN: ')); if size > 0: - t = ord(data[0]) + t = byte_ord(data[0]) #self._log(DEBUG2, 'read: %s (len=%d)' % (CMD_NAMES.get(t), '0x%02x' % t, len(data)-1)) return t, data[1:] return 0, '' diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index 84c83929..5a40f4c5 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -44,7 +44,7 @@ class SFTPAttributes (object): FLAG_UIDGID = 2 FLAG_PERMISSIONS = 4 FLAG_AMTIME = 8 - FLAG_EXTENDED = 0x80000000L + FLAG_EXTENDED = x80000000 def __init__(self): """ @@ -194,13 +194,13 @@ class SFTPAttributes (object): ks = 's' else: ks = '?' - ks += self._rwx((self.st_mode & 0700) >> 6, self.st_mode & stat.S_ISUID) - ks += self._rwx((self.st_mode & 070) >> 3, self.st_mode & stat.S_ISGID) + ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) + ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date - if (self.st_mtime is None) or (self.st_mtime == 0xffffffffL): + if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 0e3d6c81..7703246a 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -285,7 +285,7 @@ class SFTPClient (BaseSFTP): self._log(DEBUG, 'rename(%r, %r)' % (oldpath, newpath)) self._request(CMD_RENAME, oldpath, newpath) - def mkdir(self, path, mode=0777): + def mkdir(self, path, mode=o777): """ Create a folder (directory) named C{path} with numeric mode C{mode}. The default mode is 0777 (octal). On some systems, mode is ignored. @@ -698,7 +698,7 @@ class SFTPClient (BaseSFTP): msg.add_int(item) elif isinstance(item, long): msg.add_int64(item) - elif isinstance(item, str): + elif isinstance(item, string_types): msg.add_string(item) elif isinstance(item, SFTPAttributes): item._pack(msg) diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 96c1f04b..63fe5cc6 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -183,7 +183,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg.add_int(item) elif type(item) is long: msg.add_int64(item) - elif type(item) is str: + elif isinstance(item, string_types): msg.add_string(item) elif type(item) is SFTPAttributes: item._pack(msg) @@ -420,7 +420,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_READLINK: path = msg.get_text() resp = self.server.readlink(path) - if type(resp) is str: + if isinstance(resp, string_types): self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) else: self._send_status(request_number, resp) diff --git a/paramiko/transport.py b/paramiko/transport.py index c9e81fa7..101fe174 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -376,7 +376,7 @@ class Transport (threading.Thread): @rtype: str """ - out = ' 0) and (ord(s[0]) >= 0x80): + if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80): negative = 1 if len(s) % 4: - filler = '\x00' + filler = zero_byte if negative: - filler = '\xff' + filler = max_byte s = filler * (4 - len(s) % 4) + s for i in range(0, len(s), 4): out = (out << 32) + struct.unpack('>I', s[i:i+4])[0] if negative: - out -= (1L << (8 * len(s))) + out -= (long_one << (8 * len(s))) return out +deflate_zero = 0 if PY3 else zero_byte +deflate_ff = 0xff if PY3 else max_byte + def deflate_long(n, add_sign_padding=True): "turns a long-int into a normalized byte string (adapted from Crypto.Util.number)" # after much testing, this algorithm was deemed to be the fastest - s = '' + s = bytes() n = long(n) while (n != 0) and (n != -1): - s = struct.pack('>I', n & 0xffffffffL) + s + s = struct.pack('>I', n & xffffffff) + s n = n >> 32 # strip off leading zeros, FFs for i in enumerate(s): - if (n == 0) and (i[1] != '\000'): + if (n == 0) and (i[1] != deflate_zero): break - if (n == -1) and (i[1] != '\xff'): + if (n == -1) and (i[1] != deflate_ff): break else: # degenerate case, n was either 0 or -1 i = (0,) if n == 0: - s = '\000' + s = zero_byte else: - s = '\xff' + s = max_byte s = s[i[0]:] if add_sign_padding: - if (n == 0) and (ord(s[0]) >= 0x80): - s = '\x00' + s - if (n == -1) and (ord(s[0]) < 0x80): - s = '\xff' + s + if (n == 0) and (byte_ord(s[0]) >= 0x80): + s = zero_byte + s + if (n == -1) and (byte_ord(s[0]) < 0x80): + s = max_byte + s return s -def format_binary_weird(data): - out = '' - for i in enumerate(data): - out += '%02X' % ord(i[1]) - if i[0] % 2: - out += ' ' - if i[0] % 16 == 15: - out += '\n' - return out - def format_binary(data, prefix=''): x = 0 out = [] @@ -113,8 +106,8 @@ def format_binary(data, prefix=''): return [prefix + x for x in out] def format_binary_line(data): - left = ' '.join(['%02X' % ord(c) for c in data]) - right = ''.join([('.%c..' % c)[(ord(c)+63)//95] for c in data]) + left = ' '.join(['%02X' % byte_ord(c) for c in data]) + right = ''.join([('.%c..' % c)[(byte_ord(c)+63)//95] for c in data]) return '%-50s %s' % (left, right) def hexify(s): @@ -126,17 +119,17 @@ def unhexify(s): def safe_string(s): out = '' for c in s: - if (ord(c) >= 32) and (ord(c) <= 127): + if (byte_ord(c) >= 32) and (byte_ord(c) <= 127): out += c else: - out += '%%%02X' % ord(c) + out += '%%%02X' % byte_ord(c) return out # ''.join([['%%%02X' % ord(c), c][(ord(c) >= 32) and (ord(c) <= 127)] for c in s]) def bit_length(n): norm = deflate_long(n, 0) - hbyte = ord(norm[0]) + hbyte = byte_ord(norm[0]) if hbyte == 0: return 1 bitlen = len(norm) * 8 @@ -298,15 +291,15 @@ class Counter (object): """Increament the counter and return the new value""" i = self.blocksize - 1 while i > -1: - c = self.value[i] = chr((ord(self.value[i]) + 1) % 256) - if c != '\x00': + c = self.value[i] = byte_chr((byte_ord(self.value[i]) + 1) % 256) + if c != zero_byte: return self.value.tostring() i -= 1 # counter reset x = deflate_long(self.overflow, add_sign_padding=False) - self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x) + self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x) return self.value.tostring() - def new(cls, nbits, initial_value=1L, overflow=0L): + def new(cls, nbits, initial_value=long_one, overflow=long_zero): return cls(nbits, initial_value=initial_value, overflow=overflow) new = classmethod(new) diff --git a/tests/loop.py b/tests/loop.py index 2f3f5dfc..cfd76265 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -32,7 +32,7 @@ class LoopSocket (object): """ def __init__(self): - self.__in_buffer = '' + self.__in_buffer = bytes() self.__lock = threading.Lock() self.__cv = threading.Condition(self.__lock) self.__timeout = None @@ -42,7 +42,7 @@ class LoopSocket (object): self.__unlink() try: self.__lock.acquire() - self.__in_buffer = '' + self.__in_buffer = bytes() finally: self.__lock.release() diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index b8bea9b5..26ca13b3 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -104,7 +104,7 @@ class StubSFTPServer (SFTPServerInterface): else: # os.open() defaults to 0777 which is # an odd default mode for files - fd = os.open(path, flags, 0666) + fd = os.open(path, flags, o666) except OSError: e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) diff --git a/tests/test_auth.py b/tests/test_auth.py index a7c9e61b..5fd0bf5d 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -32,6 +32,11 @@ from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED from tests.loop import LoopSocket from tests.util import test_path +try: + _pwd = u'\u2022' +except Exception: + _pwd = '\u2022' + class NullServer (ServerInterface): paranoid_did_password = False @@ -65,7 +70,7 @@ class NullServer (ServerInterface): if self.paranoid_did_public_key: return AUTH_SUCCESSFUL return AUTH_PARTIALLY_SUCCESSFUL - if (username == 'utf8') and (password == u'\u2022'): + if (username == 'utf8') and (password == _pwd): return AUTH_SUCCESSFUL if (username == 'non-utf8') and (password == '\xff'): return AUTH_SUCCESSFUL @@ -203,7 +208,7 @@ class AuthTest (unittest.TestCase): """ self.start_server() self.tc.connect(hostkey=self.public_host_key) - remain = self.tc.auth_password('utf8', u'\u2022') + remain = self.tc.auth_password('utf8', _pwd) self.assertEquals([], remain) self.verify_finished() diff --git a/tests/test_kex.py b/tests/test_kex.py index 21986fcc..c94b777b 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -31,7 +31,7 @@ from paramiko.common import * class FakeRng (object): def read(self, n): - return chr(0xcc) * n + return byte_chr(0xcc) * n class FakeKey (object): @@ -44,7 +44,7 @@ class FakeKey (object): class FakeModulusPack (object): - P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL + P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF G = 2 def get_modulus(self, min, ask, max): return self.G, self.P @@ -78,7 +78,7 @@ class FakeTransport (object): class KexTest (unittest.TestCase): - K = 14730343317708716439807310032871972459448364195094179797249681733965528989482751523943515690110179031004049109375612685505881911274101441415545039654102474376472240501616988799699744135291070488314748284283496055223852115360852283821334858541043710301057312858051901453919067023103730011648890038847384890504L + K = 14730343317708716439807310032871972459448364195094179797249681733965528989482751523943515690110179031004049109375612685505881911274101441415545039654102474376472240501616988799699744135291070488314748284283496055223852115360852283821334858541043710301057312858051901453919067023103730011648890038847384890504 def setUp(self): pass diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 589a35d8..c7240db3 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -99,7 +99,7 @@ class KeyTest (unittest.TestCase): self.assertEquals(PUB_RSA.split()[1], key.get_base64()) self.assertEquals(1024, key.get_bits()) - s = StringIO.StringIO() + s = StringIO() key.write_private_key(s) self.assertEquals(RSA_PRIVATE_OUT, s.getvalue()) s.seek(0) @@ -124,7 +124,7 @@ class KeyTest (unittest.TestCase): self.assertEquals(PUB_DSS.split()[1], key.get_base64()) self.assertEquals(1024, key.get_bits()) - s = StringIO.StringIO() + s = StringIO() key.write_private_key(s) self.assertEquals(DSS_PRIVATE_OUT, s.getvalue()) s.seek(0) @@ -207,7 +207,7 @@ class KeyTest (unittest.TestCase): self.assertEquals(PUB_ECDSA.split()[1], key.get_base64()) self.assertEquals(256, key.get_bits()) - s = StringIO.StringIO() + s = StringIO() key.write_private_key(s) self.assertEquals(ECDSA_PRIVATE_OUT, s.getvalue()) s.seek(0) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index a4452711..c80967d1 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -71,7 +71,10 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True - +try: + unicode_folder = u'\u00fcnic\u00f8de' +except SyntaxError: + unicode_folder = '\u00fcnic\u00f8de' def get_sftp(): global sftp @@ -141,7 +144,7 @@ class SFTPTest (unittest.TestCase): def setUp(self): global FOLDER - for i in xrange(1000): + for i in range(1000): FOLDER = FOLDER[:-3] + '%03d' % i try: sftp.mkdir(FOLDER) @@ -236,7 +239,7 @@ class SFTPTest (unittest.TestCase): pass f = sftp.open(FOLDER + '/second.txt', 'r') f.seek(-6, f.SEEK_END) - self.assertEqual(f.read(4), 'tent') + self.assertEqual(u(f.read(4)), 'tent') f.close() finally: try: @@ -301,16 +304,16 @@ class SFTPTest (unittest.TestCase): f.close() stat = sftp.stat(FOLDER + '/special') - sftp.chmod(FOLDER + '/special', (stat.st_mode & ~0777) | 0600) + sftp.chmod(FOLDER + '/special', (stat.st_mode & ~o777) | o600) stat = sftp.stat(FOLDER + '/special') - expected_mode = 0600 + expected_mode = o600 if sys.platform == 'win32': # chmod not really functional on windows - expected_mode = 0666 + expected_mode = o666 if sys.platform == 'cygwin': # even worse. - expected_mode = 0644 - self.assertEqual(stat.st_mode & 0777, expected_mode) + expected_mode = o644 + self.assertEqual(stat.st_mode & o777, expected_mode) self.assertEqual(stat.st_size, 1024) mtime = stat.st_mtime - 3600 @@ -341,17 +344,17 @@ class SFTPTest (unittest.TestCase): f = sftp.open(FOLDER + '/special', 'r+') stat = f.stat() - f.chmod((stat.st_mode & ~0777) | 0600) + f.chmod((stat.st_mode & ~o777) | o600) stat = f.stat() - expected_mode = 0600 + expected_mode = o600 if sys.platform == 'win32': # chmod not really functional on windows - expected_mode = 0666 + expected_mode = o666 if sys.platform == 'cygwin': # even worse. - expected_mode = 0644 - self.assertEqual(stat.st_mode & 0777, expected_mode) + expected_mode = o644 + self.assertEqual(stat.st_mode & o777, expected_mode) self.assertEqual(stat.st_size, 1024) mtime = stat.st_mtime - 3600 @@ -643,7 +646,7 @@ class SFTPTest (unittest.TestCase): f.close() try: - sftp.rename(FOLDER + '/something', FOLDER + u'/\u00fcnic\u00f8de') + sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder) sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') except Exception: e = sys.exc_info()[1] @@ -651,16 +654,16 @@ class SFTPTest (unittest.TestCase): sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') def test_L_utf8_chdir(self): - sftp.mkdir(FOLDER + u'\u00fcnic\u00f8de') + sftp.mkdir(FOLDER + '/' + unicode_folder) try: - sftp.chdir(FOLDER + u'\u00fcnic\u00f8de') + sftp.chdir(FOLDER + '/' + unicode_folder) f = sftp.open('something', 'w') f.write('okay') f.close() sftp.unlink('something') finally: sftp.chdir(None) - sftp.rmdir(FOLDER + u'\u00fcnic\u00f8de') + sftp.rmdir(FOLDER + '/' + unicode_folder) def test_M_bad_readv(self): """ @@ -733,10 +736,16 @@ class SFTPTest (unittest.TestCase): Send an empty file and confirm it is sent. """ target = FOLDER + '/empty file.txt' - stream = StringIO.StringIO() + stream = StringIO() try: attrs = sftp.putfo(stream, target) # the returned attributes should not be null self.assertNotEqual(attrs, None) finally: sftp.remove(target) + + +if __name__ == '__main__': + SFTPTest.init_loopback() + from unittest import main + main() diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 94088f78..20bf0075 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -46,7 +46,7 @@ class BigSFTPTest (unittest.TestCase): def setUp(self): global FOLDER sftp = get_sftp() - for i in xrange(1000): + for i in range(1000): FOLDER = FOLDER[:-3] + '%03d' % i try: sftp.mkdir(FOLDER) @@ -69,7 +69,7 @@ class BigSFTPTest (unittest.TestCase): f = sftp.open('%s/file%d.txt' % (FOLDER, i), 'w', 1) f.write('this is file #%d.\n' % i) f.close() - sftp.chmod('%s/file%d.txt' % (FOLDER, i), 0660) + sftp.chmod('%s/file%d.txt' % (FOLDER, i), o660) # now make sure every file is there, by creating a list of filenmes # and reading them in random order. @@ -124,7 +124,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file, with no linefeeds, using pipelining. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) start = time.time() try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') @@ -165,7 +165,7 @@ class BigSFTPTest (unittest.TestCase): def test_4_prefetch_seek(self): sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -181,13 +181,13 @@ class BigSFTPTest (unittest.TestCase): start = time.time() k2blob = kblob + kblob chunk = 793 - for i in xrange(10): + for i in range(10): f = sftp.open('%s/hongry.txt' % FOLDER, 'r') f.prefetch() base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) - offsets = [base_offset + j * chunk for j in xrange(100)] + offsets = [base_offset + j * chunk for j in range(100)] # randomly seek around and read them out - for j in xrange(100): + for j in range(100): offset = offsets[random.randint(0, len(offsets) - 1)] offsets.remove(offset) f.seek(offset) @@ -203,7 +203,7 @@ class BigSFTPTest (unittest.TestCase): def test_5_readv_seek(self): sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -219,21 +219,21 @@ class BigSFTPTest (unittest.TestCase): start = time.time() k2blob = kblob + kblob chunk = 793 - for i in xrange(10): + for i in range(10): f = sftp.open('%s/hongry.txt' % FOLDER, 'r') base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) # make a bunch of offsets and put them in random order - offsets = [base_offset + j * chunk for j in xrange(100)] + offsets = [base_offset + j * chunk for j in range(100)] readv_list = [] - for j in xrange(100): + for j in range(100): o = offsets[random.randint(0, len(offsets) - 1)] offsets.remove(o) readv_list.append((o, chunk)) ret = f.readv(readv_list) - for i in xrange(len(readv_list)): + for i in range(len(readv_list)): offset = readv_list[i][0] n_offset = offset % 1024 - self.assertEqual(ret.next(), k2blob[n_offset:n_offset + chunk]) + self.assertEqual(next(ret), k2blob[n_offset:n_offset + chunk]) f.close() end = time.time() sys.stderr.write('%ds ' % round(end - start)) @@ -279,7 +279,7 @@ class BigSFTPTest (unittest.TestCase): verify that prefetch and readv don't conflict with each other. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -318,7 +318,7 @@ class BigSFTPTest (unittest.TestCase): returned as a single blob. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -367,7 +367,7 @@ class BigSFTPTest (unittest.TestCase): k32blob = (32 * 1024 * 'x') try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) - for i in xrange(32): + for i in range(32): f.write(k32blob) f.close() diff --git a/tests/test_transport.py b/tests/test_transport.py index 6c62b3e3..69fdbbb2 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -158,7 +158,7 @@ class TransportTest(ParamikoTest): pass def test_2_compute_key(self): - self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929L + self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929 self.tc.H = unhexlify('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3') self.tc.session_id = self.tc.H key = self.tc._compute_key('C', 32) @@ -406,7 +406,7 @@ class TransportTest(ParamikoTest): chan.close() # allow a few seconds for the rekeying to complete - for i in xrange(50): + for i in range(50): if self.tc.H != self.tc.session_id: break time.sleep(0.1) @@ -659,7 +659,7 @@ class TransportTest(ParamikoTest): def run(self): try: - for i in xrange(1, 1+self.iterations): + for i in range(1, 1+self.iterations): if self.done_event.isSet(): break self.watchdog_event.set() diff --git a/tests/test_util.py b/tests/test_util.py index 7e656df8..858dedba 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -101,7 +101,7 @@ class UtilTest(ParamikoTest): def test_2_parse_config(self): global test_config_file - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) self.assertEquals(config._config, [{'host': ['*'], 'config': {}}, {'host': ['*'], 'config': {'identityfile': ['~/.ssh/id_rsa'], 'user': 'robey'}}, @@ -111,7 +111,7 @@ class UtilTest(ParamikoTest): def test_3_host_config(self): global test_config_file - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) for host, values in { @@ -138,7 +138,7 @@ class UtilTest(ParamikoTest): def test_4_generate_key_bytes(self): x = paramiko.util.generate_key_bytes(SHA, 'ABCDEFGH', 'This is my secret passphrase.', 64) - hex = ''.join(['%02x' % ord(c) for c in x]) + hex = ''.join(['%02x' % byte_ord(c) for c in x]) self.assertEquals(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') def test_5_host_keys(self): @@ -172,7 +172,7 @@ Host *.example.com Host * Port 3333 """ - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) host = 'www13.example.com' self.assertEquals( @@ -216,7 +216,7 @@ Host space-delimited Host equals-delimited ProxyCommand=foo bar=biz baz """ - f = cStringIO.StringIO(conf) + f = StringIO(conf) config = paramiko.util.parse_ssh_config(f) for host in ('space-delimited', 'equals-delimited'): self.assertEquals( @@ -228,7 +228,7 @@ Host equals-delimited """ ProxyCommand should perform interpolation on the value """ - config = paramiko.util.parse_ssh_config(cStringIO.StringIO(""" + config = paramiko.util.parse_ssh_config(StringIO(""" Host specific Port 37 ProxyCommand host %h port %p lol @@ -264,7 +264,7 @@ Host www13.* Host * Port 3333 """ - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) host = 'www13.example.com' self.assertEquals( @@ -293,7 +293,7 @@ ProxyCommand foo=bar:%h-%p 'foo=bar:proxy-without-equal-divisor-22'} }.items(): - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) self.assertEquals( paramiko.util.lookup_ssh_host_config(host, config), @@ -323,7 +323,7 @@ IdentityFile id_dsa22 'identityfile': ['id_dsa0', 'id_dsa1', 'id_dsa22']} }.items(): - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) self.assertEquals( paramiko.util.lookup_ssh_host_config(host, config), @@ -338,5 +338,5 @@ IdentityFile id_dsa22 AddressFamily inet IdentityFile something_%l_using_fqdn """ - config = paramiko.util.parse_ssh_config(cStringIO.StringIO(test_config)) + config = paramiko.util.parse_ssh_config(StringIO(test_config)) assert config.lookup('meh') # will die during lookup() if bug regresses -- cgit v1.2.3 From 25dd096da065b1bc2f35c1a62d8a7055b022818b Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:06:35 -0800 Subject: Change all exceptions to modern format (not Py2.5 compatible) --- demos/demo.py | 6 ++---- demos/demo_server.py | 9 +++------ demos/demo_sftp.py | 3 +-- demos/demo_simple.py | 3 +-- demos/forward.py | 6 ++---- demos/rforward.py | 6 ++---- paramiko/auth_handler.py | 3 +-- paramiko/client.py | 16 ++++++++-------- paramiko/dsskey.py | 4 ++-- paramiko/hostkeys.py | 2 +- paramiko/packet.py | 9 +++------ paramiko/pkey.py | 4 ++-- paramiko/proxy.py | 6 ++---- paramiko/server.py | 3 +-- paramiko/sftp_client.py | 4 ++-- paramiko/sftp_file.py | 4 ++-- paramiko/sftp_handle.py | 6 ++---- paramiko/sftp_server.py | 6 ++---- paramiko/transport.py | 28 +++++++++++----------------- paramiko/util.py | 3 +-- tests/stub_sftp.py | 42 ++++++++++++++---------------------------- tests/test_sftp.py | 3 +-- tests/test_transport.py | 5 ++--- 23 files changed, 68 insertions(+), 113 deletions(-) (limited to 'tests/stub_sftp.py') diff --git a/demos/demo.py b/demos/demo.py index 2f245baf..fff61784 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -113,8 +113,7 @@ if hostname.find(':') >= 0: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -171,8 +170,7 @@ try: chan.close() t.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: diff --git a/demos/demo_server.py b/demos/demo_server.py index 5a41a714..34a9bd5e 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -84,8 +84,7 @@ try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 2200)) -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Bind failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -94,8 +93,7 @@ try: sock.listen(100) print('Listening for connection ...') client, addr = sock.accept() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Listen/accept failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -138,8 +136,7 @@ try: chan.send('\r\nI don\'t like you, ' + username + '.\r\n') chan.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index d7f28084..1cccffc2 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -114,8 +114,7 @@ try: t.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: diff --git a/demos/demo_simple.py b/demos/demo_simple.py index fb71145d..ae631e43 100755 --- a/demos/demo_simple.py +++ b/demos/demo_simple.py @@ -77,8 +77,7 @@ try: chan.close() client.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: diff --git a/demos/forward.py b/demos/forward.py index f1a0c047..96e1700d 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -58,8 +58,7 @@ class Handler (SocketServer.BaseRequestHandler): chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) - except Exception: - e = sys.exc_info()[1] + except Exception as e: verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) @@ -170,8 +169,7 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception: - e = sys.exc_info()[1] + except Exception as e: print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) diff --git a/demos/rforward.py b/demos/rforward.py index fcffbcb1..ae70670c 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -46,8 +46,7 @@ def handler(chan, host, port): sock = socket.socket() try: sock.connect((host, port)) - except Exception: - e = sys.exc_info()[1] + except Exception as e: verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) return @@ -151,8 +150,7 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception: - e = sys.exc_info()[1] + except Exception as e: print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index 83f27a18..3e389da8 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -306,8 +306,7 @@ class AuthHandler (object): keyblob = m.get_binary() try: key = self.transport._key_info[keytype](Message(keyblob)) - except SSHException: - e = sys.exc_info()[1] + except SSHException as e: self.transport._log(INFO, 'Auth rejected: public key: %s' % str(e)) key = None except: diff --git a/paramiko/client.py b/paramiko/client.py index 8800b0f6..3ce01687 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -452,8 +452,8 @@ class SSHClient (object): two_factor = (allowed_types == ['password']) if not two_factor: return - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e if not two_factor: for key_filename in key_filenames: @@ -466,8 +466,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e if not two_factor and allow_agent: if self._agent == None: @@ -482,8 +482,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e if not two_factor: keyfiles = [] @@ -514,8 +514,8 @@ class SSHClient (object): if not two_factor: return break - except (SSHException, IOError): - saved_exception = sys.exc_info()[1] + except (SSHException, IOError) as e: + saved_exception = e if password is not None: try: diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index 4c97b26f..a02a4ddb 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -187,8 +187,8 @@ class DSSKey (PKey): # DSAPrivateKey = { version = 0, p, q, g, y, x } try: keylist = BER(data).decode() - except BERException: - raise SSHException('Unable to parse key file: ' + str(sys.exc_info()[1])) + except BERException as e: + raise SSHException('Unable to parse key file: ' + str(e)) if (type(keylist) is not list) or (len(keylist) < 6) or (keylist[0] != 0): raise SSHException('not a valid DSA private key file (bad ber encoding)') self.p = keylist[1] diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 9da883e6..16f7f901 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -92,7 +92,7 @@ class HostKeyEntry: log.info("Unable to handle key of type %s" % (keytype,)) return None - except binascii.Error: + except binascii.Error as e: raise InvalidHostKey(line, sys.exc_info()[1]) return cls(names, key) diff --git a/paramiko/packet.py b/paramiko/packet.py index 26521881..b941c75b 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -216,8 +216,7 @@ class Packetizer (object): n -= len(x) except socket.timeout: got_timeout = True - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. @@ -246,8 +245,7 @@ class Packetizer (object): n = self.__socket.send(out) except socket.timeout: retry_write = True - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): retry_write = True elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): @@ -472,8 +470,7 @@ class Packetizer (object): break except socket.timeout: pass - except EnvironmentError: - e = sys.exc_info()[1] + except EnvironmentError as e: if ((type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR)): pass diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 3d786aec..a2f27edd 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -312,8 +312,8 @@ class PKey (object): # if we trudged to the end of the file, just try to cope. try: data = decodebytes(b(''.join(lines[start:end]))) - except base64.binascii.Error: - raise SSHException('base64 decoding error: ' + str(sys.exc_info()[1])) + except base64.binascii.Error as e: + raise SSHException('base64 decoding error: ' + str(e)) if 'proc-type' not in headers: # unencryped: done return data diff --git a/paramiko/proxy.py b/paramiko/proxy.py index 43c46665..72927c04 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -60,8 +60,7 @@ class ProxyCommand(object): """ try: self.process.stdin.write(content) - except IOError: - e = sys.exc_info()[1] + except IOError as e: # There was a problem with the child process. It probably # died and we can't proceed. The best option here is to # raise an exception informing the user that the informed @@ -81,8 +80,7 @@ class ProxyCommand(object): """ try: return os.read(self.process.stdout.fileno(), size) - except IOError: - e = sys.exc_info()[1] + except IOError as e: raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) def close(self): diff --git a/paramiko/server.py b/paramiko/server.py index f3383add..a922201d 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -623,8 +623,7 @@ class SubsystemHandler (threading.Thread): try: self.__transport._log(DEBUG, 'Starting handler for subsystem %s' % self.__name) self.start_subsystem(self.__name, self.__transport, self.__channel) - except Exception: - e = sys.exc_info()[1] + except Exception as e: self.__transport._log(ERROR, 'Exception in subsystem handler for "%s": %s' % (self.__name, str(e))) self.__transport._log(ERROR, util.tb_strings()) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 5a08db6a..1082c6cb 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -717,8 +717,8 @@ class SFTPClient (BaseSFTP): while True: try: t, data = self._read_packet() - except EOFError: - raise SSHException('Server connection dropped: %s' % str(sys.exc_info()[1])) + except EOFError as e: + raise SSHException('Server connection dropped: %s' % str(e)) msg = Message(data) num = msg.get_int() if num not in self._expecting: diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 3b2bcbf3..f0f7e382 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -464,8 +464,8 @@ class SFTPFile (BufferedFile): # save exception and re-raise it on next file operation try: self.sftp._convert_status(msg) - except Exception: - self._saved_exception = sys.exc_info()[1] + except Exception as e: + self._saved_exception = e return if t != CMD_DATA: raise SFTPError('Expected data') diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index 8b78063b..9fc21008 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -100,8 +100,7 @@ class SFTPHandle (object): readfile.seek(offset) self.__tell = offset data = readfile.read(length) - except IOError: - e = sys.exc_info()[1] + except IOError as e: self.__tell = None return SFTPServer.convert_errno(e.errno) self.__tell += len(data) @@ -140,8 +139,7 @@ class SFTPHandle (object): self.__tell = offset writefile.write(data) writefile.flush() - except IOError: - e = sys.exc_info()[1] + except IOError as e: self.__tell = None return SFTPServer.convert_errno(e.errno) if self.__tell is not None: diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 38adc63c..7dcef6b4 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -92,8 +92,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): except EOFError: self._log(DEBUG, 'EOF -- end of session') return - except Exception: - e = sys.exc_info()[1] + except Exception as e: self._log(DEBUG, 'Exception on channel: ' + str(e)) self._log(DEBUG, util.tb_strings()) return @@ -101,8 +100,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): request_number = msg.get_int() try: self._process(t, request_number, msg) - except Exception: - e = sys.exc_info()[1] + except Exception as e: self._log(DEBUG, 'Exception in server processing: ' + str(e)) self._log(DEBUG, util.tb_strings()) # send some kind of failure message, at least diff --git a/paramiko/transport.py b/paramiko/transport.py index 2008ecb1..6ab9274c 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -295,8 +295,8 @@ class Transport (threading.Thread): sock = socket.socket(af, socket.SOCK_STREAM) try: retry_on_signal(lambda: sock.connect((hostname, port))) - except socket.error: - reason = str(sys.exc_info()[1]) + except socket.error as e: + reason = str(e) else: break else: @@ -1184,10 +1184,9 @@ class Transport (threading.Thread): return [] try: return self.auth_handler.wait_for_response(my_event) - except BadAuthenticationType: - x = sys.exc_info()[1] + except BadAuthenticationType as e: # if password auth isn't allowed, but keyboard-interactive *is*, try to fudge it - if not fallback or ('keyboard-interactive' not in x.allowed_types): + if not fallback or ('keyboard-interactive' not in e.allowed_types): raise try: def handler(title, instructions, fields): @@ -1203,7 +1202,7 @@ class Transport (threading.Thread): return self.auth_interactive(username, handler) except SSHException: # attempt failed; just raise the original exception - raise x + raise e return None def auth_publickey(self, username, key, event=None): @@ -1608,26 +1607,22 @@ class Transport (threading.Thread): msg.add_byte(cMSG_UNIMPLEMENTED) msg.add_int(m.seqno) self._send_message(msg) - except SSHException: - e = sys.exc_info()[1] + except SSHException as e: self._log(ERROR, 'Exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e - except EOFError: - e = sys.exc_info()[1] + except EOFError as e: self._log(DEBUG, 'EOF in transport thread') #self._log(DEBUG, util.tb_strings()) self.saved_exception = e - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: if type(e.args) is tuple: emsg = '%s (%d)' % (e.args[1], e.args[0]) else: emsg = e.args self._log(ERROR, 'Socket exception: ' + emsg) self.saved_exception = e - except Exception: - e = sys.exc_info()[1] + except Exception as e: self._log(ERROR, 'Unknown exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e @@ -1687,9 +1682,8 @@ class Transport (threading.Thread): buf = self.packetizer.readline(timeout) except ProxyCommandFailure: raise - except Exception: - x = sys.exc_info()[1] - raise SSHException('Error reading SSH protocol banner' + str(x)) + except Exception as e: + raise SSHException('Error reading SSH protocol banner' + str(e)) if buf[:4] == 'SSH-': break self._log(DEBUG, 'Banner: ' + buf) diff --git a/paramiko/util.py b/paramiko/util.py index 71fc4673..e9f6250c 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -272,8 +272,7 @@ def retry_on_signal(function): while True: try: return function() - except EnvironmentError: - e = sys.exc_info()[1] + except EnvironmentError as e: if e.errno != errno.EINTR: raise diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 26ca13b3..58e4be26 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -40,8 +40,7 @@ class StubSFTPHandle (SFTPHandle): def stat(self): try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def chattr(self, attr): @@ -50,8 +49,7 @@ class StubSFTPHandle (SFTPHandle): try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) @@ -73,24 +71,21 @@ class StubSFTPServer (SFTPServerInterface): attr.filename = fname out.append(attr) return out - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def stat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.stat(path)) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def lstat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def open(self, path, flags, attr): @@ -105,8 +100,7 @@ class StubSFTPServer (SFTPServerInterface): # os.open() defaults to 0777 which is # an odd default mode for files fd = os.open(path, flags, o666) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) if (flags & os.O_CREAT) and (attr is not None): attr._flags &= ~attr.FLAG_PERMISSIONS @@ -126,8 +120,7 @@ class StubSFTPServer (SFTPServerInterface): fstr = 'rb' try: f = os.fdopen(fd, fstr) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) fobj = StubSFTPHandle(flags) fobj.filename = path @@ -139,8 +132,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.remove(path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -149,8 +141,7 @@ class StubSFTPServer (SFTPServerInterface): newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -160,8 +151,7 @@ class StubSFTPServer (SFTPServerInterface): os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -169,8 +159,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.rmdir(path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -178,8 +167,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -199,8 +187,7 @@ class StubSFTPServer (SFTPServerInterface): target_path = '' try: os.symlink(target_path, path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -208,8 +195,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: symlink = os.readlink(path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) # if it's absolute, remove the root if os.path.isabs(symlink): diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 4a412582..1a7ad02c 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -650,8 +650,7 @@ class SFTPTest (unittest.TestCase): try: sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder) sftp.open(b(FOLDER) + utf8_folder, 'r') - except Exception: - e = sys.exc_info()[1] + except Exception as e: self.fail('exception ' + str(e)) sftp.unlink(b(FOLDER) + utf8_folder) diff --git a/tests/test_transport.py b/tests/test_transport.py index 397b00ca..6f6843ba 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -302,9 +302,8 @@ class TransportTest(ParamikoTest): try: chan = self.tc.open_channel('bogus') self.fail('expected exception') - except ChannelException: - x = sys.exc_info()[1] - self.assertTrue(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) + except ChannelException as e: + self.assertTrue(e.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) def test_9_exit_status(self): """ -- cgit v1.2.3 From b4cd4bea1d5616dac5bc9c8a9c320164667d1533 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Fri, 7 Mar 2014 16:17:19 -0800 Subject: Start in on star import eradication --- paramiko/transport.py | 2 +- paramiko/util.py | 2 +- tests/loop.py | 2 +- tests/stub_sftp.py | 2 +- tests/test_file.py | 2 +- tests/test_kex.py | 2 +- tests/test_message.py | 2 +- tests/test_packetizer.py | 2 +- tests/test_sftp.py | 3 ++- tests/test_sftp_big.py | 2 +- 10 files changed, 11 insertions(+), 10 deletions(-) (limited to 'tests/stub_sftp.py') diff --git a/paramiko/transport.py b/paramiko/transport.py index e7a36c4e..4f41a414 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -33,7 +33,7 @@ import paramiko from paramiko import util from paramiko.auth_handler import AuthHandler from paramiko.channel import Channel -from paramiko.common import * +from paramiko.common import * # Legit, uses dozens of constants & funcs from paramiko.compress import ZlibCompressor, ZlibDecompressor from paramiko.dsskey import DSSKey from paramiko.kex_gex import KexGex diff --git a/paramiko/util.py b/paramiko/util.py index 0e582dc0..93998ff7 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -30,7 +30,7 @@ import struct import traceback import threading -from paramiko.common import * +from paramiko.common import PY2, DEBUG, long, zero_byte, byte_ord, xffffffff, logging, b, max_byte from paramiko.config import SSHConfig diff --git a/tests/loop.py b/tests/loop.py index 6e933f83..4f5dc163 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -21,7 +21,7 @@ """ import threading, socket -from paramiko.common import * +from paramiko.common import asbytes class LoopSocket (object): diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 58e4be26..4fbcead8 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -24,7 +24,7 @@ import os import sys from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED -from paramiko.common import * +from paramiko.common import o666 class StubServer (ServerInterface): diff --git a/tests/test_file.py b/tests/test_file.py index 33a49130..e11d7fd5 100755 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -22,7 +22,7 @@ Some unit tests for the BufferedFile abstraction. import unittest from paramiko.file import BufferedFile -from paramiko.common import * +from paramiko.common import linefeed_byte, crlf, cr_byte class LoopbackFile (BufferedFile): diff --git a/tests/test_kex.py b/tests/test_kex.py index e69c051b..4286d6e8 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -26,7 +26,7 @@ import paramiko.util from paramiko.kex_group1 import KexGroup1 from paramiko.kex_gex import KexGex from paramiko import Message -from paramiko.common import * +from paramiko.common import byte_chr class FakeRng (object): diff --git a/tests/test_message.py b/tests/test_message.py index 4da52cfb..f308c037 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -22,7 +22,7 @@ Some unit tests for ssh protocol message blocks. import unittest from paramiko.message import Message -from paramiko.common import * +from paramiko.common import byte_chr, zero_byte class MessageTest (unittest.TestCase): diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 5c36fed6..09d7fcc3 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -25,7 +25,7 @@ from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC from paramiko import Message, Packetizer, util -from paramiko.common import * +from paramiko.common import byte_chr, zero_byte x55 = byte_chr(0x55) x1f = byte_chr(0x1f) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index f8fab1ce..82422019 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -25,13 +25,14 @@ do test file operations in (so no existing files will be harmed). from binascii import hexlify import os +import sys import warnings import threading import unittest from tempfile import mkstemp import paramiko -from paramiko.common import * +from paramiko.common import PY2, b, u, StringIO, o777, o600 from tests.stub_sftp import StubServer, StubSFTPServer from tests.loop import LoopSocket from tests.util import test_path diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 6870c6b4..b1b13d58 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -33,7 +33,7 @@ import time import unittest import paramiko -from paramiko.common import * +from paramiko.common import o660 from tests.stub_sftp import StubServer, StubSFTPServer from tests.loop import LoopSocket from tests.test_sftp import get_sftp -- cgit v1.2.3 From f0017b83309899bf6fffc0fa90093c36f1a7f7ea Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 7 Mar 2014 20:45:26 -0800 Subject: Fix import * and a bunch of PEP8 formatting --- paramiko/agent.py | 18 ++--- paramiko/auth_handler.py | 31 ++++----- paramiko/ber.py | 18 ++--- paramiko/buffered_pipe.py | 4 +- paramiko/channel.py | 24 +++---- paramiko/client.py | 17 ++--- paramiko/common.py | 54 +++++++++------ paramiko/dsskey.py | 11 ++- paramiko/ecdsakey.py | 15 ++-- paramiko/file.py | 17 ++--- paramiko/hostkeys.py | 7 +- paramiko/kex_gex.py | 6 +- paramiko/kex_group1.py | 10 +-- paramiko/logging22.py | 66 ------------------ paramiko/message.py | 9 +-- paramiko/packet.py | 72 +++++-------------- paramiko/pipe.py | 21 +++--- paramiko/pkey.py | 12 ++-- paramiko/primes.py | 13 ++-- paramiko/proxy.py | 4 +- paramiko/rsakey.py | 14 ++-- paramiko/server.py | 5 +- paramiko/sftp.py | 39 +++++------ paramiko/sftp_attr.py | 8 +-- paramiko/sftp_client.py | 27 +++++--- paramiko/sftp_file.py | 10 +-- paramiko/sftp_handle.py | 8 +-- paramiko/sftp_server.py | 28 +++++--- paramiko/sftp_si.py | 7 +- paramiko/transport.py | 165 ++++++++++++++++++++++---------------------- paramiko/util.py | 5 +- tests/stub_sftp.py | 5 +- tests/test_auth.py | 3 +- tests/test_buffered_pipe.py | 15 ++-- tests/test_client.py | 2 +- tests/test_hostkeys.py | 3 +- tests/test_kex.py | 10 +++ tests/test_packetizer.py | 6 +- tests/test_pkey.py | 3 +- tests/test_sftp.py | 5 +- tests/test_sftp_big.py | 5 -- tests/test_transport.py | 9 ++- tests/test_util.py | 5 +- 43 files changed, 359 insertions(+), 457 deletions(-) delete mode 100644 paramiko/logging22.py (limited to 'tests/stub_sftp.py') diff --git a/paramiko/agent.py b/paramiko/agent.py index 3aa58bea..2b11337f 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -29,12 +29,12 @@ import time import tempfile import stat from select import select +from paramiko.common import asbytes, io_sleep +from paramiko.py3compat import byte_chr from paramiko.ssh_exception import SSHException from paramiko.message import Message from paramiko.pkey import PKey -from paramiko.channel import Channel -from paramiko.common import * from paramiko.util import retry_on_signal cSSH2_AGENTC_REQUEST_IDENTITIES = byte_chr(11) @@ -43,7 +43,6 @@ cSSH2_AGENTC_SIGN_REQUEST = byte_chr(13) SSH2_AGENT_SIGN_RESPONSE = 14 - class AgentSSH(object): def __init__(self): self._conn = None @@ -107,7 +106,7 @@ class AgentProxyThread(threading.Thread): def run(self): try: - (r,addr) = self.get_connection() + (r, addr) = self.get_connection() self.__inr = r self.__addr = addr self._agent.connect() @@ -163,11 +162,10 @@ class AgentLocalProxy(AgentProxyThread): try: conn.bind(self._agent._get_filename()) conn.listen(1) - (r,addr) = conn.accept() - return (r, addr) + (r, addr) = conn.accept() + return r, addr except: raise - return None class AgentRemoteProxy(AgentProxyThread): @@ -179,7 +177,7 @@ class AgentRemoteProxy(AgentProxyThread): self.__chan = chan def get_connection(self): - return (self.__chan, None) + return self.__chan, None class AgentClientProxy(object): @@ -280,9 +278,7 @@ class AgentServerProxy(AgentSSH): :return: a dict containing the ``SSH_AUTH_SOCK`` environnement variables """ - env = {} - env['SSH_AUTH_SOCK'] = self._get_filename() - return env + return {'SSH_AUTH_SOCK': self._get_filename()} def _get_filename(self): return self._file diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index 2cc09353..c00ad41c 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -20,15 +20,18 @@ `.AuthHandler` """ -import threading import weakref +from paramiko.common import cMSG_SERVICE_REQUEST, cMSG_DISCONNECT, \ + DISCONNECT_SERVICE_NOT_AVAILABLE, DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, \ + cMSG_USERAUTH_REQUEST, cMSG_SERVICE_ACCEPT, DEBUG, AUTH_SUCCESSFUL, INFO, \ + cMSG_USERAUTH_SUCCESS, cMSG_USERAUTH_FAILURE, AUTH_PARTIALLY_SUCCESSFUL, \ + cMSG_USERAUTH_INFO_REQUEST, WARNING, AUTH_FAILED, cMSG_USERAUTH_PK_OK, \ + cMSG_USERAUTH_INFO_RESPONSE, MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT, \ + MSG_USERAUTH_REQUEST, MSG_USERAUTH_SUCCESS, MSG_USERAUTH_FAILURE, \ + MSG_USERAUTH_BANNER, MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE -# this helps freezing utils -import encodings.utf_8 - -from paramiko.common import * -from paramiko import util from paramiko.message import Message +from paramiko.py3compat import bytestring from paramiko.ssh_exception import SSHException, AuthenticationException, \ BadAuthenticationType, PartialAuthentication from paramiko.server import InteractiveQuery @@ -114,10 +117,8 @@ class AuthHandler (object): if self.auth_event is not None: self.auth_event.set() - ### internals... - def _request_auth(self): m = Message() m.add_byte(cMSG_SERVICE_REQUEST) @@ -149,7 +150,7 @@ class AuthHandler (object): m.add_string(username) m.add_string(service) m.add_string('publickey') - m.add_boolean(1) + m.add_boolean(True) m.add_string(key.get_name()) m.add_string(key) return m.asbytes() @@ -230,9 +231,9 @@ class AuthHandler (object): m.add_byte(cMSG_USERAUTH_FAILURE) m.add_string(self.transport.server_object.get_allowed_auths(username)) if result == AUTH_PARTIALLY_SUCCESSFUL: - m.add_boolean(1) + m.add_boolean(True) else: - m.add_boolean(0) + m.add_boolean(False) self.auth_fail_count += 1 self.transport._send_message(m) if self.auth_fail_count >= 10: @@ -259,7 +260,7 @@ class AuthHandler (object): m = Message() m.add_byte(cMSG_USERAUTH_FAILURE) m.add_string('none') - m.add_boolean(0) + m.add_boolean(False) self.transport._send_message(m) return if self.authenticated: @@ -351,7 +352,7 @@ class AuthHandler (object): self.transport._log(INFO, 'Authentication (%s) successful!' % self.auth_method) self.authenticated = True self.transport._auth_trigger() - if self.auth_event != None: + if self.auth_event is not None: self.auth_event.set() def _parse_userauth_failure(self, m): @@ -369,7 +370,7 @@ class AuthHandler (object): self.transport._log(INFO, 'Authentication (%s) failed.' % self.auth_method) self.authenticated = False self.username = None - if self.auth_event != None: + if self.auth_event is not None: self.auth_event.set() def _parse_userauth_banner(self, m): @@ -411,7 +412,6 @@ class AuthHandler (object): self._interactive_query(result) return self._send_auth_result(self.auth_username, 'keyboard-interactive', result) - _handler_table = { MSG_SERVICE_REQUEST: _parse_service_request, @@ -423,4 +423,3 @@ class AuthHandler (object): MSG_USERAUTH_INFO_REQUEST: _parse_userauth_info_request, MSG_USERAUTH_INFO_RESPONSE: _parse_userauth_info_response, } - diff --git a/paramiko/ber.py b/paramiko/ber.py index c4f35210..05152303 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -15,10 +15,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - +from paramiko.common import max_byte, zero_byte +from paramiko.py3compat import b, byte_ord, byte_chr, long import paramiko.util as util -from paramiko.common import * class BERException (Exception): @@ -71,12 +71,12 @@ class BER(object): t = size & 0x7f if self.idx + t > len(self.content): return None - size = util.inflate_long(self.content[self.idx : self.idx + t], True) + size = util.inflate_long(self.content[self.idx: self.idx + t], True) self.idx += t if self.idx + size > len(self.content): # can't fit return None - data = self.content[self.idx : self.idx + size] + data = self.content[self.idx: self.idx + size] self.idx += size # now switch on id if ident == 0x30: @@ -91,9 +91,9 @@ class BER(object): def decode_sequence(data): out = [] - b = BER(data) + ber = BER(data) while True: - x = b.decode_next() + x = ber.decode_next() if x is None: break out.append(x) @@ -126,8 +126,8 @@ class BER(object): raise BERException('Unknown type for encoding: %s' % repr(type(x))) def encode_sequence(data): - b = BER() + ber = BER() for item in data: - b.encode(item) - return b.asbytes() + ber.encode(item) + return ber.asbytes() encode_sequence = staticmethod(encode_sequence) diff --git a/paramiko/buffered_pipe.py b/paramiko/buffered_pipe.py index 94514b67..ac35b3e1 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.common import * +from paramiko.py3compat import PY2, b class PipeTimeout (IOError): @@ -62,7 +62,6 @@ class BufferedPipe (object): def _buffer_tobytes(self, limit=None): return self._buffer[:limit].tobytes() - def set_event(self, event): """ Set an event on this buffer. When data is ready to be read (or the @@ -208,4 +207,3 @@ class BufferedPipe (object): return len(self._buffer) finally: self._lock.release() - diff --git a/paramiko/channel.py b/paramiko/channel.py index 107786c4..e10ddbac 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -21,15 +21,17 @@ Abstraction for an SSH2 channel. """ import binascii -import sys import time import threading import socket -import os -from paramiko.common import * from paramiko import util +from paramiko.common import cMSG_CHANNEL_REQUEST, cMSG_CHANNEL_WINDOW_ADJUST, \ + cMSG_CHANNEL_DATA, cMSG_CHANNEL_EXTENDED_DATA, DEBUG, ERROR, \ + cMSG_CHANNEL_SUCCESS, cMSG_CHANNEL_FAILURE, cMSG_CHANNEL_EOF, \ + cMSG_CHANNEL_CLOSE from paramiko.message import Message +from paramiko.py3compat import bytes_types from paramiko.ssh_exception import SSHException from paramiko.file import BufferedFile from paramiko.buffered_pipe import BufferedPipe, PipeTimeout @@ -112,7 +114,7 @@ class Channel (object): out += ' (EOF received)' if self.eof_sent: out += ' (EOF sent)' - out += ' (open) window=%d' % (self.out_window_size) + out += ' (open) window=%d' % self.out_window_size if len(self.in_buffer) > 0: out += ' in-buffer=%d' % (len(self.in_buffer),) out += ' -> ' + repr(self.transport) @@ -176,7 +178,7 @@ class Channel (object): m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('shell') - m.add_boolean(1) + m.add_boolean(True) self._event_pending() self.transport._send_user_message(m) self._wait_for_event() @@ -465,10 +467,8 @@ class Channel (object): self._feed(data) return old - ### socket API - def settimeout(self, timeout): """ Set a timeout on blocking read/write operations. The ``timeout`` @@ -885,10 +885,8 @@ class Channel (object): """ self.shutdown(1) - ### calls from Transport - def _set_transport(self, transport): self.transport = transport self.logger = util.get_logger(self.transport.get_log_channel()) @@ -1063,10 +1061,8 @@ class Channel (object): if m is not None: self.transport._send_user_message(m) - ### internals... - def _log(self, level, msg, *args): self.logger.log(level, "[chan " + self._name + "] " + msg, *args) @@ -1171,7 +1167,7 @@ class Channel (object): return 0 then = time.time() self.out_buffer_cv.wait(timeout) - if timeout != None: + if timeout is not None: timeout -= time.time() - then if timeout <= 0.0: raise socket.timeout() @@ -1201,7 +1197,7 @@ class ChannelFile (BufferedFile): flush the buffer. """ - def __init__(self, channel, mode = 'r', bufsize = -1): + def __init__(self, channel, mode='r', bufsize=-1): self.channel = channel BufferedFile.__init__(self) self._set_mode(mode, bufsize) @@ -1221,7 +1217,7 @@ class ChannelFile (BufferedFile): class ChannelStderrFile (ChannelFile): - def __init__(self, channel, mode = 'r', bufsize = -1): + def __init__(self, channel, mode='r', bufsize=-1): ChannelFile.__init__(self, channel, mode, bufsize) def _read(self, size): diff --git a/paramiko/client.py b/paramiko/client.py index 9e78abf9..c1bf4735 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -27,10 +27,11 @@ import socket import warnings from paramiko.agent import Agent -from paramiko.common import * +from paramiko.common import DEBUG from paramiko.config import SSH_PORT from paramiko.dsskey import DSSKey from paramiko.hostkeys import HostKeys +from paramiko.py3compat import string_types from paramiko.resource import ResourceManager from paramiko.rsakey import RSAKey from paramiko.ssh_exception import SSHException, BadHostKeyException @@ -266,7 +267,7 @@ class SSHClient (object): if key_filename is None: key_filenames = [] elif isinstance(key_filename, string_types): - key_filenames = [ key_filename ] + key_filenames = [key_filename] else: key_filenames = key_filename self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) @@ -280,7 +281,7 @@ class SSHClient (object): self._transport.close() self._transport = None - if self._agent != None: + if self._agent is not None: self._agent.close() self._agent = None @@ -304,7 +305,7 @@ class SSHClient (object): :raises SSHException: if the server fails to execute the command """ chan = self._transport.open_session() - if(get_pty): + if get_pty: chan.get_pty() chan.settimeout(timeout) chan.exec_command(command) @@ -314,7 +315,7 @@ class SSHClient (object): return stdin, stdout, stderr def invoke_shell(self, term='vt100', width=80, height=24, width_pixels=0, - height_pixels=0): + height_pixels=0): """ Start an interactive shell session on the SSH server. A new `.Channel` is opened and connected to a pseudo-terminal using the requested @@ -394,7 +395,7 @@ class SSHClient (object): saved_exception = e if not two_factor and allow_agent: - if self._agent == None: + if self._agent is None: self._agent = Agent() for key in self._agent.get_keys(): @@ -445,8 +446,8 @@ class SSHClient (object): try: self._transport.auth_password(username, password) return - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e elif two_factor: raise SSHException('Two-factor authentication requires a password') diff --git a/paramiko/common.py b/paramiko/common.py index e30df73a..9a5e2ee1 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -19,7 +19,8 @@ """ Common constants and global variables. """ -from paramiko.py3compat import * +import logging +from paramiko.py3compat import byte_chr, PY2, bytes_types, string_types, b, long MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, MSG_SERVICE_REQUEST, \ MSG_SERVICE_ACCEPT = range(1, 7) @@ -34,10 +35,35 @@ MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \ MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MSG_CHANNEL_REQUEST, \ MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE = range(90, 101) -for key in list(locals().keys()): - if key.startswith('MSG_'): - locals()['c' + key] = byte_chr(locals()[key]) -del key +cMSG_DISCONNECT = byte_chr(MSG_DISCONNECT) +cMSG_IGNORE = byte_chr(MSG_IGNORE) +cMSG_UNIMPLEMENTED = byte_chr(MSG_UNIMPLEMENTED) +cMSG_DEBUG = byte_chr(MSG_DEBUG) +cMSG_SERVICE_REQUEST = byte_chr(MSG_SERVICE_REQUEST) +cMSG_SERVICE_ACCEPT = byte_chr(MSG_SERVICE_ACCEPT) +cMSG_KEXINIT = byte_chr(MSG_KEXINIT) +cMSG_NEWKEYS = byte_chr(MSG_NEWKEYS) +cMSG_USERAUTH_REQUEST = byte_chr(MSG_USERAUTH_REQUEST) +cMSG_USERAUTH_FAILURE = byte_chr(MSG_USERAUTH_FAILURE) +cMSG_USERAUTH_SUCCESS = byte_chr(MSG_USERAUTH_SUCCESS) +cMSG_USERAUTH_BANNER = byte_chr(MSG_USERAUTH_BANNER) +cMSG_USERAUTH_PK_OK = byte_chr(MSG_USERAUTH_PK_OK) +cMSG_USERAUTH_INFO_REQUEST = byte_chr(MSG_USERAUTH_INFO_REQUEST) +cMSG_USERAUTH_INFO_RESPONSE = byte_chr(MSG_USERAUTH_INFO_RESPONSE) +cMSG_GLOBAL_REQUEST = byte_chr(MSG_GLOBAL_REQUEST) +cMSG_REQUEST_SUCCESS = byte_chr(MSG_REQUEST_SUCCESS) +cMSG_REQUEST_FAILURE = byte_chr(MSG_REQUEST_FAILURE) +cMSG_CHANNEL_OPEN = byte_chr(MSG_CHANNEL_OPEN) +cMSG_CHANNEL_OPEN_SUCCESS = byte_chr(MSG_CHANNEL_OPEN_SUCCESS) +cMSG_CHANNEL_OPEN_FAILURE = byte_chr(MSG_CHANNEL_OPEN_FAILURE) +cMSG_CHANNEL_WINDOW_ADJUST = byte_chr(MSG_CHANNEL_WINDOW_ADJUST) +cMSG_CHANNEL_DATA = byte_chr(MSG_CHANNEL_DATA) +cMSG_CHANNEL_EXTENDED_DATA = byte_chr(MSG_CHANNEL_EXTENDED_DATA) +cMSG_CHANNEL_EOF = byte_chr(MSG_CHANNEL_EOF) +cMSG_CHANNEL_CLOSE = byte_chr(MSG_CHANNEL_CLOSE) +cMSG_CHANNEL_REQUEST = byte_chr(MSG_CHANNEL_REQUEST) +cMSG_CHANNEL_SUCCESS = byte_chr(MSG_CHANNEL_SUCCESS) +cMSG_CHANNEL_FAILURE = byte_chr(MSG_CHANNEL_FAILURE) # for debugging: MSG_NAMES = { @@ -105,24 +131,6 @@ from Crypto import Random # keep a crypto-strong PRNG nearby rng = Random.new() -import sys -if sys.version_info < (2, 3): - try: - import logging - except: - import logging22 as logging - import select - PY22 = True - - import socket - if not hasattr(socket, 'timeout'): - class timeout(socket.error): pass - socket.timeout = timeout - del timeout -else: - import logging - PY22 = False - zero_byte = byte_chr(0) one_byte = byte_chr(1) four_byte = byte_chr(4) diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index 6ab298ac..c26966e8 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -23,8 +23,9 @@ DSS keys. from Crypto.PublicKey import DSA from Crypto.Hash import SHA -from paramiko.common import * from paramiko import util +from paramiko.common import zero_byte, rng +from paramiko.py3compat import long from paramiko.ssh_exception import SSHException from paramiko.message import Message from paramiko.ber import BER, BERException @@ -110,9 +111,9 @@ class DSSKey (PKey): rstr = util.deflate_long(r, 0) sstr = util.deflate_long(s, 0) if len(rstr) < 20: - rstr = zero_byte * (20 - len(rstr)) + rstr + rstr += zero_byte * (20 - len(rstr)) if len(sstr) < 20: - sstr = zero_byte * (20 - len(sstr)) + sstr + sstr += zero_byte * (20 - len(sstr)) m.add_string(rstr + sstr) return m @@ -137,7 +138,7 @@ class DSSKey (PKey): def _encode_key(self): if self.x is None: raise SSHException('Not enough key information') - keylist = [ 0, self.p, self.q, self.g, self.y, self.x ] + keylist = [0, self.p, self.q, self.g, self.y, self.x] try: b = BER() b.encode(keylist) @@ -168,10 +169,8 @@ class DSSKey (PKey): return key generate = staticmethod(generate) - ### internals... - def _from_private_key_file(self, filename, password): data = self._read_private_key_file('DSA', filename, password) self._decode_key(data) diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index 3ecf0a58..6ae2d277 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -22,15 +22,13 @@ L{ECDSAKey} import binascii from ecdsa import SigningKey, VerifyingKey, der, curves -from ecdsa.util import number_to_string, sigencode_string, sigencode_strings, sigdecode_strings -from Crypto.Hash import SHA256, MD5 -from Crypto.Cipher import DES3 +from Crypto.Hash import SHA256 +from ecdsa.test_pyecdsa import ECDSA +from paramiko.common import four_byte, one_byte -from paramiko.common import * -from paramiko import util from paramiko.message import Message -from paramiko.ber import BER, BERException from paramiko.pkey import PKey +from paramiko.py3compat import byte_chr, u from paramiko.ssh_exception import SSHException @@ -145,10 +143,8 @@ class ECDSAKey (PKey): return key generate = staticmethod(generate) - ### internals... - def _from_private_key_file(self, filename, password): data = self._read_private_key_file('EC', filename, password) self._decode_key(data) @@ -159,6 +155,7 @@ class ECDSAKey (PKey): ALLOWED_PADDINGS = [one_byte, byte_chr(2) * 2, byte_chr(3) * 3, byte_chr(4) * 4, byte_chr(5) * 5, byte_chr(6) * 6, byte_chr(7) * 7] + def _decode_key(self, data): s, padding = der.remove_sequence(data) if padding: @@ -180,4 +177,4 @@ class ECDSAKey (PKey): msg = Message(sig) r = msg.get_mpint() s = msg.get_mpint() - return (r, s) + return r, s diff --git a/paramiko/file.py b/paramiko/file.py index 69b730e2..f57aa79f 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -15,8 +15,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - -from paramiko.common import * +from paramiko.common import linefeed_byte_value, crlf, cr_byte, linefeed_byte, \ + cr_byte_value +from paramiko.py3compat import BytesIO, PY2, u, b, bytes_types class BufferedFile (object): @@ -232,7 +233,7 @@ class BufferedFile (object): pos = line.find(linefeed_byte) if self._flags & self.FLAG_UNIVERSAL_NEWLINE: rpos = line.find(cr_byte) - if (rpos >= 0) and ((rpos < pos) or (pos < 0)): + if (rpos >= 0) and (rpos < pos or pos < 0): pos = rpos xpos = pos + 1 if (line[pos] == cr_byte_value) and (xpos < len(line)) and (line[xpos] == linefeed_byte_value): @@ -358,10 +359,8 @@ class BufferedFile (object): def closed(self): return self._closed - ### overrides... - def _read(self, size): """ (subclass override) @@ -388,10 +387,8 @@ class BufferedFile (object): """ return 0 - ### internals... - def _set_mode(self, mode='r', bufsize=-1): """ Subclasses call this method to initialize the BufferedFile. @@ -419,13 +416,13 @@ class BufferedFile (object): self._flags |= self.FLAG_READ if ('w' in mode) or ('+' in mode): self._flags |= self.FLAG_WRITE - if ('a' in mode): + if 'a' in mode: self._flags |= self.FLAG_WRITE | self.FLAG_APPEND self._size = self._get_size() self._pos = self._realpos = self._size - if ('b' in mode): + if 'b' in mode: self._flags |= self.FLAG_BINARY - if ('U' in mode): + if 'U' in mode: self._flags |= self.FLAG_UNIVERSAL_NEWLINE # built-in file objects have this attribute to store which kinds of # line terminations they've seen: diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 0141bb24..f32fbeb6 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -17,15 +17,17 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -import base64 import binascii from Crypto.Hash import SHA, HMAC +from paramiko.common import rng +from paramiko.py3compat import b, u, encodebytes, decodebytes + try: from collections import MutableMapping except ImportError: + # noinspection PyUnresolvedReferences from UserDict import DictMixin as MutableMapping -from paramiko.common import * from paramiko.dsskey import DSSKey from paramiko.rsakey import RSAKey from paramiko.util import get_logger, constant_time_bytes_eq @@ -213,7 +215,6 @@ class HostKeys (MutableMapping): def __delitem__(self, key): k = self[key] - pass def __getitem__(self, key): ret = self.lookup(key) diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index 8ac23212..02e507b7 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -23,11 +23,11 @@ client side, and a B{lot} more on the server side. """ from Crypto.Hash import SHA -from Crypto.Util import number -from paramiko.common import * from paramiko import util +from paramiko.common import DEBUG from paramiko.message import Message +from paramiko.py3compat import byte_chr, byte_ord, byte_mask from paramiko.ssh_exception import SSHException @@ -88,10 +88,8 @@ class KexGex (object): return self._parse_kexdh_gex_request_old(m) raise SSHException('KexGex asked to handle packet type %d' % ptype) - ### internals... - def _generate_x(self): # generate an "x" (1 < x < (p-1)/2). q = (self.p - 1) // 2 diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py index 05693a1f..3dfb7f18 100644 --- a/paramiko/kex_group1.py +++ b/paramiko/kex_group1.py @@ -23,9 +23,10 @@ Standard SSH key exchange ("kex" if you wanna sound cool). Diffie-Hellman of from Crypto.Hash import SHA -from paramiko.common import * from paramiko import util +from paramiko.common import max_byte, zero_byte from paramiko.message import Message +from paramiko.py3compat import byte_chr, long, byte_mask from paramiko.ssh_exception import SSHException @@ -39,6 +40,7 @@ G = 2 b7fffffffffffffff = byte_chr(0x7f) + max_byte * 7 b0000000000000000 = zero_byte * 8 + class KexGroup1(object): name = 'diffie-hellman-group1-sha1' @@ -71,10 +73,8 @@ class KexGroup1(object): return self._parse_kexdh_reply(m) raise SSHException('KexGroup1 asked to handle packet type %d' % ptype) - ### internals... - def _generate_x(self): # generate an "x" (1 < x < q), where q is (p-1)/2. # p is a 128-byte (1024-bit) number, where the first 64 bits are 1. @@ -84,8 +84,8 @@ class KexGroup1(object): while 1: x_bytes = self.transport.rng.read(128) x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] - if (x_bytes[:8] != b7fffffffffffffff) and \ - (x_bytes[:8] != b0000000000000000): + if (x_bytes[:8] != b7fffffffffffffff and + x_bytes[:8] != b0000000000000000): break self.x = util.inflate_long(x_bytes) diff --git a/paramiko/logging22.py b/paramiko/logging22.py deleted file mode 100644 index 34a9a931..00000000 --- a/paramiko/logging22.py +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright (C) 2003-2007 Robey Pointer -# -# This file is part of paramiko. -# -# Paramiko is free software; you can redistribute it and/or modify it under the -# terms of the GNU Lesser General Public License as published by the Free -# Software Foundation; either version 2.1 of the License, or (at your option) -# any later version. -# -# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Paramiko; if not, write to the Free Software Foundation, Inc., -# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - -""" -Stub out logging on Python < 2.3. -""" - - -DEBUG = 10 -INFO = 20 -WARNING = 30 -ERROR = 40 -CRITICAL = 50 - - -def getLogger(name): - return _logger - - -class logger (object): - def __init__(self): - self.handlers = [ ] - self.level = ERROR - - def setLevel(self, level): - self.level = level - - def addHandler(self, h): - self.handlers.append(h) - - def addFilter(self, filter): - pass - - def log(self, level, text): - if level >= self.level: - for h in self.handlers: - h.f.write(text + '\n') - h.f.flush() - -class StreamHandler (object): - def __init__(self, f): - self.f = f - - def setFormatter(self, f): - pass - -class Formatter (object): - def __init__(self, x, y): - pass - -_logger = logger() diff --git a/paramiko/message.py b/paramiko/message.py index 9007cd4b..da6acf8e 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -23,7 +23,8 @@ Implementation of an SSH2 "message". import struct from paramiko import util -from paramiko.common import * +from paramiko.common import zero_byte, max_byte, one_byte, asbytes +from paramiko.py3compat import long, BytesIO, u, integer_types class Message (object): @@ -47,7 +48,7 @@ class Message (object): the byte stream to use as the message content (passed in only when decomposing a message). """ - if content != None: + if content is not None: self.packet = BytesIO(content) else: self.packet = BytesIO() @@ -105,8 +106,8 @@ class Message (object): bytes remaining in the message. """ b = self.packet.read(n) - max_pad_size = 1<<20 # Limit padding to 1 MB - if len(b) < n and n < max_pad_size: + max_pad_size = 1 << 20 # Limit padding to 1 MB + if len(b) < n < max_pad_size: return b + zero_byte * (n - len(b)) return b diff --git a/paramiko/packet.py b/paramiko/packet.py index fd1f0197..0f51df5e 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -21,14 +21,15 @@ Packet handling """ import errno -import select import socket import struct import threading import time -from paramiko.common import * from paramiko import util +from paramiko.common import linefeed_byte, cr_byte_value, asbytes, MSG_NAMES, \ + DEBUG, xffffffff, zero_byte, rng +from paramiko.py3compat import u, byte_ord from paramiko.ssh_exception import SSHException, ProxyCommandFailure from paramiko.message import Message @@ -57,8 +58,8 @@ class Packetizer (object): REKEY_PACKETS = pow(2, 29) REKEY_BYTES = pow(2, 29) - REKEY_PACKETS_OVERFLOW_MAX = pow(2,29) # Allow receiving this many packets after a re-key request before terminating - REKEY_BYTES_OVERFLOW_MAX = pow(2,29) # Allow receiving this many bytes after a re-key request before terminating + REKEY_PACKETS_OVERFLOW_MAX = pow(2, 29) # Allow receiving this many packets after a re-key request before terminating + REKEY_BYTES_OVERFLOW_MAX = pow(2, 29) # Allow receiving this many bytes after a re-key request before terminating def __init__(self, socket): self.__socket = socket @@ -201,8 +202,6 @@ class Packetizer (object): out = self.__remainder[:n] self.__remainder = self.__remainder[n:] n -= len(out) - if PY22: - return self._py22_read_all(n, out) while n > 0: got_timeout = False try: @@ -251,7 +250,7 @@ class Packetizer (object): else: n = -1 except ProxyCommandFailure: - raise # so it doesn't get swallowed by the below catchall + raise # so it doesn't get swallowed by the below catchall except Exception: # could be: (32, 'Broken pipe') n = -1 @@ -275,7 +274,7 @@ class Packetizer (object): while not linefeed_byte in buf: buf += self._read_timeout(timeout) n = buf.index(linefeed_byte) - self.__remainder = buf[n+1:] + self.__remainder = buf[n + 1:] buf = buf[:n] if (len(buf) > 0) and (buf[-1] == cr_byte_value): buf = buf[:-1] @@ -301,12 +300,12 @@ class Packetizer (object): if self.__dump_packets: self._log(DEBUG, 'Write packet <%s>, length %d' % (cmd_name, orig_len)) self._log(DEBUG, util.format_binary(packet, 'OUT: ')) - if self.__block_engine_out != None: + if self.__block_engine_out is not None: out = self.__block_engine_out.encrypt(packet) else: out = packet # + mac - if self.__block_engine_out != None: + if self.__block_engine_out is not None: payload = struct.pack('>I', self.__sequence_number_out) + packet out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out] self.__sequence_number_out = (self.__sequence_number_out + 1) & xffffffff @@ -314,8 +313,8 @@ class Packetizer (object): self.__sent_bytes += len(out) self.__sent_packets += 1 - if ((self.__sent_packets >= self.REKEY_PACKETS) or (self.__sent_bytes >= self.REKEY_BYTES)) \ - and not self.__need_rekey: + if (self.__sent_packets >= self.REKEY_PACKETS or self.__sent_bytes >= self.REKEY_BYTES)\ + and not self.__need_rekey: # only ask once for rekeying self._log(DEBUG, 'Rekeying (hit %d packets, %d bytes sent)' % (self.__sent_packets, self.__sent_bytes)) @@ -334,10 +333,10 @@ class Packetizer (object): :raises NeedRekeyException: if the transport should rekey """ header = self.read_all(self.__block_size_in, check_rekey=True) - if self.__block_engine_in != None: + if self.__block_engine_in is not None: header = self.__block_engine_in.decrypt(header) if self.__dump_packets: - self._log(DEBUG, util.format_binary(header, 'IN: ')); + self._log(DEBUG, util.format_binary(header, 'IN: ')) packet_size = struct.unpack('>I', header[:4])[0] # leftover contains decrypted bytes from the first block (after the length field) leftover = header[4:] @@ -346,10 +345,10 @@ class Packetizer (object): buf = self.read_all(packet_size + self.__mac_size_in - len(leftover)) packet = buf[:packet_size - len(leftover)] post_packet = buf[packet_size - len(leftover):] - if self.__block_engine_in != None: + if self.__block_engine_in is not None: packet = self.__block_engine_in.decrypt(packet) if self.__dump_packets: - self._log(DEBUG, util.format_binary(packet, 'IN: ')); + self._log(DEBUG, util.format_binary(packet, 'IN: ')) packet = leftover + packet if self.__mac_size_in > 0: @@ -401,10 +400,8 @@ class Packetizer (object): self._log(DEBUG, 'Read packet <%s>, length %d' % (cmd_name, len(payload))) return cmd, msg - ########## protected - def _log(self, level, msg): if self.__logger is None: return @@ -416,7 +413,7 @@ class Packetizer (object): def _check_keepalive(self): if (not self.__keepalive_interval) or (not self.__block_engine_out) or \ - self.__need_rekey: + self.__need_rekey: # wait till we're encrypting, and not in the middle of rekeying return now = time.time() @@ -424,40 +421,7 @@ class Packetizer (object): self.__keepalive_callback() self.__keepalive_last = now - def _py22_read_all(self, n, out): - while n > 0: - r, w, e = select.select([self.__socket], [], [], 0.1) - if self.__socket not in r: - if self.__closed: - raise EOFError() - self._check_keepalive() - else: - x = self.__socket.recv(n) - if len(x) == 0: - raise EOFError() - out += x - n -= len(x) - return out - - def _py22_read_timeout(self, timeout): - start = time.time() - while True: - r, w, e = select.select([self.__socket], [], [], 0.1) - if self.__socket in r: - x = self.__socket.recv(1) - if len(x) == 0: - raise EOFError() - break - if self.__closed: - raise EOFError() - now = time.time() - if now - start >= timeout: - raise socket.timeout() - return x - def _read_timeout(self, timeout): - if PY22: - return self._py22_read_timeout(timeout) start = time.time() while True: try: @@ -468,8 +432,8 @@ class Packetizer (object): except socket.timeout: pass except EnvironmentError as e: - if ((type(e.args) is tuple) and (len(e.args) > 0) and - (e.args[0] == errno.EINTR)): + if (type(e.args) is tuple and len(e.args) > 0 and + e.args[0] == errno.EINTR): pass else: raise diff --git a/paramiko/pipe.py b/paramiko/pipe.py index 568aca6b..b0cfcf24 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -28,10 +28,9 @@ will trigger as readable in `select `. import sys import os import socket -from paramiko.py3compat import b -def make_pipe (): +def make_pipe(): if sys.platform[:3] != 'win': p = PosixPipe() else: @@ -40,34 +39,34 @@ def make_pipe (): class PosixPipe (object): - def __init__ (self): + def __init__(self): self._rfd, self._wfd = os.pipe() self._set = False self._forever = False self._closed = False - def close (self): + def close(self): os.close(self._rfd) os.close(self._wfd) # used for unit tests: self._closed = True - def fileno (self): + def fileno(self): return self._rfd - def clear (self): + def clear(self): if not self._set or self._forever: return os.read(self._rfd, 1) self._set = False - def set (self): + def set(self): if self._set or self._closed: return self._set = True os.write(self._wfd, b'*') - def set_forever (self): + def set_forever(self): self._forever = True self.set() @@ -77,7 +76,7 @@ class WindowsPipe (object): On Windows, only an OS-level "WinSock" may be used in select(), but reads and writes must be to the actual socket object. """ - def __init__ (self): + def __init__(self): serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.bind(('127.0.0.1', 0)) serv.listen(1) @@ -92,13 +91,13 @@ class WindowsPipe (object): self._forever = False self._closed = False - def close (self): + def close(self): self._rsock.close() self._wsock.close() # used for unit tests: self._closed = True - def fileno (self): + def fileno(self): return self._rsock.fileno() def clear (self): diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 3a38d19d..c8f84e0a 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -27,9 +27,9 @@ import os from Crypto.Hash import MD5 from Crypto.Cipher import DES3, AES -from paramiko.common import * from paramiko import util -from paramiko.message import Message +from paramiko.common import o600, rng, zero_byte +from paramiko.py3compat import u, encodebytes, decodebytes, b from paramiko.ssh_exception import SSHException, PasswordRequiredException @@ -40,11 +40,10 @@ class PKey (object): # known encryption types for private key files: _CIPHER_TABLE = { - 'AES-128-CBC': { 'cipher': AES, 'keysize': 16, 'blocksize': 16, 'mode': AES.MODE_CBC }, - 'DES-EDE3-CBC': { 'cipher': DES3, 'keysize': 24, 'blocksize': 8, 'mode': DES3.MODE_CBC }, + 'AES-128-CBC': {'cipher': AES, 'keysize': 16, 'blocksize': 16, 'mode': AES.MODE_CBC}, + 'DES-EDE3-CBC': {'cipher': DES3, 'keysize': 24, 'blocksize': 8, 'mode': DES3.MODE_CBC}, } - def __init__(self, msg=None, data=None): """ Create a new instance of this public key type. If ``msg`` is given, @@ -73,6 +72,7 @@ class PKey (object): def __str__(self): return self.asbytes() + # noinspection PyUnresolvedReferences def __cmp__(self, other): """ Compare this key to another. Returns 0 if this key is equivalent to @@ -345,7 +345,7 @@ class PKey (object): s = u(encodebytes(data)) # re-wrap to 64-char lines s = ''.join(s.split('\n')) - s = '\n'.join([s[i : i+64] for i in range(0, len(s), 64)]) + s = '\n'.join([s[i: i + 64] for i in range(0, len(s), 64)]) f.write(s) f.write('\n') f.write('-----END %s PRIVATE KEY-----\n' % tag) diff --git a/paramiko/primes.py b/paramiko/primes.py index bf1a6c62..58d158c8 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -23,16 +23,16 @@ Utility functions for dealing with primes. from Crypto.Util import number from paramiko import util +from paramiko.py3compat import byte_mask, long from paramiko.ssh_exception import SSHException -from paramiko.common import * def _generate_prime(bits, rng): - "primtive attempt at prime generation" + """primtive attempt at prime generation""" hbyte_mask = pow(2, bits % 8) - 1 while True: # loop catches the case where we increment n into a higher bit-range - x = rng.read((bits+7) // 8) + x = rng.read((bits + 7) // 8) if hbyte_mask > 0: x = byte_mask(x[0], hbyte_mask) + x[1:] n = util.inflate_long(x, 1) @@ -44,9 +44,10 @@ def _generate_prime(bits, rng): break return n + def _roll_random(rng, n): - "returns a random # from 0 to N-1" - bits = util.bit_length(n-1) + """returns a random # from 0 to N-1""" + bits = util.bit_length(n - 1) byte_count = (bits + 7) // 8 hbyte_mask = pow(2, bits % 8) - 1 @@ -130,7 +131,7 @@ class ModulusPack (object): good = -1 # find nearest bitsize >= preferred for b in bitsizes: - if (b >= prefer) and (b < max) and ((b < good) or (good == -1)): + if (b >= prefer) and (b < max) and (b < good or good == -1): good = b # if that failed, find greatest bitsize >= min if good == -1: diff --git a/paramiko/proxy.py b/paramiko/proxy.py index c7e93efa..8959b244 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -80,7 +80,7 @@ class ProxyCommand(object): while len(self.buffer) < size: if self.timeout is not None: elapsed = (datetime.now() - start).microseconds - timeout = self.timeout * 1000 * 1000 # to microseconds + timeout = self.timeout * 1000 * 1000 # to microseconds if elapsed >= timeout: raise socket.timeout() r, w, x = select([self.process.stdout], [], [], 0.0) @@ -94,7 +94,7 @@ class ProxyCommand(object): self.buffer = [] return result except socket.timeout: - raise # socket.timeout is a subclass of IOError + raise # socket.timeout is a subclass of IOError except IOError as e: raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index 06f0085d..c93f3218 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -21,14 +21,14 @@ RSA keys. """ from Crypto.PublicKey import RSA -from Crypto.Hash import SHA, MD5 -from Crypto.Cipher import DES3 +from Crypto.Hash import SHA -from paramiko.common import * from paramiko import util +from paramiko.common import rng, max_byte, zero_byte, one_byte from paramiko.message import Message from paramiko.ber import BER, BERException from paramiko.pkey import PKey +from paramiko.py3compat import long from paramiko.ssh_exception import SSHException SHA1_DIGESTINFO = b'\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14' @@ -113,9 +113,9 @@ class RSAKey (PKey): def _encode_key(self): if (self.p is None) or (self.q is None): raise SSHException('Not enough key info to write private key file') - keylist = [ 0, self.n, self.e, self.d, self.p, self.q, - self.d % (self.p - 1), self.d % (self.q - 1), - util.mod_inverse(self.q, self.p) ] + keylist = [0, self.n, self.e, self.d, self.p, self.q, + self.d % (self.p - 1), self.d % (self.q - 1), + util.mod_inverse(self.q, self.p)] try: b = BER() b.encode(keylist) @@ -148,10 +148,8 @@ class RSAKey (PKey): return key generate = staticmethod(generate) - ### internals... - def _pkcs1imify(self, data): """ turn a 20-byte SHA1 hash into a blob of data as large as the key's N, diff --git a/paramiko/server.py b/paramiko/server.py index ad0acb94..496cd60c 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -21,8 +21,9 @@ """ import threading -from paramiko.common import * from paramiko import util +from paramiko.common import DEBUG, ERROR, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, AUTH_FAILED +from paramiko.py3compat import string_types class ServerInterface (object): @@ -291,10 +292,8 @@ class ServerInterface (object): """ return False - ### Channel requests - def check_channel_pty_request(self, channel, term, width, height, pixelwidth, pixelheight, modes): """ diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 3e05de9f..f44a804d 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -20,32 +20,31 @@ import select import socket import struct -from paramiko.common import * from paramiko import util -from paramiko.channel import Channel +from paramiko.common import asbytes, DEBUG from paramiko.message import Message +from paramiko.py3compat import byte_chr, byte_ord CMD_INIT, CMD_VERSION, CMD_OPEN, CMD_CLOSE, CMD_READ, CMD_WRITE, CMD_LSTAT, CMD_FSTAT, \ - CMD_SETSTAT, CMD_FSETSTAT, CMD_OPENDIR, CMD_READDIR, CMD_REMOVE, CMD_MKDIR, \ - CMD_RMDIR, CMD_REALPATH, CMD_STAT, CMD_RENAME, CMD_READLINK, CMD_SYMLINK \ - = range(1, 21) + CMD_SETSTAT, CMD_FSETSTAT, CMD_OPENDIR, CMD_READDIR, CMD_REMOVE, CMD_MKDIR, \ + CMD_RMDIR, CMD_REALPATH, CMD_STAT, CMD_RENAME, CMD_READLINK, CMD_SYMLINK = range(1, 21) CMD_STATUS, CMD_HANDLE, CMD_DATA, CMD_NAME, CMD_ATTRS = range(101, 106) CMD_EXTENDED, CMD_EXTENDED_REPLY = range(200, 202) SFTP_OK = 0 SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, SFTP_BAD_MESSAGE, \ - SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED = range(1, 9) - -SFTP_DESC = [ 'Success', - 'End of file', - 'No such file', - 'Permission denied', - 'Failure', - 'Bad message', - 'No connection', - 'Connection lost', - 'Operation unsupported' ] + SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED = range(1, 9) + +SFTP_DESC = ['Success', + 'End of file', + 'No such file', + 'Permission denied', + 'Failure', + 'Bad message', + 'No connection', + 'Connection lost', + 'Operation unsupported'] SFTP_FLAG_READ = 0x1 SFTP_FLAG_WRITE = 0x2 @@ -99,10 +98,8 @@ class BaseSFTP (object): self.sock = None self.ultra_debug = False - ### internals... - def _send_version(self): self._send_packet(CMD_INIT, struct.pack('>I', _VERSION)) t, data = self._read_packet() @@ -121,7 +118,7 @@ class BaseSFTP (object): raise SFTPError('Incompatible sftp protocol') version = struct.unpack('>I', data[:4])[0] # advertise that we support "check-file" - extension_pairs = [ 'check-file', 'md5,sha1' ] + extension_pairs = ['check-file', 'md5,sha1'] msg = Message() msg.add_int(_VERSION) msg.add(*extension_pairs) @@ -151,7 +148,7 @@ class BaseSFTP (object): # return or raise an exception, but calling select on a closed # socket will.) while True: - read, write, err = select.select([ self.sock ], [], [], 0.1) + read, write, err = select.select([self.sock], [], [], 0.1) if len(read) > 0: x = self.sock.recv(n) break @@ -181,7 +178,7 @@ class BaseSFTP (object): size = struct.unpack('>I', x)[0] data = self._read_all(size) if self.ultra_debug: - self._log(DEBUG, util.format_binary(data, 'IN: ')); + self._log(DEBUG, util.format_binary(data, 'IN: ')) if size > 0: t = byte_ord(data[0]) #self._log(DEBUG2, 'read: %s (len=%d)' % (CMD_NAMES.get(t), '0x%02x' % t, len(data)-1)) diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index ffdaa864..d12eff8d 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -18,8 +18,8 @@ import stat import time -from paramiko.common import * -from paramiko.sftp import * +from paramiko.common import x80000000, o700, o70, xffffffff +from paramiko.py3compat import long, b class SFTPAttributes (object): @@ -84,10 +84,8 @@ class SFTPAttributes (object): def __repr__(self): return '' % self._debug_str() - ### internals... - def _from_msg(cls, msg, filename=None, longname=None): attr = cls() attr._unpack(msg) @@ -173,7 +171,7 @@ class SFTPAttributes (object): _rwx = staticmethod(_rwx) def __str__(self): - "create a unix-style long description of the file (like ls -l)" + """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) if kind == stat.S_IFIFO: diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 67558986..ce6fbec6 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -24,8 +24,18 @@ import stat import threading import time import weakref +from paramiko import util +from paramiko.channel import Channel +from paramiko.message import Message +from paramiko.common import INFO, DEBUG, o777 +from paramiko.py3compat import bytestring, b, u, long, string_types, bytes_types +from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, CMD_READDIR, \ + CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, SFTP_FLAG_CREATE, \ + SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, CMD_OPEN, CMD_REMOVE, \ + CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, \ + CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, CMD_REALPATH, CMD_STATUS, SFTP_OK, \ + SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED -from paramiko.sftp import * from paramiko.sftp_attr import SFTPAttributes from paramiko.ssh_exception import SSHException from paramiko.sftp_file import SFTPFile @@ -47,6 +57,7 @@ def _to_unicode(s): b_slash = b'/' + class SFTPClient(BaseSFTP): """ SFTP client object. @@ -106,9 +117,9 @@ class SFTPClient(BaseSFTP): def _log(self, level, msg, *args): if isinstance(msg, list): for m in msg: - super(SFTPClient, self)._log(level, "[chan %s] " + m, *([ self.sock.get_name() ] + list(args))) + super(SFTPClient, self)._log(level, "[chan %s] " + m, *([self.sock.get_name()] + list(args))) else: - super(SFTPClient, self)._log(level, "[chan %s] " + msg, *([ self.sock.get_name() ] + list(args))) + super(SFTPClient, self)._log(level, "[chan %s] " + msg, *([self.sock.get_name()] + list(args))) def close(self): """ @@ -222,11 +233,11 @@ class SFTPClient(BaseSFTP): imode |= SFTP_FLAG_READ if ('w' in mode) or ('+' in mode) or ('a' in mode): imode |= SFTP_FLAG_WRITE - if ('w' in mode): + if 'w' in mode: imode |= SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC - if ('a' in mode): + if 'a' in mode: imode |= SFTP_FLAG_CREATE | SFTP_FLAG_APPEND - if ('x' in mode): + if 'x' in mode: imode |= SFTP_FLAG_CREATE | SFTP_FLAG_EXCL attrblock = SFTPAttributes() t, msg = self._request(CMD_OPEN, filename, imode, attrblock) @@ -629,10 +640,8 @@ class SFTPClient(BaseSFTP): if s.st_size != size: raise IOError('size mismatch in get! %d != %d' % (s.st_size, size)) - ### internals... - def _request(self, t, *arg): num = self._async_request(type(None), t, *arg) return self._read_response(num) @@ -689,7 +698,7 @@ class SFTPClient(BaseSFTP): if waitfor is None: # just doing a single check break - return (None, None) + return None, None def _finish_responses(self, fileobj): while fileobj in self._expecting.values(): diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 8c4decd8..03d67b33 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -27,10 +27,12 @@ from collections import deque import socket import threading import time +from paramiko.common import DEBUG -from paramiko.common import * -from paramiko.sftp import * from paramiko.file import BufferedFile +from paramiko.py3compat import long +from paramiko.sftp import CMD_CLOSE, CMD_READ, CMD_DATA, SFTPError, CMD_WRITE, \ + CMD_STATUS, CMD_FSTAT, CMD_ATTRS, CMD_FSETSTAT, CMD_EXTENDED from paramiko.sftp_attr import SFTPAttributes @@ -437,11 +439,9 @@ class SFTPFile (BufferedFile): for x in chunks: self.seek(x[0]) yield self.read(x[1]) - ### internals... - def _get_size(self): try: return self.stat().st_size @@ -483,7 +483,7 @@ class SFTPFile (BufferedFile): self._prefetch_done = True def _check_exception(self): - "if there's a saved exception, raise & clear it" + """if there's a saved exception, raise & clear it""" if self._saved_exception is not None: x = self._saved_exception self._saved_exception = None diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index 79c0045c..92dd9cfe 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -21,9 +21,7 @@ Abstraction of an SFTP file handle (for server mode). """ import os - -from paramiko.common import * -from paramiko.sftp import * +from paramiko.sftp import SFTP_OP_UNSUPPORTED, SFTP_OK class SFTPHandle (object): @@ -46,7 +44,7 @@ class SFTPHandle (object): self.__flags = flags self.__name = None # only for handles to folders: - self.__files = { } + self.__files = {} self.__tell = None def close(self): @@ -166,10 +164,8 @@ class SFTPHandle (object): """ return SFTP_OP_UNSUPPORTED - ### internals... - def _set_files(self, files): """ Used by the SFTP server code to cache a directory listing. (In diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 1c197dfd..dadfd026 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -24,14 +24,26 @@ import os import errno from Crypto.Hash import MD5, SHA -from paramiko.common import * +import sys +from paramiko import util +from paramiko.sftp import BaseSFTP, Message, SFTP_FAILURE, \ + SFTP_PERMISSION_DENIED, SFTP_NO_SUCH_FILE +from paramiko.sftp_si import SFTPServerInterface +from paramiko.sftp_attr import SFTPAttributes +from paramiko.common import DEBUG +from paramiko.py3compat import long, string_types, bytes_types, b from paramiko.server import SubsystemHandler -from paramiko.sftp import * -from paramiko.sftp_si import * -from paramiko.sftp_attr import * # known hash algorithms for the "check-file" extension +from paramiko.sftp import CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, CMD_NAME, \ + SFTP_BAD_MESSAGE, CMD_EXTENDED_REPLY, SFTP_FLAG_READ, SFTP_FLAG_WRITE, \ + SFTP_FLAG_APPEND, SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_EXCL, \ + CMD_NAMES, CMD_OPEN, CMD_CLOSE, SFTP_OK, CMD_READ, CMD_DATA, CMD_WRITE, \ + CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_OPENDIR, CMD_READDIR, \ + CMD_STAT, CMD_ATTRS, CMD_LSTAT, CMD_FSTAT, CMD_SETSTAT, CMD_FSETSTAT, \ + CMD_READLINK, CMD_SYMLINK, CMD_REALPATH, CMD_EXTENDED, SFTP_OP_UNSUPPORTED + _hash_class = { 'sha1': SHA, 'md5': MD5, @@ -67,8 +79,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self.ultra_debug = transport.get_hexdump() self.next_handle = 1 # map of handle-string to SFTPHandle for files & folders: - self.file_table = { } - self.folder_table = { } + self.file_table = {} + self.folder_table = {} self.server = sftp_si(server, *largs, **kwargs) def _log(self, level, msg): @@ -163,10 +175,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): f.truncate(attr.st_size) set_file_attr = staticmethod(set_file_attr) - ### internals... - def _response(self, request_number, t, *arg): msg = Message() msg.add_int(request_number) @@ -290,7 +300,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self._send_packet(CMD_EXTENDED_REPLY, msg) def _convert_pflags(self, pflags): - "convert SFTP-style open() flags to Python's os.open() flags" + """convert SFTP-style open() flags to Python's os.open() flags""" if (pflags & SFTP_FLAG_READ) and (pflags & SFTP_FLAG_WRITE): flags = os.O_RDWR elif pflags & SFTP_FLAG_WRITE: diff --git a/paramiko/sftp_si.py b/paramiko/sftp_si.py index 3786be4e..61db956c 100644 --- a/paramiko/sftp_si.py +++ b/paramiko/sftp_si.py @@ -21,9 +21,8 @@ An interface to override for SFTP server support. """ import os - -from paramiko.common import * -from paramiko.sftp import * +import sys +from paramiko.sftp import SFTP_OP_UNSUPPORTED class SFTPServerInterface (object): @@ -41,7 +40,7 @@ class SFTPServerInterface (object): clients & servers obey the requirement that paths be encoded in UTF-8. """ - def __init__ (self, server, *largs, **kwargs): + def __init__(self, server, *largs, **kwargs): """ Create a new SFTPServerInterface object. This method does nothing by default and is meant to be overridden by subclasses. diff --git a/paramiko/transport.py b/paramiko/transport.py index 092ab1c6..1471b543 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -20,10 +20,7 @@ Core protocol implementation """ -import os import socket -import string -import struct import sys import threading import time @@ -33,7 +30,17 @@ import paramiko from paramiko import util from paramiko.auth_handler import AuthHandler from paramiko.channel import Channel -from paramiko.common import * # Legit, uses dozens of constants & funcs +from paramiko.common import rng, xffffffff, cMSG_CHANNEL_OPEN, cMSG_IGNORE, \ + cMSG_GLOBAL_REQUEST, DEBUG, MSG_KEXINIT, MSG_IGNORE, MSG_DISCONNECT, \ + MSG_DEBUG, ERROR, WARNING, cMSG_UNIMPLEMENTED, INFO, cMSG_KEXINIT, \ + cMSG_NEWKEYS, MSG_NEWKEYS, cMSG_REQUEST_SUCCESS, cMSG_REQUEST_FAILURE, \ + CONNECTION_FAILED_CODE, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, \ + OPEN_SUCCEEDED, cMSG_CHANNEL_OPEN_FAILURE, cMSG_CHANNEL_OPEN_SUCCESS, \ + MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE, \ + MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, MSG_CHANNEL_OPEN, \ + MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE, MSG_CHANNEL_DATA, \ + MSG_CHANNEL_EXTENDED_DATA, MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_REQUEST, \ + MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE from paramiko.compress import ZlibCompressor, ZlibDecompressor from paramiko.dsskey import DSSKey from paramiko.kex_gex import KexGex @@ -41,12 +48,13 @@ from paramiko.kex_group1 import KexGroup1 from paramiko.message import Message from paramiko.packet import Packetizer, NeedRekeyException from paramiko.primes import ModulusPack +from paramiko.py3compat import string_types, long, byte_ord, b from paramiko.rsakey import RSAKey from paramiko.ecdsakey import ECDSAKey from paramiko.server import ServerInterface from paramiko.sftp_client import SFTPClient from paramiko.ssh_exception import (SSHException, BadAuthenticationType, - ChannelException, ProxyCommandFailure) + ChannelException, ProxyCommandFailure) from paramiko.util import retry_on_signal from Crypto import Random @@ -60,9 +68,11 @@ except ImportError: # for thread cleanup _active_threads = [] + def _join_lingering_threads(): for thr in _active_threads: thr.stop_thread() + import atexit atexit.register(_join_lingering_threads) @@ -76,54 +86,53 @@ class Transport (threading.Thread): forwardings). """ _PROTO_ID = '2.0' - _CLIENT_ID = 'paramiko_%s' % (paramiko.__version__) + _CLIENT_ID = 'paramiko_%s' % paramiko.__version__ - _preferred_ciphers = ( 'aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'aes256-cbc', '3des-cbc', - 'arcfour128', 'arcfour256' ) - _preferred_macs = ( 'hmac-sha1', 'hmac-md5', 'hmac-sha1-96', 'hmac-md5-96' ) - _preferred_keys = ( 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256' ) - _preferred_kex = ( 'diffie-hellman-group1-sha1', 'diffie-hellman-group-exchange-sha1' ) - _preferred_compression = ( 'none', ) + _preferred_ciphers = ('aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', + 'aes256-cbc', '3des-cbc', 'arcfour128', 'arcfour256') + _preferred_macs = ('hmac-sha1', 'hmac-md5', 'hmac-sha1-96', 'hmac-md5-96') + _preferred_keys = ('ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256') + _preferred_kex = ('diffie-hellman-group1-sha1', 'diffie-hellman-group-exchange-sha1') + _preferred_compression = ('none',) _cipher_info = { - 'aes128-ctr': { 'class': AES, 'mode': AES.MODE_CTR, 'block-size': 16, 'key-size': 16 }, - 'aes256-ctr': { 'class': AES, 'mode': AES.MODE_CTR, 'block-size': 16, 'key-size': 32 }, - 'blowfish-cbc': { 'class': Blowfish, 'mode': Blowfish.MODE_CBC, 'block-size': 8, 'key-size': 16 }, - 'aes128-cbc': { 'class': AES, 'mode': AES.MODE_CBC, 'block-size': 16, 'key-size': 16 }, - 'aes256-cbc': { 'class': AES, 'mode': AES.MODE_CBC, 'block-size': 16, 'key-size': 32 }, - '3des-cbc': { 'class': DES3, 'mode': DES3.MODE_CBC, 'block-size': 8, 'key-size': 24 }, - 'arcfour128': { 'class': ARC4, 'mode': None, 'block-size': 8, 'key-size': 16 }, - 'arcfour256': { 'class': ARC4, 'mode': None, 'block-size': 8, 'key-size': 32 }, - } + 'aes128-ctr': {'class': AES, 'mode': AES.MODE_CTR, 'block-size': 16, 'key-size': 16}, + 'aes256-ctr': {'class': AES, 'mode': AES.MODE_CTR, 'block-size': 16, 'key-size': 32}, + 'blowfish-cbc': {'class': Blowfish, 'mode': Blowfish.MODE_CBC, 'block-size': 8, 'key-size': 16}, + 'aes128-cbc': {'class': AES, 'mode': AES.MODE_CBC, 'block-size': 16, 'key-size': 16}, + 'aes256-cbc': {'class': AES, 'mode': AES.MODE_CBC, 'block-size': 16, 'key-size': 32}, + '3des-cbc': {'class': DES3, 'mode': DES3.MODE_CBC, 'block-size': 8, 'key-size': 24}, + 'arcfour128': {'class': ARC4, 'mode': None, 'block-size': 8, 'key-size': 16}, + 'arcfour256': {'class': ARC4, 'mode': None, 'block-size': 8, 'key-size': 32}, + } _mac_info = { - 'hmac-sha1': { 'class': SHA, 'size': 20 }, - 'hmac-sha1-96': { 'class': SHA, 'size': 12 }, - 'hmac-md5': { 'class': MD5, 'size': 16 }, - 'hmac-md5-96': { 'class': MD5, 'size': 12 }, - } + 'hmac-sha1': {'class': SHA, 'size': 20}, + 'hmac-sha1-96': {'class': SHA, 'size': 12}, + 'hmac-md5': {'class': MD5, 'size': 16}, + 'hmac-md5-96': {'class': MD5, 'size': 12}, + } _key_info = { 'ssh-rsa': RSAKey, 'ssh-dss': DSSKey, 'ecdsa-sha2-nistp256': ECDSAKey, - } + } _kex_info = { 'diffie-hellman-group1-sha1': KexGroup1, 'diffie-hellman-group-exchange-sha1': KexGex, - } + } _compression_info = { # zlib@openssh.com is just zlib, but only turned on after a successful # authentication. openssh servers may only offer this type because # they've had troubles with security holes in zlib in the past. - 'zlib@openssh.com': ( ZlibCompressor, ZlibDecompressor ), - 'zlib': ( ZlibCompressor, ZlibDecompressor ), - 'none': ( None, None ), + 'zlib@openssh.com': (ZlibCompressor, ZlibDecompressor), + 'zlib': (ZlibCompressor, ZlibDecompressor), + 'none': (None, None), } - _modulus_pack = None def __init__(self, sock): @@ -220,8 +229,8 @@ class Transport (threading.Thread): # tracking open channels self._channels = ChannelMap() - self.channel_events = { } # (id -> Event) - self.channels_seen = { } # (id -> True) + self.channel_events = {} # (id -> Event) + self.channels_seen = {} # (id -> True) self._channel_counter = 1 self.window_size = 65536 self.max_packet_size = 34816 @@ -244,10 +253,10 @@ class Transport (threading.Thread): # server mode: self.server_mode = False self.server_object = None - self.server_key_dict = { } - self.server_accepts = [ ] + self.server_key_dict = {} + self.server_accepts = [] self.server_accept_cv = threading.Condition(self.lock) - self.subsystem_table = { } + self.subsystem_table = {} def __repr__(self): """ @@ -468,7 +477,7 @@ class Transport (threading.Thread): """ Transport._modulus_pack = ModulusPack(rng) # places to look for the openssh "moduli" file - file_list = [ '/etc/ssh/moduli', '/usr/local/etc/moduli' ] + file_list = ['/etc/ssh/moduli', '/usr/local/etc/moduli'] if filename is not None: file_list.insert(0, filename) for fn in file_list: @@ -623,7 +632,7 @@ class Transport (threading.Thread): self.lock.release() self._send_user_message(m) while True: - event.wait(0.1); + event.wait(0.1) if not self.active: e = self.get_exception() if e is None: @@ -764,7 +773,7 @@ class Transport (threading.Thread): 0 to disable keepalives). """ self.packetizer.set_keepalive(interval, - lambda x=weakref.proxy(self): x.global_request('keepalive@lag.net', wait=False)) + lambda x=weakref.proxy(self): x.global_request('keepalive@lag.net', wait=False)) def global_request(self, kind, data=None, wait=True): """ @@ -863,12 +872,12 @@ class Transport (threading.Thread): supplied by the server is incorrect, or authentication fails. """ if hostkey is not None: - self._preferred_keys = [ hostkey.get_name() ] + self._preferred_keys = [hostkey.get_name()] self.start_client() # check host key if we were given one - if (hostkey is not None): + if hostkey is not None: key = self.get_remote_server_key() if (key.get_name() != hostkey.get_name()) or (key.asbytes() != hostkey.asbytes()): self._log(DEBUG, 'Bad host key from server') @@ -1061,12 +1070,11 @@ class Transport (threading.Thread): # to try to fake out automated scripting of the exact # type we're doing here. *shrug* :) return [] - return [ password ] + return [password] return self.auth_interactive(username, handler) except SSHException: # attempt failed; just raise the original exception raise e - return None def auth_publickey(self, username, key, event=None): """ @@ -1227,9 +1235,9 @@ class Transport (threading.Thread): .. versionadded:: 1.5.2 """ if compress: - self._preferred_compression = ( 'zlib@openssh.com', 'zlib', 'none' ) + self._preferred_compression = ('zlib@openssh.com', 'zlib', 'none') else: - self._preferred_compression = ( 'none', ) + self._preferred_compression = ('none',) def getpeername(self): """ @@ -1244,7 +1252,7 @@ class Transport (threading.Thread): """ gp = getattr(self.sock, 'getpeername', None) if gp is None: - return ('unknown', 0) + return 'unknown', 0 return gp() def stop_thread(self): @@ -1253,10 +1261,8 @@ class Transport (threading.Thread): while self.isAlive(): self.join(10) - ### internals... - def _log(self, level, msg, *args): if issubclass(type(msg), list): for m in msg: @@ -1265,11 +1271,11 @@ class Transport (threading.Thread): self.logger.log(level, msg, *args) def _get_modulus_pack(self): - "used by KexGex to find primes for group exchange" + """used by KexGex to find primes for group exchange""" return self._modulus_pack def _next_channel(self): - "you are holding the lock" + """you are holding the lock""" chanid = self._channel_counter while self._channels.get(chanid) is not None: self._channel_counter = (self._channel_counter + 1) & 0xffffff @@ -1278,7 +1284,7 @@ class Transport (threading.Thread): return chanid def _unlink_channel(self, chanid): - "used by a Channel to remove itself from the active channel list" + """used by a Channel to remove itself from the active channel list""" self._channels.delete(chanid) def _send_message(self, data): @@ -1307,14 +1313,14 @@ class Transport (threading.Thread): self.clear_to_send_lock.release() def _set_K_H(self, k, h): - "used by a kex object to set the K (root key) and H (exchange hash)" + """used by a kex object to set the K (root key) and H (exchange hash)""" self.K = k self.H = h - if self.session_id == None: + if self.session_id is None: self.session_id = h def _expect_packet(self, *ptypes): - "used by a kex object to register the next packet type it expects to see" + """used by a kex object to register the next packet type it expects to see""" self._expected_packet = tuple(ptypes) def _verify_key(self, host_key, sig): @@ -1326,7 +1332,7 @@ class Transport (threading.Thread): self.host_key = key def _compute_key(self, id, nbytes): - "id is 'A' - 'F' for the various keys used by ssh" + """id is 'A' - 'F' for the various keys used by ssh""" m = Message() m.add_mpint(self.K) m.add_bytes(self.H) @@ -1471,7 +1477,7 @@ class Transport (threading.Thread): if type(e.args) is tuple: if e.args: emsg = '%s (%d)' % (e.args[1], e.args[0]) - else: # empty tuple, e.g. socket.timeout + else: # empty tuple, e.g. socket.timeout emsg = str(e) or repr(e) else: emsg = e.args @@ -1487,7 +1493,7 @@ class Transport (threading.Thread): if self.active: self.active = False self.packetizer.close() - if self.completion_event != None: + if self.completion_event is not None: self.completion_event.set() if self.auth_handler is not None: self.auth_handler.abort() @@ -1507,10 +1513,8 @@ class Transport (threading.Thread): if self.sys.modules is not None: raise - ### protocol stages - def _negotiate_keys(self, m): # throws SSHException on anything unusual self.clear_to_send_lock.acquire() @@ -1518,7 +1522,7 @@ class Transport (threading.Thread): self.clear_to_send.clear() finally: self.clear_to_send_lock.release() - if self.local_kex_init == None: + if self.local_kex_init is None: # remote side wants to renegotiate self._send_kex_init() self._parse_kex_init(m) @@ -1580,7 +1584,7 @@ class Transport (threading.Thread): pkex.remove('diffie-hellman-group-exchange-sha1') self.get_security_options().kex = pkex available_server_keys = list(filter(list(self.server_key_dict.keys()).__contains__, - self._preferred_keys)) + self._preferred_keys)) else: available_server_keys = self._preferred_keys @@ -1618,15 +1622,15 @@ class Transport (threading.Thread): kex_follows = m.get_boolean() unused = m.get_int() - self._log(DEBUG, 'kex algos:' + str(kex_algo_list) + ' server key:' + str(server_key_algo_list) + \ - ' client encrypt:' + str(client_encrypt_algo_list) + \ - ' server encrypt:' + str(server_encrypt_algo_list) + \ - ' client mac:' + str(client_mac_algo_list) + \ - ' server mac:' + str(server_mac_algo_list) + \ - ' client compress:' + str(client_compress_algo_list) + \ - ' server compress:' + str(server_compress_algo_list) + \ - ' client lang:' + str(client_lang_list) + \ - ' server lang:' + str(server_lang_list) + \ + self._log(DEBUG, 'kex algos:' + str(kex_algo_list) + ' server key:' + str(server_key_algo_list) + + ' client encrypt:' + str(client_encrypt_algo_list) + + ' server encrypt:' + str(server_encrypt_algo_list) + + ' client mac:' + str(client_mac_algo_list) + + ' server mac:' + str(server_mac_algo_list) + + ' client compress:' + str(client_compress_algo_list) + + ' server compress:' + str(server_compress_algo_list) + + ' client lang:' + str(client_lang_list) + + ' server lang:' + str(server_lang_list) + ' kex follows?' + str(kex_follows)) # as a server, we pick the first item in the client's list that we support. @@ -1641,7 +1645,7 @@ class Transport (threading.Thread): if self.server_mode: available_server_keys = list(filter(list(self.server_key_dict.keys()).__contains__, - self._preferred_keys)) + self._preferred_keys)) agreed_keys = list(filter(available_server_keys.__contains__, server_key_algo_list)) else: agreed_keys = list(filter(server_key_algo_list.__contains__, self._preferred_keys)) @@ -1701,7 +1705,7 @@ class Transport (threading.Thread): self.remote_kex_init = cMSG_KEXINIT + m.get_so_far() def _activate_inbound(self): - "switch on newly negotiated encryption parameters for inbound traffic" + """switch on newly negotiated encryption parameters for inbound traffic""" block_size = self._cipher_info[self.remote_cipher]['block-size'] if self.server_mode: IV_in = self._compute_key('A', block_size) @@ -1725,7 +1729,7 @@ class Transport (threading.Thread): self.packetizer.set_inbound_compressor(compress_in()) def _activate_outbound(self): - "switch on newly negotiated encryption parameters for outbound traffic" + """switch on newly negotiated encryption parameters for outbound traffic""" m = Message() m.add_byte(cMSG_NEWKEYS) self._send_message(m) @@ -1782,7 +1786,7 @@ class Transport (threading.Thread): # this was the first key exchange self.initial_kex_done = True # send an event? - if self.completion_event != None: + if self.completion_event is not None: self.completion_event.set() # it's now okay to send data again (if this was a re-key) if not self.packetizer.need_rekey(): @@ -1810,7 +1814,7 @@ class Transport (threading.Thread): address = m.get_text() port = m.get_int() ok = self.server_object.check_port_forward_request(address, port) - if ok != False: + if ok: ok = (ok,) elif kind == 'cancel-tcpip-forward': address = m.get_text() @@ -1933,8 +1937,7 @@ class Transport (threading.Thread): origin_addr = m.get_text() origin_port = m.get_int() reason = self.server_object.check_channel_direct_tcpip_request( - my_chanid, (origin_addr, origin_port), - (dest_addr, dest_port)) + my_chanid, (origin_addr, origin_port), (dest_addr, dest_port)) else: reason = self.server_object.check_channel_request(kind, my_chanid) if reason != OPEN_SUCCEEDED: @@ -1988,7 +1991,7 @@ class Transport (threading.Thread): try: self.lock.acquire() if name not in self.subsystem_table: - return (None, [], {}) + return None, [], {} return self.subsystem_table[name] finally: self.lock.release() @@ -2002,7 +2005,7 @@ class Transport (threading.Thread): MSG_CHANNEL_OPEN_FAILURE: _parse_channel_open_failure, MSG_CHANNEL_OPEN: _parse_channel_open, MSG_KEXINIT: _negotiate_keys, - } + } _channel_handler_table = { MSG_CHANNEL_SUCCESS: Channel._request_success, @@ -2013,7 +2016,7 @@ class Transport (threading.Thread): MSG_CHANNEL_REQUEST: Channel._handle_request, MSG_CHANNEL_EOF: Channel._handle_eof, MSG_CHANNEL_CLOSE: Channel._handle_close, - } + } class SecurityOptions (object): diff --git a/paramiko/util.py b/paramiko/util.py index 93998ff7..d7023bcc 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -29,8 +29,10 @@ import sys import struct import traceback import threading +import logging -from paramiko.common import PY2, DEBUG, long, zero_byte, byte_ord, xffffffff, logging, b, max_byte +from paramiko.common import DEBUG, zero_byte, xffffffff, max_byte +from paramiko.py3compat import PY2, long, byte_ord, b, byte_chr from paramiko.config import SSHConfig @@ -307,6 +309,7 @@ def constant_time_bytes_eq(a, b): if len(a) != len(b): return False res = 0 + # noinspection PyUnresolvedReferences for i in (xrange if PY2 else range)(len(a)): res |= byte_ord(a[i]) ^ byte_ord(b[i]) return res == 0 diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 4fbcead8..47644433 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -21,7 +21,6 @@ A stub SFTP server for loopback SFTP testing. """ import os -import sys from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED from paramiko.common import o666 @@ -64,7 +63,7 @@ class StubSFTPServer (SFTPServerInterface): def list_folder(self, path): path = self._realpath(path) try: - out = [ ] + out = [] flist = os.listdir(path) for fname in flist: attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname))) @@ -91,7 +90,7 @@ class StubSFTPServer (SFTPServerInterface): def open(self, path, flags, attr): path = self._realpath(path) try: - binary_flag = getattr(os, 'O_BINARY', 0) + binary_flag = getattr(os, 'O_BINARY', 0) flags |= binary_flag mode = getattr(attr, 'st_mode', None) if mode is not None: diff --git a/tests/test_auth.py b/tests/test_auth.py index d26b1807..1d972d53 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -25,10 +25,9 @@ import threading import unittest from paramiko import Transport, ServerInterface, RSAKey, DSSKey, \ - SSHException, BadAuthenticationType, InteractiveQuery, ChannelException, \ + BadAuthenticationType, InteractiveQuery, \ AuthenticationException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL -from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED from paramiko.py3compat import u from tests.loop import LoopSocket from tests.util import test_path diff --git a/tests/test_buffered_pipe.py b/tests/test_buffered_pipe.py index b9d2bef4..a53081a9 100644 --- a/tests/test_buffered_pipe.py +++ b/tests/test_buffered_pipe.py @@ -22,24 +22,22 @@ Some unit tests for BufferedPipe. import threading import time -import unittest from paramiko.buffered_pipe import BufferedPipe, PipeTimeout from paramiko import pipe -from paramiko.py3compat import b from tests.util import ParamikoTest -def delay_thread(pipe): - pipe.feed('a') +def delay_thread(p): + p.feed('a') time.sleep(0.5) - pipe.feed('b') - pipe.close() + p.feed('b') + p.close() -def close_thread(pipe): +def close_thread(p): time.sleep(0.2) - pipe.close() + p.close() class BufferedPipeTest(ParamikoTest): @@ -91,4 +89,3 @@ class BufferedPipeTest(ParamikoTest): self.assertTrue(p._set) p2.clear() self.assertFalse(p._set) - diff --git a/tests/test_client.py b/tests/test_client.py index af292c32..7e5c80b4 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -158,7 +158,7 @@ class SSHClientTest (unittest.TestCase): self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) - self.tc.connect(self.addr, self.port, username='slowdive', key_filename=[ test_path('test_rsa.key'), test_path('test_dss.key') ]) + self.tc.connect(self.addr, self.port, username='slowdive', key_filename=[test_path('test_rsa.key'), test_path('test_dss.key')]) self.event.wait(1.0) self.assertTrue(self.event.isSet()) diff --git a/tests/test_hostkeys.py b/tests/test_hostkeys.py index 9a7e3689..0ee1bbf0 100644 --- a/tests/test_hostkeys.py +++ b/tests/test_hostkeys.py @@ -20,12 +20,11 @@ Some unit tests for HostKeys. """ -import base64 from binascii import hexlify import os import unittest import paramiko -from paramiko.py3compat import b, decodebytes +from paramiko.py3compat import decodebytes test_hosts_file = """\ diff --git a/tests/test_kex.py b/tests/test_kex.py index 4286d6e8..c522be46 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -37,8 +37,10 @@ class FakeRng (object): class FakeKey (object): def __str__(self): return 'fake-key' + def asbytes(self): return b'fake-key' + def sign_ssh_data(self, rng, H): return b'fake-sig' @@ -46,6 +48,7 @@ class FakeKey (object): class FakeModulusPack (object): P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF G = 2 + def get_modulus(self, min, ask, max): return self.G, self.P @@ -59,19 +62,26 @@ class FakeTransport (object): def _send_message(self, m): self._message = m + def _expect_packet(self, *t): self._expect = t + def _set_K_H(self, K, H): self._K = K self._H = H + def _verify_key(self, host_key, sig): self._verify = (host_key, sig) + def _activate_outbound(self): self._activated = True + def _log(self, level, s): pass + def get_server_key(self): return FakeKey() + def _get_modulus_pack(self): return FakeModulusPack() diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 09d7fcc3..d4d5544e 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -23,7 +23,7 @@ Some unit tests for the ssh2 protocol in Transport. import unittest from tests.loop import LoopSocket from Crypto.Cipher import AES -from Crypto.Hash import SHA, HMAC +from Crypto.Hash import SHA from paramiko import Message, Packetizer, util from paramiko.common import byte_chr, zero_byte @@ -33,7 +33,7 @@ x1f = byte_chr(0x1f) class PacketizerTest (unittest.TestCase): - def test_1_write (self): + def test_1_write(self): rsock = LoopSocket() wsock = LoopSocket() rsock.link(wsock) @@ -56,7 +56,7 @@ class PacketizerTest (unittest.TestCase): self.assertEqual(44, len(data)) self.assertEqual(b'\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) - def test_2_read (self): + def test_2_read(self): rsock = LoopSocket() wsock = LoopSocket() rsock.link(wsock) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 2e565a5f..6ff68fc2 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -23,7 +23,8 @@ Some unit tests for public/private key objects. from binascii import hexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng, StringIO, byte_chr, b, bytes +from paramiko.py3compat import StringIO, byte_chr, b, bytes +from paramiko.common import rng from tests.util import test_path # from openssh's ssh-keygen diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 82422019..6417ac90 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -32,7 +32,8 @@ import unittest from tempfile import mkstemp import paramiko -from paramiko.common import PY2, b, u, StringIO, o777, o600 +from paramiko.py3compat import PY2, b, u, StringIO +from paramiko.common import o777, o600, o666, o644 from tests.stub_sftp import StubServer, StubSFTPServer from tests.loop import LoopSocket from tests.util import test_path @@ -554,6 +555,7 @@ class SFTPTest (unittest.TestCase): with open(localname, 'wb') as f: f.write(text) saved_progress = [] + def progress_callback(x, y): saved_progress.append((x, y)) sftp.put(localname, FOLDER + '/bunny.txt', progress_callback) @@ -663,6 +665,7 @@ class SFTPTest (unittest.TestCase): with open(localname, 'w') as f: f.write(text) saved_progress = [] + def progress_callback(x, y): saved_progress.append((x, y)) res = sftp.put(localname, FOLDER + '/bunny.txt', progress_callback, False) diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index b1b13d58..521fbdc8 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -23,19 +23,14 @@ a real actual sftp server is contacted, and a new folder is created there to do test file operations in (so no existing files will be harmed). """ -import logging import os import random import struct import sys -import threading import time import unittest -import paramiko from paramiko.common import o660 -from tests.stub_sftp import StubServer, StubSFTPServer -from tests.loop import LoopSocket from tests.test_sftp import get_sftp FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') diff --git a/tests/test_transport.py b/tests/test_transport.py index 876759c8..485a18e8 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -23,17 +23,16 @@ Some unit tests for the ssh2 protocol in Transport. from binascii import hexlify import select import socket -import sys import time import threading -import unittest import random from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, \ - SSHException, BadAuthenticationType, InteractiveQuery, ChannelException -from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL + SSHException, ChannelException +from paramiko import AUTH_FAILED, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST, b, bytes +from paramiko.common import MSG_KEXINIT, cMSG_CHANNEL_WINDOW_ADJUST +from paramiko.py3compat import bytes from paramiko.message import Message from tests.loop import LoopSocket from tests.util import ParamikoTest, test_path diff --git a/tests/test_util.py b/tests/test_util.py index 4f85c391..6bde4045 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -23,11 +23,10 @@ Some unit tests for utility functions. from binascii import hexlify import errno import os -import unittest from Crypto.Hash import SHA import paramiko.util from paramiko.util import lookup_ssh_host_config as host_config -from paramiko.py3compat import StringIO, byte_ord, b +from paramiko.py3compat import StringIO, byte_ord from tests.util import ParamikoTest @@ -338,4 +337,4 @@ AddressFamily inet IdentityFile something_%l_using_fqdn """ config = paramiko.util.parse_ssh_config(StringIO(test_config)) - assert config.lookup('meh') # will die during lookup() if bug regresses + assert config.lookup('meh') # will die during lookup() if bug regresses -- cgit v1.2.3