]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39828: Fix json.tool to catch BrokenPipeError (GH-18779)
authorDong-hee Na <donghee.na92@gmail.com>
Tue, 10 Mar 2020 07:41:44 +0000 (16:41 +0900)
committerGitHub <noreply@github.com>
Tue, 10 Mar 2020 07:41:44 +0000 (08:41 +0100)
Lib/json/tool.py
Lib/test/test_json/test_tool.py
Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst [new file with mode: 0644]

index 6d7d9a002e5c242870f480e2e698762f02c9b568..5dee0a744b2a99e1e88c52dfecfec56a8598d10f 100644 (file)
@@ -72,4 +72,7 @@ def main():
 
 
 if __name__ == '__main__':
-    main()
+    try:
+        main()
+    except BrokenPipeError as exc:
+        sys.exit(exc.errno)
index c9a969b303398e2549b1fc58d03fba84b71644c8..fc2a7a4fca3c5a3f4769ed86e961a364f43b6368 100644 (file)
@@ -1,8 +1,10 @@
+import errno
 import os
 import sys
 import textwrap
 import unittest
 import subprocess
+
 from test import support
 from test.support.script_helper import assert_python_ok
 
@@ -206,3 +208,14 @@ class TestTool(unittest.TestCase):
         # asserting an ascii encoded output file
         expected = [b'{', rb'    "key": "\ud83d\udca9"', b"}"]
         self.assertEqual(lines, expected)
+
+    @unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
+    def test_broken_pipe_error(self):
+        cmd = [sys.executable, '-m', 'json.tool']
+        proc = subprocess.Popen(cmd,
+                                stdout=subprocess.PIPE,
+                                stdin=subprocess.PIPE)
+        # bpo-39828: Closing before json.tool attempts to write into stdout.
+        proc.stdout.close()
+        proc.communicate(b'"{}"')
+        self.assertEqual(proc.returncode, errno.EPIPE)
diff --git a/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst
new file mode 100644 (file)
index 0000000..04c61b9
--- /dev/null
@@ -0,0 +1 @@
+Fix :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na.