Skip to content

Commit 648b729

Browse files
bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)
(cherry picked from commit 3554fa4) Co-authored-by: Zhiming Wang <[email protected]>
1 parent ece5dfd commit 648b729

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/cProfile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,12 @@ def main():
191191
'__package__': None,
192192
'__cached__': None,
193193
}
194-
runctx(code, globs, None, options.outfile, options.sort)
194+
try:
195+
runctx(code, globs, None, options.outfile, options.sort)
196+
except BrokenPipeError as exc:
197+
# Prevent "Exception ignored" during interpreter shutdown.
198+
sys.stdout = None
199+
sys.exit(exc.errno)
195200
else:
196201
parser.print_usage()
197202
return parser

Lib/profile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,12 @@ def main():
611611
'__package__': None,
612612
'__cached__': None,
613613
}
614-
runctx(code, globs, None, options.outfile, options.sort)
614+
try:
615+
runctx(code, globs, None, options.outfile, options.sort)
616+
except BrokenPipeError as exc:
617+
# Prevent "Exception ignored" during interpreter shutdown.
618+
sys.stdout = None
619+
sys.exit(exc.errno)
615620
else:
616621
parser.print_usage()
617622
return parser
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix CLI of :mod:`cProfile` and :mod:`profile` to catch
2+
:exc:`BrokenPipeError`.

0 commit comments

Comments
 (0)