From f7a27a5e6936e1fdf07cbf6dfd225273d21c8c5d Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sat, 4 Apr 2020 16:12:18 -0700 Subject: [PATCH] Simplify exception handlers Use the "as" keyword to capture the exception in a variable instead of sys.exc_info(). Re-raise exception with the bare "raise" syntax. Avoid "# noqa: E722" by catching BaseException, which includes all exceptions including SystemExit. --- benchmarks/command_packer_benchmark.py | 14 ++++++-------- redis/client.py | 17 ++++++++--------- redis/connection.py | 17 +++++++---------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/benchmarks/command_packer_benchmark.py b/benchmarks/command_packer_benchmark.py index 817639187f..53c7c3fce8 100644 --- a/benchmarks/command_packer_benchmark.py +++ b/benchmarks/command_packer_benchmark.py @@ -13,8 +13,7 @@ def send_packed_command(self, command): self.connect() try: self._sock.sendall(command) - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: self.disconnect() if len(e.args) == 1: _errno, errmsg = 'UNKNOWN', e.args[0] @@ -22,9 +21,9 @@ def send_packed_command(self, command): _errno, errmsg = e.args raise ConnectionError("Error %s while writing to socket. %s." % (_errno, errmsg)) - except Exception as e: + except Exception: self.disconnect() - raise e + raise def pack_command(self, *args): "Pack a series of arguments into a value Redis command" @@ -46,8 +45,7 @@ def send_packed_command(self, command): command = [command] for item in command: self._sock.sendall(item) - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: self.disconnect() if len(e.args) == 1: _errno, errmsg = 'UNKNOWN', e.args[0] @@ -55,9 +53,9 @@ def send_packed_command(self, command): _errno, errmsg = e.args raise ConnectionError("Error %s while writing to socket. %s." % (_errno, errmsg)) - except Exception as e: + except Exception: self.disconnect() - raise e + raise def pack_command(self, *args): output = [] diff --git a/redis/client.py b/redis/client.py index 07aa9f68a5..7454321030 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3887,8 +3887,8 @@ def _execute_transaction(self, connection, commands, raise_on_error): # the socket try: self.parse_response(connection, '_') - except ResponseError: - errors.append((0, sys.exc_info()[1])) + except ResponseError as e: + errors.append((0, e)) # and all the other commands for i, command in enumerate(commands): @@ -3897,10 +3897,9 @@ def _execute_transaction(self, connection, commands, raise_on_error): else: try: self.parse_response(connection, '_') - except ResponseError: - ex = sys.exc_info()[1] - self.annotate_exception(ex, i + 1, command[0]) - errors.append((i, ex)) + except ResponseError as e: + self.annotate_exception(e, i + 1, command[0]) + errors.append((i, e)) # parse the EXEC. try: @@ -3908,7 +3907,7 @@ def _execute_transaction(self, connection, commands, raise_on_error): except ExecAbortError: if errors: raise errors[0][1] - raise sys.exc_info()[1] + raise # EXEC clears any watched keys self.watching = False @@ -3950,8 +3949,8 @@ def _execute_pipeline(self, connection, commands, raise_on_error): try: response.append( self.parse_response(connection, args[0], **options)) - except ResponseError: - response.append(sys.exc_info()[1]) + except ResponseError as e: + response.append(e) if raise_on_error: self.raise_first_error(commands, response) diff --git a/redis/connection.py b/redis/connection.py index c61517e155..bdc1d2cb91 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -552,8 +552,7 @@ def connect(self): sock = self._connect() except socket.timeout: raise TimeoutError("Timeout connecting to server") - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: raise ConnectionError(self._error_message(e)) self._sock = sock @@ -701,8 +700,7 @@ def send_packed_command(self, command, check_health=True): except socket.timeout: self.disconnect() raise TimeoutError("Timeout writing to socket") - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: self.disconnect() if len(e.args) == 1: errno, errmsg = 'UNKNOWN', e.args[0] @@ -711,7 +709,7 @@ def send_packed_command(self, command, check_health=True): errmsg = e.args[1] raise ConnectionError("Error %s while writing to socket. %s." % (errno, errmsg)) - except: # noqa: E722 + except BaseException: self.disconnect() raise @@ -736,12 +734,11 @@ def read_response(self): self.disconnect() raise TimeoutError("Timeout reading from %s:%s" % (self.host, self.port)) - except socket.error: + except socket.error as e: self.disconnect() - e = sys.exc_info()[1] raise ConnectionError("Error while reading from %s:%s : %s" % (self.host, self.port, e.args)) - except: # noqa: E722 + except BaseException: self.disconnect() raise @@ -1197,7 +1194,7 @@ def get_connection(self, command_name, *keys, **options): connection.connect() if connection.can_read(): raise ConnectionError('Connection not ready') - except: # noqa: E722 + except BaseException: # release the connection back to the pool so that we don't # leak it self.release(connection) @@ -1359,7 +1356,7 @@ def get_connection(self, command_name, *keys, **options): connection.connect() if connection.can_read(): raise ConnectionError('Connection not ready') - except: # noqa: E722 + except BaseException: # release the connection back to the pool so that we don't leak it self.release(connection) raise