[Python-checkins] bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)

miss-islington webhook-mailer at python.org
Wed Jan 20 04:18:26 EST 2021


https://github.com/python/cpython/commit/648b72900b5039ab46b8b459f921daecb8db2a6b
commit: 648b72900b5039ab46b8b459f921daecb8db2a6b
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-01-20T01:18:07-08:00
summary:

bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)

(cherry picked from commit 3554fa4abecfb77ac5fcaa5ce8310eeca5683960)

Co-authored-by: Zhiming Wang <i at zhimingwang.org>

files:
A Misc/NEWS.d/next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst
M Lib/cProfile.py
M Lib/profile.py

diff --git a/Lib/cProfile.py b/Lib/cProfile.py
index 47aacf9e2d494..406a9b7cf11be 100755
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -191,7 +191,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 9df4435c5ae83..df4450dac6a1b 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -611,7 +611,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 0000000000000..be4ed7f55ffde
--- /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`.



More information about the Python-checkins mailing list