[Python 3]: New except syntax.

This commit is contained in:
Dorian 2013-08-13 15:57:38 -04:00
parent 0847ac780b
commit b1e235d820
24 changed files with 64 additions and 64 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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))

View File

@ -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')

View File

@ -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)')

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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())

View File

@ -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()

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 = '<error>'
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):

View File

@ -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')

View File

@ -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):