[3.8] bpo-39828: Fix json.tool to catch BrokenPipeError (GH-18779). (GH-18894)

(cherry picked from commit 700cb58730)

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>

Automerge-Triggered-By: @vstinner
This commit is contained in:
Dong-hee Na 2020-03-10 17:14:08 +09:00 committed by GitHub
parent cadfe52a00
commit caec8a0dfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

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

View File

@ -1,7 +1,9 @@
import errno
import os import os
import sys import sys
import textwrap import textwrap
import unittest import unittest
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
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
@ -149,3 +151,12 @@ class TestTool(unittest.TestCase):
self.assertEqual(out.splitlines(), self.assertEqual(out.splitlines(),
self.expect_without_sort_keys.encode().splitlines()) self.expect_without_sort_keys.encode().splitlines())
self.assertEqual(err, b'') self.assertEqual(err, b'')
@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 = Popen(cmd, stdout=PIPE, stdin=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.