Skip to content

Commit 0654c4c

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 50938b6 commit 0654c4c

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
@@ -175,7 +175,12 @@ def main():
175175
'__package__': None,
176176
'__cached__': None,
177177
}
178-
runctx(code, globs, None, options.outfile, options.sort)
178+
try:
179+
runctx(code, globs, None, options.outfile, options.sort)
180+
except BrokenPipeError as exc:
181+
# Prevent "Exception ignored" during interpreter shutdown.
182+
sys.stdout = None
183+
sys.exit(exc.errno)
179184
else:
180185
parser.print_usage()
181186
return parser

Lib/profile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,12 @@ def main():
595595
'__package__': None,
596596
'__cached__': None,
597597
}
598-
runctx(code, globs, None, options.outfile, options.sort)
598+
try:
599+
runctx(code, globs, None, options.outfile, options.sort)
600+
except BrokenPipeError as exc:
601+
# Prevent "Exception ignored" during interpreter shutdown.
602+
sys.stdout = None
603+
sys.exit(exc.errno)
599604
else:
600605
parser.print_usage()
601606
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)