Skip to content

Commit 9f065c6

Browse files
committed
[3.7] bpo-39828: Fix json.tool to catch BrokenPipeError (pythonGH-18779).
(cherry picked from commit 700cb58) Co-authored-by: Dong-hee Na <[email protected]>
1 parent ae0bafc commit 9f065c6

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/json/tool.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ def main():
4242

4343

4444
if __name__ == '__main__':
45-
main()
45+
try:
46+
main()
47+
except BrokenPipeError as exc:
48+
sys.exit(exc.errno)

Lib/test/test_json/test_tool.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import errno
12
import os
23
import sys
34
import textwrap
45
import unittest
6+
57
from subprocess import Popen, PIPE
68
from test import support
79
from test.support.script_helper import assert_python_ok
@@ -120,3 +122,12 @@ def test_sort_keys_flag(self):
120122
self.assertEqual(out.splitlines(),
121123
self.expect_without_sort_keys.encode().splitlines())
122124
self.assertEqual(err, b'')
125+
126+
@unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
127+
def test_broken_pipe_error(self):
128+
cmd = [sys.executable, '-m', 'json.tool']
129+
proc = Popen(cmd, stdout=PIPE, stdin=PIPE)
130+
# bpo-39828: Closing before json.tool attempts to write into stdout.
131+
proc.stdout.close()
132+
proc.communicate(b'"{}"')
133+
self.assertEqual(proc.returncode, errno.EPIPE)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na.

0 commit comments

Comments
 (0)