Skip to content

Commit ae86503

Browse files
authored
Simplify exception handlers (#1319)
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.
1 parent 2fcd547 commit ae86503

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

benchmarks/command_packer_benchmark.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@ def send_packed_command(self, command, check_health=True):
1313
self.connect()
1414
try:
1515
self._sock.sendall(command)
16-
except socket.error:
17-
e = sys.exc_info()[1]
16+
except socket.error as e:
1817
self.disconnect()
1918
if len(e.args) == 1:
2019
_errno, errmsg = 'UNKNOWN', e.args[0]
2120
else:
2221
_errno, errmsg = e.args
2322
raise ConnectionError("Error %s while writing to socket. %s." %
2423
(_errno, errmsg))
25-
except Exception as e:
24+
except Exception:
2625
self.disconnect()
27-
raise e
26+
raise
2827

2928
def pack_command(self, *args):
3029
"Pack a series of arguments into a value Redis command"
@@ -46,18 +45,17 @@ def send_packed_command(self, command, check_health=True):
4645
command = [command]
4746
for item in command:
4847
self._sock.sendall(item)
49-
except socket.error:
50-
e = sys.exc_info()[1]
48+
except socket.error as e:
5149
self.disconnect()
5250
if len(e.args) == 1:
5351
_errno, errmsg = 'UNKNOWN', e.args[0]
5452
else:
5553
_errno, errmsg = e.args
5654
raise ConnectionError("Error %s while writing to socket. %s." %
5755
(_errno, errmsg))
58-
except Exception as e:
56+
except Exception:
5957
self.disconnect()
60-
raise e
58+
raise
6159

6260
def pack_command(self, *args):
6361
output = []

redis/client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,8 +3887,8 @@ def _execute_transaction(self, connection, commands, raise_on_error):
38873887
# the socket
38883888
try:
38893889
self.parse_response(connection, '_')
3890-
except ResponseError:
3891-
errors.append((0, sys.exc_info()[1]))
3890+
except ResponseError as e:
3891+
errors.append((0, e))
38923892

38933893
# and all the other commands
38943894
for i, command in enumerate(commands):
@@ -3897,18 +3897,17 @@ def _execute_transaction(self, connection, commands, raise_on_error):
38973897
else:
38983898
try:
38993899
self.parse_response(connection, '_')
3900-
except ResponseError:
3901-
ex = sys.exc_info()[1]
3902-
self.annotate_exception(ex, i + 1, command[0])
3903-
errors.append((i, ex))
3900+
except ResponseError as e:
3901+
self.annotate_exception(e, i + 1, command[0])
3902+
errors.append((i, e))
39043903

39053904
# parse the EXEC.
39063905
try:
39073906
response = self.parse_response(connection, '_')
39083907
except ExecAbortError:
39093908
if errors:
39103909
raise errors[0][1]
3911-
raise sys.exc_info()[1]
3910+
raise
39123911

39133912
# EXEC clears any watched keys
39143913
self.watching = False
@@ -3950,8 +3949,8 @@ def _execute_pipeline(self, connection, commands, raise_on_error):
39503949
try:
39513950
response.append(
39523951
self.parse_response(connection, args[0], **options))
3953-
except ResponseError:
3954-
response.append(sys.exc_info()[1])
3952+
except ResponseError as e:
3953+
response.append(e)
39553954

39563955
if raise_on_error:
39573956
self.raise_first_error(commands, response)

redis/connection.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ def connect(self):
552552
sock = self._connect()
553553
except socket.timeout:
554554
raise TimeoutError("Timeout connecting to server")
555-
except socket.error:
556-
e = sys.exc_info()[1]
555+
except socket.error as e:
557556
raise ConnectionError(self._error_message(e))
558557

559558
self._sock = sock
@@ -701,8 +700,7 @@ def send_packed_command(self, command, check_health=True):
701700
except socket.timeout:
702701
self.disconnect()
703702
raise TimeoutError("Timeout writing to socket")
704-
except socket.error:
705-
e = sys.exc_info()[1]
703+
except socket.error as e:
706704
self.disconnect()
707705
if len(e.args) == 1:
708706
errno, errmsg = 'UNKNOWN', e.args[0]
@@ -711,7 +709,7 @@ def send_packed_command(self, command, check_health=True):
711709
errmsg = e.args[1]
712710
raise ConnectionError("Error %s while writing to socket. %s." %
713711
(errno, errmsg))
714-
except: # noqa: E722
712+
except BaseException:
715713
self.disconnect()
716714
raise
717715

@@ -736,12 +734,11 @@ def read_response(self):
736734
self.disconnect()
737735
raise TimeoutError("Timeout reading from %s:%s" %
738736
(self.host, self.port))
739-
except socket.error:
737+
except socket.error as e:
740738
self.disconnect()
741-
e = sys.exc_info()[1]
742739
raise ConnectionError("Error while reading from %s:%s : %s" %
743740
(self.host, self.port, e.args))
744-
except: # noqa: E722
741+
except BaseException:
745742
self.disconnect()
746743
raise
747744

@@ -1197,7 +1194,7 @@ def get_connection(self, command_name, *keys, **options):
11971194
connection.connect()
11981195
if connection.can_read():
11991196
raise ConnectionError('Connection not ready')
1200-
except: # noqa: E722
1197+
except BaseException:
12011198
# release the connection back to the pool so that we don't
12021199
# leak it
12031200
self.release(connection)
@@ -1359,7 +1356,7 @@ def get_connection(self, command_name, *keys, **options):
13591356
connection.connect()
13601357
if connection.can_read():
13611358
raise ConnectionError('Connection not ready')
1362-
except: # noqa: E722
1359+
except BaseException:
13631360
# release the connection back to the pool so that we don't leak it
13641361
self.release(connection)
13651362
raise

0 commit comments

Comments
 (0)