From b1e235d820805c431966e2b29f3b0a99d445d7e4 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 13 Aug 2013 15:57:38 -0400 Subject: [PATCH] [Python 3]: New except syntax. --- demos/demo.py | 4 ++-- demos/demo_server.py | 8 ++++---- demos/demo_sftp.py | 2 +- demos/demo_simple.py | 2 +- demos/forward.py | 4 ++-- demos/rforward.py | 4 ++-- paramiko/auth_handler.py | 2 +- paramiko/channel.py | 4 ++-- paramiko/client.py | 12 ++++++------ paramiko/dsskey.py | 2 +- paramiko/hostkeys.py | 2 +- paramiko/packet.py | 6 +++--- paramiko/pkey.py | 2 +- paramiko/proxy.py | 4 ++-- paramiko/server.py | 2 +- paramiko/sftp_client.py | 6 +++--- paramiko/sftp_file.py | 2 +- paramiko/sftp_handle.py | 4 ++-- paramiko/sftp_server.py | 4 ++-- paramiko/transport.py | 16 ++++++++-------- paramiko/util.py | 2 +- tests/stub_sftp.py | 28 ++++++++++++++-------------- tests/test_sftp.py | 2 +- tests/test_transport.py | 4 ++-- 24 files changed, 64 insertions(+), 64 deletions(-) diff --git a/demos/demo.py b/demos/demo.py index ac9af0b..1859de5 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -110,7 +110,7 @@ if hostname.find(':') >= 0: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) -except Exception, e: +except Exception as e: print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -168,7 +168,7 @@ try: chan.close() t.close() -except Exception, e: +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 1e0bead..640e3d1 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -85,7 +85,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: +except Exception as e: print('*** Bind failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -94,7 +94,7 @@ try: sock.listen(100) print('Listening for connection ...') client, addr = sock.accept() -except Exception, e: +except Exception as e: print('*** Listen/accept failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -112,7 +112,7 @@ try: server = Server() try: t.start_server(server=server) - except paramiko.SSHException, x: + except paramiko.SSHException as x: print('*** SSH negotiation failed.') sys.exit(1) @@ -137,7 +137,7 @@ try: chan.send('\r\nI don\'t like you, ' + username + '.\r\n') chan.close() -except Exception, e: +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 c7a9c71..29d78d0 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -111,7 +111,7 @@ try: t.close() -except Exception, e: +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 452ffa0..148c777 100755 --- a/demos/demo_simple.py +++ b/demos/demo_simple.py @@ -75,7 +75,7 @@ try: chan.close() client.close() -except Exception, e: +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 5f84fa2..0648c41 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -56,7 +56,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: + except Exception as e: verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) @@ -165,7 +165,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: + 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 0d0fbef..49e1547 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -48,7 +48,7 @@ def handler(chan, host, port): sock = socket.socket() try: sock.connect((host, port)) - except Exception, e: + except Exception as e: verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) return @@ -152,7 +152,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: + 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 e3bd82d..c7bb58b 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -308,7 +308,7 @@ class AuthHandler (object): keyblob = m.get_string() try: key = self.transport._key_info[keytype](Message(keyblob)) - except SSHException, e: + except SSHException as e: 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 0c603c6..9250a0a 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 as e: 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 as e: raise socket.timeout() ack = self._check_add_window(len(out)) diff --git a/paramiko/client.py b/paramiko/client.py index 493d548..002c04c 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -452,7 +452,7 @@ class SSHClient (object): two_factor = (allowed_types == ['password']) if not two_factor: return - except SSHException, e: + except SSHException as e: saved_exception = e if not two_factor: @@ -466,7 +466,7 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: + except SSHException as e: saved_exception = e if not two_factor and allow_agent: @@ -482,7 +482,7 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: + except SSHException as e: saved_exception = e if not two_factor: @@ -514,16 +514,16 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: + except SSHException as e: saved_exception = e - except IOError, e: + except IOError as e: saved_exception = e if password is not None: try: self._transport.auth_password(username, password) return - except SSHException, e: + except SSHException as e: saved_exception = e elif two_factor: raise SSHException('Two-factor authentication requires a password') diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index 53ca92b..5b3e642 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -184,7 +184,7 @@ class DSSKey (PKey): # DSAPrivateKey = { version = 0, p, q, g, y, x } try: keylist = BER(data).decode() - except BERException, x: + except BERException as x: raise SSHException('Unable to parse key file: ' + str(x)) 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)') diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index f64e6e6..424ed4f 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -84,7 +84,7 @@ class HostKeyEntry: else: log.info("Unable to handle key of type %s" % (keytype,)) return None - except binascii.Error, e: + except binascii.Error as e: raise InvalidHostKey(line, e) return cls(names, key) diff --git a/paramiko/packet.py b/paramiko/packet.py index 38a6d4b..ad92733 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -219,7 +219,7 @@ class Packetizer (object): n -= len(x) except socket.timeout: got_timeout = True - except socket.error, e: + 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. @@ -248,7 +248,7 @@ class Packetizer (object): n = self.__socket.send(out) except socket.timeout: retry_write = True - except socket.error, e: + 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): @@ -473,7 +473,7 @@ class Packetizer (object): break except socket.timeout: pass - except EnvironmentError, e: + 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 3e71222..4a0653d 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -304,7 +304,7 @@ 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: + except base64.binascii.Error as e: raise SSHException('base64 decoding error: ' + str(e)) if 'proc-type' not in headers: # unencryped: done diff --git a/paramiko/proxy.py b/paramiko/proxy.py index 218b76e..14390e9 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -59,7 +59,7 @@ class ProxyCommand(object): """ try: self.process.stdin.write(content) - except IOError, e: + 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 @@ -79,7 +79,7 @@ class ProxyCommand(object): """ try: return os.read(self.process.stdout.fileno(), size) - except IOError, e: + except IOError as e: raise BadProxyCommand(' '.join(self.cmd), e.strerror) def close(self): diff --git a/paramiko/server.py b/paramiko/server.py index dac9bf1..df1f7fc 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -602,7 +602,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: + 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 17ea493..689dcf7 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 as x: 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 as e: # done with handle break if t != CMD_NAME: @@ -717,7 +717,7 @@ class SFTPClient (BaseSFTP): while True: try: t, data = self._read_packet() - except EOFError, e: + except EOFError as e: raise SSHException('Server connection dropped: %s' % (str(e),)) msg = Message(data) num = msg.get_int() diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index e056d70..2274039 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -464,7 +464,7 @@ class SFTPFile (BufferedFile): # save exception and re-raise it on next file operation try: self.sftp._convert_status(msg) - except Exception, x: + except Exception as x: self._saved_exception = x return if t != CMD_DATA: diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index a6cd44a..d000e34 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -100,7 +100,7 @@ class SFTPHandle (object): readfile.seek(offset) self.__tell = offset data = readfile.read(length) - except IOError, e: + except IOError as e: self.__tell = None return SFTPServer.convert_errno(e.errno) self.__tell += len(data) @@ -139,7 +139,7 @@ class SFTPHandle (object): self.__tell = offset writefile.write(data) writefile.flush() - except IOError, e: + 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 7cc6c0c..720c3a7 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -92,7 +92,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): except EOFError: self._log(DEBUG, 'EOF -- end of session') return - except Exception, e: + except Exception as e: self._log(DEBUG, 'Exception on channel: ' + str(e)) self._log(DEBUG, util.tb_strings()) return @@ -100,7 +100,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): request_number = msg.get_int() try: self._process(t, request_number, msg) - except Exception, e: + 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 201a253..c3ad270 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -292,7 +292,7 @@ class Transport (threading.Thread): sock = socket.socket(af, socket.SOCK_STREAM) try: retry_on_signal(lambda: sock.connect((hostname, port))) - except socket.error, e: + except socket.error as e: reason = str(e) else: break @@ -1181,7 +1181,7 @@ class Transport (threading.Thread): return [] try: return self.auth_handler.wait_for_response(my_event) - except BadAuthenticationType, x: + except BadAuthenticationType as x: # 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 +1197,7 @@ class Transport (threading.Thread): return [] return [ password ] return self.auth_interactive(username, handler) - except SSHException, ignored: + except SSHException as ignored: # attempt failed; just raise the original exception raise x return None @@ -1602,22 +1602,22 @@ class Transport (threading.Thread): msg.add_byte(chr(MSG_UNIMPLEMENTED)) msg.add_int(m.seqno) self._send_message(msg) - except SSHException, e: + except SSHException as e: self._log(ERROR, 'Exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e - except EOFError, e: + 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: + 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: + except Exception as e: self._log(ERROR, 'Unknown exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e @@ -1677,7 +1677,7 @@ class Transport (threading.Thread): buf = self.packetizer.readline(timeout) except ProxyCommandFailure: raise - except Exception, x: + except Exception as x: raise SSHException('Error reading SSH protocol banner' + str(x)) if buf[:4] == 'SSH-': break diff --git a/paramiko/util.py b/paramiko/util.py index f4bfbec..4ce86ed 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -276,7 +276,7 @@ def retry_on_signal(function): while True: try: return function() - except EnvironmentError, e: + except EnvironmentError as e: if e.errno != errno.EINTR: raise diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 7f1ecc7..e960967 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -38,7 +38,7 @@ class StubSFTPHandle (SFTPHandle): def stat(self): try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) def chattr(self, attr): @@ -47,7 +47,7 @@ class StubSFTPHandle (SFTPHandle): try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) @@ -69,21 +69,21 @@ class StubSFTPServer (SFTPServerInterface): attr.filename = fname out.append(attr) return out - except OSError, e: + 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: + 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: + except OSError as e: return SFTPServer.convert_errno(e.errno) def open(self, path, flags, attr): @@ -98,7 +98,7 @@ 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 as e: return SFTPServer.convert_errno(e.errno) if (flags & os.O_CREAT) and (attr is not None): attr._flags &= ~attr.FLAG_PERMISSIONS @@ -118,7 +118,7 @@ class StubSFTPServer (SFTPServerInterface): fstr = 'rb' try: f = os.fdopen(fd, fstr) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) fobj = StubSFTPHandle(flags) fobj.filename = path @@ -130,7 +130,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.remove(path) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -139,7 +139,7 @@ class StubSFTPServer (SFTPServerInterface): newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -149,7 +149,7 @@ class StubSFTPServer (SFTPServerInterface): os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -157,7 +157,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.rmdir(path) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -165,7 +165,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -185,7 +185,7 @@ class StubSFTPServer (SFTPServerInterface): target_path = '' try: os.symlink(target_path, path) - except OSError, e: + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -193,7 +193,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: symlink = os.readlink(path) - except OSError, e: + 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 b1697ea..e9615b4 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -644,7 +644,7 @@ class SFTPTest (unittest.TestCase): try: sftp.rename(FOLDER + '/something', FOLDER + u'/\u00fcnic\u00f8de') sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') - except Exception, e: + except Exception as e: self.fail('exception ' + e) sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') diff --git a/tests/test_transport.py b/tests/test_transport.py index 1c57d18..2dc3e08 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 as x: pass chan = self.tc.open_session() @@ -302,7 +302,7 @@ class TransportTest(ParamikoTest): try: chan = self.tc.open_channel('bogus') self.fail('expected exception') - except ChannelException, x: + except ChannelException as x: self.assert_(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) def test_9_exit_status(self):