Skip to content

Commit caec8a0

Browse files
authored
[3.8] bpo-39828: Fix json.tool to catch BrokenPipeError (GH-18779). (GH-18894)
(cherry picked from commit 700cb58) Co-authored-by: Dong-hee Na <[email protected]> Automerge-Triggered-By: @vstinner
1 parent cadfe52 commit caec8a0

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
@@ -52,4 +52,7 @@ def main():
5252

5353

5454
if __name__ == '__main__':
55-
main()
55+
try:
56+
main()
57+
except BrokenPipeError as exc:
58+
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
@@ -149,3 +151,12 @@ def test_sort_keys_flag(self):
149151
self.assertEqual(out.splitlines(),
150152
self.expect_without_sort_keys.encode().splitlines())
151153
self.assertEqual(err, b'')
154+
155+
@unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
156+
def test_broken_pipe_error(self):
157+
cmd = [sys.executable, '-m', 'json.tool']
158+
proc = Popen(cmd, stdout=PIPE, stdin=PIPE)
159+
# bpo-39828: Closing before json.tool attempts to write into stdout.
160+
proc.stdout.close()
161+
proc.communicate(b'"{}"')
162+
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)