Skip to content

Commit

Permalink
Python 3.3+: replace deprecated aliases
Browse files Browse the repository at this point in the history
since 3.3: EnvironmentError, IOError, socket.error and select.error are merged into IOError.
They may now return a more specific subclass - which this commit does not utilize yet.
  • Loading branch information
pajod committed Apr 22, 2024
1 parent ec85b32 commit 4f77665
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/when_ready.conf.py
Expand Up @@ -17,7 +17,7 @@ def __init__(self, server, max_mem):
def memory_usage(self, pid):
try:
out = commands.getoutput("ps -o rss -p %s" % pid)
except IOError:
except OSError:
return -1
used_mem = sum(int(x) for x in out.split('\n')[1:])
return used_mem
Expand Down
4 changes: 2 additions & 2 deletions gunicorn/arbiter.py
Expand Up @@ -332,7 +332,7 @@ def wakeup(self):
"""
try:
os.write(self.PIPE[1], b'.')
except IOError as e:
except OSError as e:
if e.errno not in [errno.EAGAIN, errno.EINTR]:
raise

Expand Down Expand Up @@ -361,7 +361,7 @@ def sleep(self):
return
while os.read(self.PIPE[0], 1):
pass
except (select.error, OSError) as e:
except OSError as e:
# TODO: select.error is a subclass of OSError since Python 3.3.
error_number = getattr(e, 'errno', e.args[0])
if error_number not in [errno.EAGAIN, errno.EINTR]:
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/debug.py
Expand Up @@ -36,7 +36,7 @@ def __call__(self, frame, event, arg):
try:
src = inspect.getsourcelines(frame)
line = src[lineno]
except IOError:
except OSError:
line = 'Unknown code named [%s]. VM instruction #%d' % (
frame.f_code.co_name, frame.f_lasti)
if self.trace_names is None or name in self.trace_names:
Expand Down
4 changes: 2 additions & 2 deletions gunicorn/http/message.py
Expand Up @@ -372,13 +372,13 @@ def parse_proxy_protocol(self, line):
try:
socket.inet_pton(socket.AF_INET, s_addr)
socket.inet_pton(socket.AF_INET, d_addr)
except socket.error:
except OSError:
raise InvalidProxyLine(line)
elif proto == "TCP6":
try:
socket.inet_pton(socket.AF_INET6, s_addr)
socket.inet_pton(socket.AF_INET6, d_addr)
except socket.error:
except OSError:
raise InvalidProxyLine(line)

try:
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/pidfile.py
Expand Up @@ -79,7 +79,7 @@ def validate(self):
if e.args[0] == errno.ESRCH:
return
raise
except IOError as e:
except OSError as e:
if e.args[0] == errno.ENOENT:
return
raise
6 changes: 3 additions & 3 deletions gunicorn/sock.py
Expand Up @@ -42,7 +42,7 @@ def set_options(self, sock, bound=False):
and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
except OSError as err:
if err.errno not in (errno.ENOPROTOOPT, errno.EINVAL):
raise
if not bound:
Expand All @@ -65,7 +65,7 @@ def close(self):

try:
self.sock.close()
except socket.error as e:
except OSError as e:
self.log.info("Error while closing socket %s", str(e))

self.sock = None
Expand Down Expand Up @@ -182,7 +182,7 @@ def create_sockets(conf, log, fds=None):
for i in range(5):
try:
sock = sock_type(addr, conf, log)
except socket.error as e:
except OSError as e:
if e.args[0] == errno.EADDRINUSE:
log.error("Connection in use: %s", str(addr))
if e.args[0] == errno.EADDRNOTAVAIL:
Expand Down
8 changes: 4 additions & 4 deletions gunicorn/util.py
Expand Up @@ -216,7 +216,7 @@ def unlink(filename):
def is_ipv6(addr):
try:
socket.inet_pton(socket.AF_INET6, addr)
except socket.error: # not a valid address
except OSError: # not a valid address
return False
except ValueError: # ipv6 not supported on this platform
return False
Expand Down Expand Up @@ -268,7 +268,7 @@ def set_non_blocking(fd):
def close(sock):
try:
sock.close()
except socket.error:
except OSError:
pass


Expand Down Expand Up @@ -565,7 +565,7 @@ def check_is_writable(path):
try:
with open(path, 'a') as f:
f.close()
except IOError as e:
except OSError as e:
raise RuntimeError("Error: '%s' isn't writable [%r]" % (path, e))


Expand All @@ -586,7 +586,7 @@ def has_fileno(obj):
# check BytesIO case and maybe others
try:
obj.fileno()
except (AttributeError, IOError, io.UnsupportedOperation):
except (AttributeError, OSError, io.UnsupportedOperation):
return False

return True
Expand Down
8 changes: 4 additions & 4 deletions gunicorn/workers/base_async.py
Expand Up @@ -59,7 +59,7 @@ def handle(self, listener, client, addr):
except ssl.SSLError:
# pass to next try-except level
util.reraise(*sys.exc_info())
except EnvironmentError:
except OSError:
# pass to next try-except level
util.reraise(*sys.exc_info())
except Exception as e:
Expand All @@ -71,7 +71,7 @@ def handle(self, listener, client, addr):
else:
self.log.debug("Error processing SSL request.")
self.handle_error(req, client, addr, e)
except EnvironmentError as e:
except OSError as e:
if e.errno not in (errno.EPIPE, errno.ECONNRESET, errno.ENOTCONN):
self.log.exception("Socket error processing request.")
else:
Expand Down Expand Up @@ -123,7 +123,7 @@ def handle_request(self, listener_name, req, sock, addr):
raise StopIteration()
except StopIteration:
raise
except EnvironmentError:
except OSError:
# If the original exception was a socket.error we delegate
# handling it to the caller (where handle() might ignore it)
util.reraise(*sys.exc_info())
Expand All @@ -135,7 +135,7 @@ def handle_request(self, listener_name, req, sock, addr):
try:
sock.shutdown(socket.SHUT_RDWR)
sock.close()
except EnvironmentError:
except OSError:
pass
raise StopIteration()
raise
Expand Down
10 changes: 5 additions & 5 deletions gunicorn/workers/gthread.py
Expand Up @@ -127,7 +127,7 @@ def accept(self, server, listener):
with self._lock:
self.poller.register(conn.sock, selectors.EVENT_READ,
partial(self.on_client_socket_readable, conn))
except EnvironmentError as e:
except OSError as e:
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
errno.EWOULDBLOCK):
raise
Expand Down Expand Up @@ -170,7 +170,7 @@ def murder_keepalived(self):
with self._lock:
try:
self.poller.unregister(conn.sock)
except EnvironmentError as e:
except OSError as e:
if e.errno != errno.EBADF:
raise
except KeyError:
Expand Down Expand Up @@ -294,7 +294,7 @@ def handle(self, conn):
self.log.debug("Error processing SSL request.")
self.handle_error(req, conn.sock, conn.client, e)

except EnvironmentError as e:
except OSError as e:
if e.errno not in (errno.EPIPE, errno.ECONNRESET, errno.ENOTCONN):
self.log.exception("Socket error processing request.")
else:
Expand Down Expand Up @@ -348,7 +348,7 @@ def handle_request(self, req, conn):
if resp.should_close():
self.log.debug("Closing connection.")
return False
except EnvironmentError:
except OSError:
# pass to next try-except level
util.reraise(*sys.exc_info())
except Exception:
Expand All @@ -359,7 +359,7 @@ def handle_request(self, req, conn):
try:
conn.sock.shutdown(socket.SHUT_RDWR)
conn.sock.close()
except EnvironmentError:
except OSError:
pass
raise StopIteration()
raise
Expand Down
12 changes: 6 additions & 6 deletions gunicorn/workers/sync.py
Expand Up @@ -39,7 +39,7 @@ def wait(self, timeout):
os.read(self.PIPE[0], 1)
return ret[0]

except select.error as e:
except OSError as e:
if e.args[0] == errno.EINTR:
return self.sockets
if e.args[0] == errno.EBADF:
Expand Down Expand Up @@ -72,7 +72,7 @@ def run_for_one(self, timeout):
# process.
continue

except EnvironmentError as e:
except OSError as e:
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
errno.EWOULDBLOCK):
raise
Expand Down Expand Up @@ -101,7 +101,7 @@ def run_for_multiple(self, timeout):

try:
self.accept(listener)
except EnvironmentError as e:
except OSError as e:
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
errno.EWOULDBLOCK):
raise
Expand Down Expand Up @@ -143,7 +143,7 @@ def handle(self, listener, client, addr):
else:
self.log.debug("Error processing SSL request.")
self.handle_error(req, client, addr, e)
except EnvironmentError as e:
except OSError as e:
if e.errno not in (errno.EPIPE, errno.ECONNRESET, errno.ENOTCONN):
self.log.exception("Socket error processing request.")
else:
Expand Down Expand Up @@ -187,7 +187,7 @@ def handle_request(self, listener, req, client, addr):
self.log.access(resp, req, environ, request_time)
if hasattr(respiter, "close"):
respiter.close()
except EnvironmentError:
except OSError:
# pass to next try-except level
util.reraise(*sys.exc_info())
except Exception:
Expand All @@ -198,7 +198,7 @@ def handle_request(self, listener, req, client, addr):
try:
client.shutdown(socket.SHUT_RDWR)
client.close()
except EnvironmentError:
except OSError:
pass
raise StopIteration()
raise
Expand Down

0 comments on commit 4f77665

Please sign in to comment.