diff options
-rw-r--r-- | Lib/json/tool.py | 5 | ||||
-rw-r--r-- | Lib/test/test_json/test_tool.py | 13 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst | 1 |
3 files changed, 18 insertions, 1 deletions
diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 6d7d9a0..5dee0a7 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -72,4 +72,7 @@ def main(): if __name__ == '__main__': - main() + try: + main() + except BrokenPipeError as exc: + sys.exit(exc.errno) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index c9a969b..fc2a7a4 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -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 index 0000000..04c61b9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst @@ -0,0 +1 @@ +Fix :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na. |