]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 20 Jan 2021 09:18:07 +0000 (01:18 -0800)
committerGitHub <noreply@github.com>
Wed, 20 Jan 2021 09:18:07 +0000 (01:18 -0800)
(cherry picked from commit 3554fa4abecfb77ac5fcaa5ce8310eeca5683960)

Co-authored-by: Zhiming Wang <i@zhimingwang.org>
Lib/cProfile.py
Lib/profile.py
Misc/NEWS.d/next/Library/2020-10-11-13-48-03.bpo-42005.Jq6Az-.rst [new file with mode: 0644]

index 47aacf9e2d494e680e91019fa129ec7ae5e071d4..406a9b7cf11be954539c31cb62aa295c7b3d099b 100755 (executable)
@@ -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
index 9df4435c5ae8374ca8f37326efefa400b0f00608..df4450dac6a1b65fff2c090b74743ba8474c2964 100755 (executable)
@@ -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 (file)
index 0000000..be4ed7f
--- /dev/null
@@ -0,0 +1,2 @@
+Fix CLI of :mod:`cProfile` and :mod:`profile` to catch
+:exc:`BrokenPipeError`.