From f1fc4abf4161274d415d0df9f5cad452e7e369af Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 5 Mar 2020 00:59:54 +0900 Subject: [PATCH 1/8] bpo-39828: Fix json.tool to ignore BrokenPipeError. --- Lib/json/tool.py | 5 ++++- .../next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 6d7d9a002e5c242..5835ab3bfb26ac6 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -72,4 +72,7 @@ def main(): if __name__ == '__main__': - main() + try: + main() + except BrokenPipeError: + pass diff --git a/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst new file mode 100644 index 000000000000000..954c1d3c962d42d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst @@ -0,0 +1 @@ +Fix :mod:`json.tool` to ignore :exc:`BrokenPipeError`. Patch by Dong-hee Na. From dfa361d346e73721f730e29d1afc64e8d4a051be Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 5 Mar 2020 14:41:53 +0900 Subject: [PATCH 2/8] bpo-39828: Update --- Lib/json/tool.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 5835ab3bfb26ac6..51208c1ca64bf36 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -13,6 +13,10 @@ import argparse import json import sys +import signal + + +signal.signal(signal.SIGPIPE, signal.SIG_DFL) def main(): @@ -72,7 +76,4 @@ def main(): if __name__ == '__main__': - try: - main() - except BrokenPipeError: - pass + main() From 871eabfbe31b35b06ea74b952af3aeb970af3006 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 5 Mar 2020 15:00:31 +0900 Subject: [PATCH 3/8] bpo-39828: Update --- Lib/json/tool.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 51208c1ca64bf36..4ba169ac6dcf105 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -13,10 +13,6 @@ import argparse import json import sys -import signal - - -signal.signal(signal.SIGPIPE, signal.SIG_DFL) def main(): @@ -76,4 +72,7 @@ def main(): if __name__ == '__main__': - main() + try: + main() + except BrokenPipeError as e: + sys.exit(e.errno) From 337fdd272251934715645d4e84c64f600edebf3d Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 7 Mar 2020 09:33:04 +0900 Subject: [PATCH 4/8] bpo-39828: Apply Victor's code review --- Lib/json/tool.py | 4 ++-- .../next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 4ba169ac6dcf105..5dee0a744b2a99e 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -74,5 +74,5 @@ def main(): if __name__ == '__main__': try: main() - except BrokenPipeError as e: - sys.exit(e.errno) + except BrokenPipeError as exc: + sys.exit(exc.errno) diff --git a/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst index 954c1d3c962d42d..659070bbe316e7a 100644 --- a/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst +++ b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst @@ -1 +1 @@ -Fix :mod:`json.tool` to ignore :exc:`BrokenPipeError`. Patch by Dong-hee Na. +Updated :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na. From d02c0891136f20fd5234660a35c731641203d4b0 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 7 Mar 2020 09:35:26 +0900 Subject: [PATCH 5/8] bpo-39828: Update NEWS.d --- .../next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst index 659070bbe316e7a..04c61b94c45d6c6 100644 --- a/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst +++ b/Misc/NEWS.d/next/Library/2020-03-05-00-57-49.bpo-39828.yWq9NJ.rst @@ -1 +1 @@ -Updated :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na. +Fix :mod:`json.tool` to catch :exc:`BrokenPipeError`. Patch by Dong-hee Na. From f67630c56c49f8cbd829e6fe65934bb101f4f637 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sun, 8 Mar 2020 12:55:45 +0900 Subject: [PATCH 6/8] bpo-39828: Add unittest --- Lib/test/test_json/test_tool.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index c9a969b303398e2..4638c98d711af45 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -3,6 +3,7 @@ import textwrap import unittest import subprocess + from test import support from test.support.script_helper import assert_python_ok @@ -206,3 +207,14 @@ def test_ensure_ascii_default(self): # asserting an ascii encoded output file expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"] 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) + proc.stdout.close() + proc.communicate(b'"a"') + proc.stdin.close() + self.assertEqual(proc.returncode, 32) From e6931d80130c15255dbf71597f049f230e4e4ec1 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 10 Mar 2020 12:23:50 +0900 Subject: [PATCH 7/8] bpo-39828: Apply Victor's review --- Lib/test/test_json/test_tool.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 4638c98d711af45..a380a1b04d37623 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -1,3 +1,4 @@ +import errno import os import sys import textwrap @@ -208,13 +209,12 @@ def test_ensure_ascii_default(self): expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"] 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'"a"') - proc.stdin.close() - self.assertEqual(proc.returncode, 32) + proc.communicate(b'"{}"') + self.assertEqual(proc.returncode, errno.EPIPE) From e9c7eaa29be2cccb014fde8ea13d341ca70fbca8 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 10 Mar 2020 13:20:32 +0900 Subject: [PATCH 8/8] bpo-39828: Ignore test for Windows --- Lib/test/test_json/test_tool.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index a380a1b04d37623..fc2a7a4fca3c5a3 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -209,6 +209,7 @@ def test_ensure_ascii_default(self): expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"] 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,