]> 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:19:08 +0000 (01:19 -0800)
committerGitHub <noreply@github.com>
Wed, 20 Jan 2021 09:19:08 +0000 (01:19 -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 59b4699feb5062aef3fef5110be0acf8d7ae4e61..22a7d0aade855fd83e72d251cdf6ee4336e41765 100755 (executable)
@@ -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
index 5cb017ed8300993ad9767db967e105926e2f0159..d8599fb4eebd66f2e15020b7b0a637b5140dc5f8 100755 (executable)
@@ -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 (file)
index 0000000..be4ed7f
--- /dev/null
@@ -0,0 +1,2 @@
+Fix CLI of :mod:`cProfile` and :mod:`profile` to catch
+:exc:`BrokenPipeError`.