bpo-39828: Fix json.tool to catch BrokenPipeError (GH-18779)

This commit is contained in:
Dong-hee Na 2020-03-10 16:41:44 +09:00 committed by GitHub
parent b4698ecfdb
commit 700cb58730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View File

@ -72,4 +72,7 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() try:
main()
except BrokenPipeError as exc:
sys.exit(exc.errno)

View File

@ -1,8 +1,10 @@
import errno
import os import os
import sys import sys
import textwrap import textwrap
import unittest import unittest
import subprocess import subprocess
from test import support from test import support
from test.support.script_helper import assert_python_ok from test.support.script_helper import assert_python_ok
@ -206,3 +208,14 @@ class TestTool(unittest.TestCase):
# asserting an ascii encoded output file # asserting an ascii encoded output file
expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"] expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"]
self.assertEqual(lines, expected) 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)

View File

@ -0,0 +1 @@
Fix :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na.