From 0654c4c4cc54a325e878154f8b117159d0105cf7 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 20 Jan 2021 01:19:08 -0800 Subject: [PATCH] bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643) (cherry picked from commit 3554fa4abecfb77ac5fcaa5ce8310eeca5683960) Co-authored-by: Zhiming Wang --- Lib/cProfile.py | 7 ++++++- Lib/profile.py | 7 ++++++- .../next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst diff --git a/Lib/cProfile.py b/Lib/cProfile.py index 59b4699feb50..22a7d0aade85 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -175,7 +175,12 @@ def main(): '__package__': None, '__cached__': None, } - runctx(code, globs, None, options.outfile, options.sort) + try: + runctx(code, globs, None, options.outfile, options.sort) + except BrokenPipeError as exc: + # Prevent "Exception ignored" during interpreter shutdown. + sys.stdout = None + sys.exit(exc.errno) else: parser.print_usage() return parser diff --git a/Lib/profile.py b/Lib/profile.py index 5cb017ed8300..d8599fb4eebd 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -595,7 +595,12 @@ def main(): '__package__': None, '__cached__': None, } - runctx(code, globs, None, options.outfile, options.sort) + try: + runctx(code, globs, None, options.outfile, options.sort) + except BrokenPipeError as exc: + # Prevent "Exception ignored" during interpreter shutdown. + sys.stdout = None + sys.exit(exc.errno) else: parser.print_usage() return parser diff --git a/Misc/NEWS.d/next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst b/Misc/NEWS.d/next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst new file mode 100644 index 000000000000..be4ed7f55ffd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst @@ -0,0 +1,2 @@ +Fix CLI of :mod:`cProfile` and :mod:`profile` to catch +:exc:`BrokenPipeError`. -- 2.47.3