Skip to content

Commit e2a5a5a

Browse files
committed
Add comments explaining why bare except clauses are avoided
1 parent 2578054 commit e2a5a5a

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

‎memory_profiler.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ def posix_tool():
202202
else:
203203
return mem
204204
except (ValueError, IndexError):
205+
# Catch only parsing errors rather than a bare except so we do not
206+
# swallow BaseException subclasses like SystemExit or KeyboardInterrupt.
205207
if timestamps:
206208
return -1, time.time()
207209
else:

‎mprof.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def run_action():
249249
try:
250250
cmd_line = proc.cmdline()
251251
except Exception:
252+
# Avoid bare except so SystemExit and KeyboardInterrupt are not caught
252253
cmd_line = get_cmd_line(program)
253254
if args.timeout is None:
254255
args.timeout = 3600
@@ -757,6 +758,8 @@ def xlim_type(value):
757758
try:
758759
newvalue = [float(x) for x in value.split(',')]
759760
except (ValueError, AttributeError):
761+
# Catch specific parsing errors rather than a bare except to avoid
762+
# swallowing SystemExit or KeyboardInterrupt.
760763
raise ArgumentError("'%s' option must contain two numbers separated with a comma" % value)
761764
if len(newvalue) != 2:
762765
raise ArgumentError("'%s' option must contain two numbers separated with a comma" % value)

0 commit comments

Comments
 (0)