Skip to content

Commit 700cb58

Browse files
authored
bpo-39828: Fix json.tool to catch BrokenPipeError (GH-18779)
1 parent b4698ec commit 700cb58

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/json/tool.py

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

7373

7474
if __name__ == '__main__':
75-
main()
75+
try:
76+
main()
77+
except BrokenPipeError as exc:
78+
sys.exit(exc.errno)

Lib/test/test_json/test_tool.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import errno
12
import os
23
import sys
34
import textwrap
45
import unittest
56
import subprocess
7+
68
from test import support
79
from test.support.script_helper import assert_python_ok
810

@@ -206,3 +208,14 @@ def test_ensure_ascii_default(self):
206208
# asserting an ascii encoded output file
207209
expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"]
208210
self.assertEqual(lines, expected)
211+
212+
@unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
213+
def test_broken_pipe_error(self):
214+
cmd = [sys.executable, '-m', 'json.tool']
215+
proc = subprocess.Popen(cmd,
216+
stdout=subprocess.PIPE,
217+
stdin=subprocess.PIPE)
218+
# bpo-39828: Closing before json.tool attempts to write into stdout.
219+
proc.stdout.close()
220+
proc.communicate(b'"{}"')
221+
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)