Commit c093b89e authored by Denis Bilenko's avatar Denis Bilenko

use as-syntax in except clauses #38

parent abe61a16
......@@ -28,7 +28,7 @@ class PortForwarder(StreamServer):
log('%s:%s accepted', *address[:2])
try:
dest = create_connection(self.dest)
except IOError, ex:
except IOError as ex:
log('%s:%s failed to connect to %s:%s: %s', address[0], address[1], self.dest[0], self.dest[1], ex)
return
gevent.spawn(forward, source, dest)
......
......@@ -161,7 +161,7 @@ cpdef unsigned int _flags_to_int(object flags) except? -1:
value = value.strip().lower()
if value:
result |= _flags_str2int[value]
except KeyError, ex:
except KeyError as ex:
raise ValueError('Invalid backend or flag: %s\nPossible values: %s' % (ex, ', '.join(sorted(_flags_str2int.keys()))))
return result
......
......@@ -279,7 +279,7 @@ class Hub(greenlet):
else:
try:
info = self.loop._format()
except Exception, ex:
except Exception as ex:
info = str(ex) or repr(ex) or 'error'
result = '<%s at 0x%x %s' % (self.__class__.__name__, id(self), info)
if self._resolver is not None:
......
......@@ -48,7 +48,7 @@ if fcntl:
while True:
try:
return _read(fd, n)
except OSError, e:
except OSError as e:
if e.errno not in ignored_errors:
raise
sys.exc_clear()
......@@ -67,7 +67,7 @@ if fcntl:
while True:
try:
return _write(fd, buf)
except OSError, e:
except OSError as e:
if e.errno not in ignored_errors:
raise
sys.exc_clear()
......
......@@ -353,7 +353,7 @@ class WSGIHandler(object):
def _sendall(self, data):
try:
self.socket.sendall(data)
except socket.error, ex:
except socket.error as ex:
self.status = 'socket error: %s' % ex
if self.code > 0:
self.code = -self.code
......
......@@ -91,7 +91,7 @@ class StreamServer(BaseServer):
def do_read(self):
try:
client_socket, address = self.socket.accept()
except _socket.error, err:
except _socket.error as err:
if err[0] == EWOULDBLOCK:
return
raise
......@@ -130,7 +130,7 @@ class DatagramServer(BaseServer):
def do_read(self):
try:
data, address = self._socket.recvfrom(8192)
except _socket.error, err:
except _socket.error as err:
if err[0] == EWOULDBLOCK:
return
raise
......
......@@ -70,7 +70,7 @@ class SSLSocket(socket):
# see if it's connected
try:
socket.getpeername(self)
except socket_error, e:
except socket_error as e:
if e[0] != errno.ENOTCONN:
raise
# no, no connection yet
......
......@@ -443,7 +443,7 @@ class Popen(object):
env,
cwd,
startupinfo)
except pywintypes.error, e:
except pywintypes.error as e:
# Translate pywintypes.error to WindowsError, which is
# a subclass of OSError. FIXME: We should really
# translate errno using _sys_errlist (or similar), but
......@@ -799,7 +799,7 @@ def write_and_close(fobj, data):
try:
if data:
fobj.write(data)
except (OSError, IOError), ex:
except (OSError, IOError) as ex:
if ex.errno != errno.EPIPE and ex.errno != errno.EINVAL:
raise
finally:
......
......@@ -68,7 +68,7 @@ class Timeout(BaseException):
timeout.start()
try:
...
except Timeout, t:
except Timeout as t:
if t is not timeout:
raise # not my timeout
"""
......
......@@ -14,7 +14,7 @@ class wrap_errors(object):
def wrapped_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except (A, B, C), ex:
except (A, B, C) as ex:
return ex
:class:`wrap_errors` provides a shortcut to write that in one line::
......
......@@ -31,7 +31,7 @@ def bench_gevent(arg=0):
def bench_eventlet(arg):
try:
import eventlet
except ImportError, ex:
except ImportError as ex:
sys.stderr.write('Failed to import eventlet: %s\n' % ex)
return
from eventlet.api import sleep
......
......@@ -777,7 +777,7 @@ class TestContentLength304(TestCase):
def application(self, env, start_response):
try:
start_response('304 Not modified', [('Content-Length', '100')])
except AssertionError, ex:
except AssertionError as ex:
start_response('200 Raised', [])
return [str(ex)]
else:
......@@ -804,7 +804,7 @@ class TestBody304(TestCase):
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
try:
read_http(fd)
except AssertionError, ex:
except AssertionError as ex:
self.assertEqual(str(ex), 'The 304 response must have no body')
else:
raise AssertionError('AssertionError must be raised')
......@@ -827,7 +827,7 @@ class TestWrite304(TestCase):
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
try:
read_http(fd)
except AssertionError, ex:
except AssertionError as ex:
self.assertEqual(str(ex), 'The 304 response must have no body')
else:
raise AssertionError('write() must raise')
......
......@@ -298,7 +298,7 @@ class TestFamily(TestCase):
try:
result = function(*args)
raise AssertionError('%s: Expected to raise %s, instead returned %r' % (function, error, result))
except Exception, ex:
except Exception as ex:
if isinstance(error, basestring):
repr_error = error
else:
......
......@@ -11,7 +11,7 @@ class TestClosedSocket(greentest.TestCase):
sock.close()
try:
sock.send('a', timeout=1)
except socket.error, ex:
except socket.error as ex:
if ex[0] != 9:
raise
......
......@@ -28,7 +28,7 @@ class Test(greentest.TestCase):
def test_child_exception(self):
try:
subprocess.Popen(['*']).wait()
except OSError, ex:
except OSError as ex:
assert ex.errno == 2, ex
else:
raise AssertionError('Expected OSError: [Errno 2] No such file or directory')
......@@ -141,7 +141,7 @@ class Test(greentest.TestCase):
def test_check_output_keyword_error(self):
try:
subprocess.check_output([sys.executable, '-c', 'import sys; sys.exit(44)'])
except subprocess.CalledProcessError, e:
except subprocess.CalledProcessError as e:
self.assertEqual(e.returncode, 44)
else:
raise AssertionError('must fail with CalledProcessError')
......
......@@ -8,7 +8,7 @@ address = ('127.0.0.10', 53)
listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
listener.bind(address)
except socket.error, ex:
except socket.error as ex:
if 'permission denied' in str(ex).lower():
sys.stderr.write('This test binds on port 53 and thus must be run as root.\n')
sys.exit(0)
......@@ -24,7 +24,7 @@ gevent.spawn(reader)
r = gevent.get_hub().resolver = Resolver(servers=['127.0.0.10'], timeout=0.001, tries=1)
try:
result = r.gethostbyname('www.google.com')
except socket.gaierror, ex:
except socket.gaierror as ex:
if 'ARES_ETIMEOUT' not in str(ex):
raise
else:
......
......@@ -14,7 +14,7 @@ for count in xrange(2):
os.close(fileno)
try:
gevent.sleep(0.001)
except SystemError, ex:
except SystemError as ex:
if '(libev)' in str(ex):
print 'The error is expected: %s' % ex
else:
......
......@@ -50,10 +50,10 @@ def killpg(pid):
return
try:
return os.killpg(pid, 9)
except OSError, ex:
except OSError as ex:
if ex.errno != 3:
log('killpg(%r, 9) failed: %s: %s', pid, type(ex).__name__, ex)
except Exception, ex:
except Exception as ex:
log('killpg(%r, 9) failed: %s: %s', pid, type(ex).__name__, ex)
......@@ -68,7 +68,7 @@ def _kill(popen):
if hasattr(popen, 'kill'):
try:
popen.kill()
except OSError, ex:
except OSError as ex:
if ex.errno == 3: # No such process
return
if ex.errno == 13: # Permission denied (translated from windows error 5: "Access is denied")
......
......@@ -13,7 +13,7 @@ def kill(popen):
return
try:
popen.kill()
except OSError, ex:
except OSError as ex:
if ex.errno == 3: # No such process
return
if ex.errno == 13: # Permission denied (translated from windows error 5: "Access is denied")
......
......@@ -108,7 +108,7 @@ def modify_version(filename, new_version):
def unlink(path):
try:
os.unlink(path)
except OSError, ex:
except OSError as ex:
if ex.errno == 2: # No such file or directory
return
raise
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment