diff options
-rwxr-xr-x | demos/demo.py | 6 | ||||
-rw-r--r-- | demos/demo_server.py | 9 | ||||
-rwxr-xr-x | demos/demo_sftp.py | 3 | ||||
-rwxr-xr-x | demos/demo_simple.py | 3 | ||||
-rw-r--r-- | demos/forward.py | 6 | ||||
-rwxr-xr-x | demos/rforward.py | 6 | ||||
-rw-r--r-- | paramiko/auth_handler.py | 3 | ||||
-rw-r--r-- | paramiko/client.py | 16 | ||||
-rw-r--r-- | paramiko/dsskey.py | 4 | ||||
-rw-r--r-- | paramiko/hostkeys.py | 2 | ||||
-rw-r--r-- | paramiko/packet.py | 9 | ||||
-rw-r--r-- | paramiko/pkey.py | 4 | ||||
-rw-r--r-- | paramiko/proxy.py | 6 | ||||
-rw-r--r-- | paramiko/server.py | 3 | ||||
-rw-r--r-- | paramiko/sftp_client.py | 4 | ||||
-rw-r--r-- | paramiko/sftp_file.py | 4 | ||||
-rw-r--r-- | paramiko/sftp_handle.py | 6 | ||||
-rw-r--r-- | paramiko/sftp_server.py | 6 | ||||
-rw-r--r-- | paramiko/transport.py | 28 | ||||
-rw-r--r-- | paramiko/util.py | 3 | ||||
-rw-r--r-- | tests/stub_sftp.py | 42 | ||||
-rwxr-xr-x | tests/test_sftp.py | 3 | ||||
-rw-r--r-- | tests/test_transport.py | 5 |
23 files changed, 68 insertions, 113 deletions
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 = '<error>' 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): """ |