From a47f396ead8b02ce505abd06884f75ea1c294adf Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Mon, 20 Feb 2017 12:06:02 -0500 Subject: [PATCH 01/13] Add indent support for json.tool --- Lib/json/tool.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 4f3182c0c1e7f1..d5aa38737b5c5c 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -16,6 +16,18 @@ import sys +def parse_indent(indent): + """Parse the argparse indent argument.""" + if indent == 'None': + return None + if indent == r'\t': + return '\t' + try: + return int(indent) + except ValueError: + return indent + + def main(): prog = 'python -m json.tool' description = ('A simple command line interface for json module ' @@ -25,24 +37,33 @@ def main(): help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') + parser.add_argument('--indent', default='4', type=parse_indent, + help='Indent level or str for pretty-printing. ' + 'Use None for the most compact representation. ' + r'Use "\t" for tab indentation.') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() + # Read input JSON infile = options.infile or sys.stdin - outfile = options.outfile or sys.stdout - sort_keys = options.sort_keys with infile: try: - if sort_keys: + if options.sort_keys: obj = json.load(infile) else: obj = json.load(infile, object_pairs_hook=collections.OrderedDict) except ValueError as e: raise SystemExit(e) + + # Export JSON + outfile = options.outfile or sys.stdout with outfile: - json.dump(obj, outfile, sort_keys=sort_keys, indent=4) + json.dump(obj, outfile, + indent=options.indent, + sort_keys=options.sort_keys, + ) outfile.write('\n') From fb5d9847db241aaf7de715b8089ea3f08fdd2441 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Mon, 20 Feb 2017 12:22:44 -0500 Subject: [PATCH 02/13] Add support for disabling ensure_ascii in json.tool --- Lib/json/tool.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index d5aa38737b5c5c..b3fcbee81e2de6 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -37,6 +37,8 @@ def main(): help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') + parser.add_argument('--no_escape', action='store_true', default=False, + help='Do not set ensure_ascii to escape non-ASCII characters') parser.add_argument('--indent', default='4', type=parse_indent, help='Indent level or str for pretty-printing. ' 'Use None for the most compact representation. ' @@ -62,6 +64,7 @@ def main(): with outfile: json.dump(obj, outfile, indent=options.indent, + ensure_ascii=not options.no_escape, sort_keys=options.sort_keys, ) outfile.write('\n') From 4fb3bb3d7eca5de1c3c43de5dd15d1a8f57e45cb Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Mon, 20 Feb 2017 13:07:24 -0500 Subject: [PATCH 03/13] Add test for no_escape arg of json.tool --- Lib/test/test_json/test_tool.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 15f373664e1278..70e72b96635e5f 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -68,22 +68,22 @@ def test_stdin_stdout(self): self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(err, None) - def _create_infile(self): + def _create_infile(self, text): infile = support.TESTFN with open(infile, "w") as fp: self.addCleanup(os.remove, infile) - fp.write(self.data) + fp.write(text) return infile def test_infile_stdout(self): - infile = self._create_infile() + infile = self._create_infile(self.data) rc, out, err = assert_python_ok('-m', 'json.tool', infile) self.assertEqual(rc, 0) self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(err, b'') def test_infile_outfile(self): - infile = self._create_infile() + infile = self._create_infile(self.data) outfile = support.TESTFN + '.out' rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile) self.addCleanup(os.remove, outfile) @@ -100,9 +100,22 @@ def test_help_flag(self): self.assertEqual(err, b'') def test_sort_keys_flag(self): - infile = self._create_infile() + infile = self._create_infile(self.data) rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile) self.assertEqual(rc, 0) self.assertEqual(out.splitlines(), self.expect_without_sort_keys.encode().splitlines()) self.assertEqual(err, b'') + + def test_no_ascii_flag(self): + data = '{"json": "🐍 and δ"}' + expect = textwrap.dedent('''\ + { + "json": "🐍 and δ" + } + ''') + infile = self._create_infile(data) + rc, out, err = assert_python_ok('-m', 'json.tool', '--no_ascii', infile) + self.assertEqual(rc, 0) + self.assertEqual(out.splitlines(), expect.splitlines()) + self.assertEqual(err, b'') From 29d01fe80274e8e4f1b13a189b2daaff1fc65dac Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Thu, 23 Feb 2017 14:49:27 -0500 Subject: [PATCH 04/13] bpo-27413: --no-ensure-ascii argument in json.tool Closes https://bugs.python.org/issue27413 --- Lib/json/tool.py | 4 ++-- Lib/test/test_json/test_tool.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index b3fcbee81e2de6..12563c0c8f3363 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -37,7 +37,7 @@ def main(): help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') - parser.add_argument('--no_escape', action='store_true', default=False, + parser.add_argument('--no-ensure-ascii', action='store_true', default=False, help='Do not set ensure_ascii to escape non-ASCII characters') parser.add_argument('--indent', default='4', type=parse_indent, help='Indent level or str for pretty-printing. ' @@ -64,7 +64,7 @@ def main(): with outfile: json.dump(obj, outfile, indent=options.indent, - ensure_ascii=not options.no_escape, + ensure_ascii=not options.no_ensure_ascii, sort_keys=options.sort_keys, ) outfile.write('\n') diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 70e72b96635e5f..7ad64bc7d1ffab 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -115,7 +115,7 @@ def test_no_ascii_flag(self): } ''') infile = self._create_infile(data) - rc, out, err = assert_python_ok('-m', 'json.tool', '--no_ascii', infile) + rc, out, err = assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile) self.assertEqual(rc, 0) - self.assertEqual(out.splitlines(), expect.splitlines()) + self.assertEqual(out.splitlines(), expect.encode().splitlines()) self.assertEqual(err, b'') From a3e2b23b1115e66444469685b57e534f0cef69ff Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Thu, 23 Feb 2017 15:52:07 -0500 Subject: [PATCH 05/13] Add non-ascii characters to TestTool.data --- Lib/test/test_json/test_tool.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 7ad64bc7d1ffab..ac916b8b484216 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -11,7 +11,7 @@ class TestTool(unittest.TestCase): data = """ [["blorpie"],[ "whoops" ] , [ - ],\t"d-shtaeou",\r"d-nthiouh", + ],\t"d-shtaeou",\r"🐍 and δ", "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" :"yes"} ] """ @@ -26,7 +26,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "d-nthiouh", + "🐍 and δ", "i-vhbjkhnth", { "nifty": 87 @@ -48,7 +48,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "d-nthiouh", + "🐍 and δ", "i-vhbjkhnth", { "nifty": 87 @@ -68,22 +68,22 @@ def test_stdin_stdout(self): self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(err, None) - def _create_infile(self, text): + def _create_infile(self): infile = support.TESTFN with open(infile, "w") as fp: self.addCleanup(os.remove, infile) - fp.write(text) + fp.write(self.data) return infile def test_infile_stdout(self): - infile = self._create_infile(self.data) + infile = self._create_infile() rc, out, err = assert_python_ok('-m', 'json.tool', infile) self.assertEqual(rc, 0) self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(err, b'') def test_infile_outfile(self): - infile = self._create_infile(self.data) + infile = self._create_infile() outfile = support.TESTFN + '.out' rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile) self.addCleanup(os.remove, outfile) @@ -100,22 +100,17 @@ def test_help_flag(self): self.assertEqual(err, b'') def test_sort_keys_flag(self): - infile = self._create_infile(self.data) + infile = self._create_infile() rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile) self.assertEqual(rc, 0) self.assertEqual(out.splitlines(), self.expect_without_sort_keys.encode().splitlines()) self.assertEqual(err, b'') - def test_no_ascii_flag(self): - data = '{"json": "🐍 and δ"}' - expect = textwrap.dedent('''\ - { - "json": "🐍 and δ" - } - ''') - infile = self._create_infile(data) + def test_no_ensure_ascii_flag(self): + infile = self._create_infile() rc, out, err = assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile) self.assertEqual(rc, 0) - self.assertEqual(out.splitlines(), expect.encode().splitlines()) + self.assertEqual(out.splitlines(), + self.expect_without_sort_keys.encode().splitlines()) self.assertEqual(err, b'') From 97152e9ea93942dc0a5d4ff2bc00cc4dd1c4f5e4 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Thu, 23 Feb 2017 18:18:36 -0500 Subject: [PATCH 06/13] Add --no-indent argument --- Lib/json/tool.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 12563c0c8f3363..bb13878f224b7d 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -16,18 +16,6 @@ import sys -def parse_indent(indent): - """Parse the argparse indent argument.""" - if indent == 'None': - return None - if indent == r'\t': - return '\t' - try: - return int(indent) - except ValueError: - return indent - - def main(): prog = 'python -m json.tool' description = ('A simple command line interface for json module ' @@ -39,10 +27,11 @@ def main(): help='write the output of infile to outfile') parser.add_argument('--no-ensure-ascii', action='store_true', default=False, help='Do not set ensure_ascii to escape non-ASCII characters') - parser.add_argument('--indent', default='4', type=parse_indent, - help='Indent level or str for pretty-printing. ' - 'Use None for the most compact representation. ' - r'Use "\t" for tab indentation.') + group = parser.add_mutually_exclusive_group() + group.add_argument('--indent', default=4, type=int, + help='Indent level for pretty-printing.') + group.add_argument('--no-indent', action='store_true', default=False, + help='Use compact mode.') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() @@ -63,7 +52,7 @@ def main(): outfile = options.outfile or sys.stdout with outfile: json.dump(obj, outfile, - indent=options.indent, + indent=None if options.no_indent else options.indent, ensure_ascii=not options.no_ensure_ascii, sort_keys=options.sort_keys, ) From d69907079b5a9e9e6b6de49245cb0e686ead39ff Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Thu, 23 Feb 2017 18:34:59 -0500 Subject: [PATCH 07/13] Escape non-ascii characters in json.tool tests --- Lib/test/test_json/test_tool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index ac916b8b484216..a8e3254f4bf1f4 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -11,7 +11,7 @@ class TestTool(unittest.TestCase): data = """ [["blorpie"],[ "whoops" ] , [ - ],\t"d-shtaeou",\r"🐍 and δ", + ],\t"d-shtaeou",\r"\N{snake} \u03B4 and \U0001D037", "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" :"yes"} ] """ @@ -26,7 +26,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "🐍 and δ", + "\N{snake} \u03B4 and \U0001D037", "i-vhbjkhnth", { "nifty": 87 @@ -48,7 +48,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "🐍 and δ", + "\N{snake} \u03B4 and \U0001D037", "i-vhbjkhnth", { "nifty": 87 From 0f38c18178cb6e9cbbaa495bf5a7d879e93dc117 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sun, 26 Feb 2017 15:37:53 -0500 Subject: [PATCH 08/13] Switch --no-indent to use store_const --- Lib/json/tool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index bb13878f224b7d..e815e15664b773 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -30,8 +30,8 @@ def main(): group = parser.add_mutually_exclusive_group() group.add_argument('--indent', default=4, type=int, help='Indent level for pretty-printing.') - group.add_argument('--no-indent', action='store_true', default=False, - help='Use compact mode.') + group.add_argument('--no-indent', action='store_const', dest='indent', + const=None, help='Use compact mode.') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() @@ -52,7 +52,7 @@ def main(): outfile = options.outfile or sys.stdout with outfile: json.dump(obj, outfile, - indent=None if options.no_indent else options.indent, + indent=options.indent, ensure_ascii=not options.no_ensure_ascii, sort_keys=options.sort_keys, ) From 66a173a88ec9e1d27bdd13e146a1f153b357af97 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sun, 26 Feb 2017 15:50:10 -0500 Subject: [PATCH 09/13] Improve argparse help messages --- Lib/json/tool.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index e815e15664b773..d0f43f770dd6f9 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -26,10 +26,11 @@ def main(): parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') parser.add_argument('--no-ensure-ascii', action='store_true', default=False, - help='Do not set ensure_ascii to escape non-ASCII characters') + help='Do not escape non-ASCII characters in output.') group = parser.add_mutually_exclusive_group() group.add_argument('--indent', default=4, type=int, - help='Indent level for pretty-printing.') + help='Indent level (number of spaces) for ' + 'pretty-printing. Defaults to 4.') group.add_argument('--no-indent', action='store_const', dest='indent', const=None, help='Use compact mode.') parser.add_argument('--sort-keys', action='store_true', default=False, From dd25cf320a35e80de71fa8656a3e52d96d243006 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sun, 26 Feb 2017 16:28:32 -0500 Subject: [PATCH 10/13] Add unicode character in the U+0080-U+00FF range --- Lib/test/test_json/test_tool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index a8e3254f4bf1f4..a660dbf8cd6a3f 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -11,7 +11,7 @@ class TestTool(unittest.TestCase): data = """ [["blorpie"],[ "whoops" ] , [ - ],\t"d-shtaeou",\r"\N{snake} \u03B4 and \U0001D037", + ],\t"d-shtaeou",\r"\xA7 \N{snake} \u03B4 and \U0001D037", "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" :"yes"} ] """ @@ -26,7 +26,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "\N{snake} \u03B4 and \U0001D037", + "\xA7 \N{snake} \u03B4 and \U0001D037", "i-vhbjkhnth", { "nifty": 87 @@ -48,7 +48,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "\N{snake} \u03B4 and \U0001D037", + "\xA7 \N{snake} \u03B4 and \U0001D037", "i-vhbjkhnth", { "nifty": 87 From 3413f4fe4d88e4e6dbd10fe365cef94319572600 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sun, 26 Feb 2017 16:35:47 -0500 Subject: [PATCH 11/13] Format unicode output for JSON --- 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 a660dbf8cd6a3f..f82ab2109928bb 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -16,7 +16,7 @@ class TestTool(unittest.TestCase): :"yes"} ] """ - expect_without_sort_keys = textwrap.dedent("""\ + expect_without_sort_keys = textwrap.dedent(r"""\ [ [ "blorpie" @@ -26,7 +26,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "\xA7 \N{snake} \u03B4 and \U0001D037", + "\u00a7 \ud83d\udc0d \u03b4 and \ud834\udc3", "i-vhbjkhnth", { "nifty": 87 @@ -38,7 +38,7 @@ class TestTool(unittest.TestCase): ] """) - expect = textwrap.dedent("""\ + expect = textwrap.dedent(r"""\ [ [ "blorpie" @@ -48,7 +48,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "\xA7 \N{snake} \u03B4 and \U0001D037", + "\u00a7 \ud83d\udc0d \u03b4 and \ud834\udc37", "i-vhbjkhnth", { "nifty": 87 From 52bafb00fa49fe99e826b2c48b71fe40222c62bf Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sun, 26 Feb 2017 17:48:23 -0500 Subject: [PATCH 12/13] Test indent --- Lib/test/test_json/test_tool.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index f82ab2109928bb..34c07f30666240 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -2,7 +2,7 @@ import sys import textwrap import unittest -import subprocess +from subprocess import Popen, PIPE from test import support from test.support.script_helper import assert_python_ok @@ -61,12 +61,11 @@ class TestTool(unittest.TestCase): """) def test_stdin_stdout(self): - with subprocess.Popen( - (sys.executable, '-m', 'json.tool'), - stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc: + args = sys.executable, '-m', 'json.tool' + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: out, err = proc.communicate(self.data.encode()) self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) - self.assertEqual(err, None) + self.assertEqual(err, b'') def _create_infile(self): infile = support.TESTFN @@ -114,3 +113,25 @@ def test_no_ensure_ascii_flag(self): self.assertEqual(out.splitlines(), self.expect_without_sort_keys.encode().splitlines()) self.assertEqual(err, b'') + + def test_indent(self): + json_stdin = b'[1, 2]' + expect = textwrap.dedent(b'''\ + [ + 1, + 2 + ] + ''') + args = sys.executable, '-m', 'json.tool', '--indent', '2' + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: + json_stdout, err = proc.communicate(json_stdin) + self.assertEqual(expect.splitlines(), json_stdout.splitlines()) + self.assertEqual(err, b'') + + def test_no_indent(self): + json_stdin = b'[1, 2]' + args = sys.executable, '-m', 'json.tool', '--no-indent' + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: + json_stdout, err = proc.communicate(json_stdin) + self.assertEqual(json_stdin, json_stdout) + self.assertEqual(err, b'') From 1306518fda7801e6b8477bd8ec0af1aaab0b5bb7 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sun, 26 Feb 2017 18:13:29 -0500 Subject: [PATCH 13/13] Move unicode tests outside of self.data --- Lib/test/test_json/test_tool.py | 41 ++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 34c07f30666240..b8890c096aaa47 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -11,12 +11,12 @@ class TestTool(unittest.TestCase): data = """ [["blorpie"],[ "whoops" ] , [ - ],\t"d-shtaeou",\r"\xA7 \N{snake} \u03B4 and \U0001D037", + ],\t"d-shtaeou",\r"d-nthiouh", "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" :"yes"} ] """ - expect_without_sort_keys = textwrap.dedent(r"""\ + expect_without_sort_keys = textwrap.dedent("""\ [ [ "blorpie" @@ -26,7 +26,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "\u00a7 \ud83d\udc0d \u03b4 and \ud834\udc3", + "d-nthiouh", "i-vhbjkhnth", { "nifty": 87 @@ -38,7 +38,7 @@ class TestTool(unittest.TestCase): ] """) - expect = textwrap.dedent(r"""\ + expect = textwrap.dedent("""\ [ [ "blorpie" @@ -48,7 +48,7 @@ class TestTool(unittest.TestCase): ], [], "d-shtaeou", - "\u00a7 \ud83d\udc0d \u03b4 and \ud834\udc37", + "d-nthiouh", "i-vhbjkhnth", { "nifty": 87 @@ -106,22 +106,14 @@ def test_sort_keys_flag(self): self.expect_without_sort_keys.encode().splitlines()) self.assertEqual(err, b'') - def test_no_ensure_ascii_flag(self): - infile = self._create_infile() - rc, out, err = assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile) - self.assertEqual(rc, 0) - self.assertEqual(out.splitlines(), - self.expect_without_sort_keys.encode().splitlines()) - self.assertEqual(err, b'') - def test_indent(self): json_stdin = b'[1, 2]' - expect = textwrap.dedent(b'''\ + expect = textwrap.dedent('''\ [ 1, 2 ] - ''') + ''').encode() args = sys.executable, '-m', 'json.tool', '--indent', '2' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: json_stdout, err = proc.communicate(json_stdin) @@ -133,5 +125,22 @@ def test_no_indent(self): args = sys.executable, '-m', 'json.tool', '--no-indent' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: json_stdout, err = proc.communicate(json_stdin) - self.assertEqual(json_stdin, json_stdout) + self.assertEqual(json_stdin.splitlines(), json_stdout.splitlines()) + self.assertEqual(err, b'') + + def test_ensure_ascii(self): + json_stdin = '"\xA7 \N{snake} \u03B4 \U0001D037"'.encode() + expect = b'"\\u00a7 \\ud83d\\udc0d \\u03b4 \\ud834\\udc37"\n' + args = sys.executable, '-m', 'json.tool' + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: + json_stdout, err = proc.communicate(json_stdin) + self.assertEqual(expect.splitlines(), json_stdout.splitlines()) + self.assertEqual(err, b'') + + def test_no_ensure_ascii(self): + json_stdin = '"\xA7 \N{snake} \u03B4 \U0001D037"'.encode() + args = sys.executable, '-m', 'json.tool', '--no-ensure-ascii' + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: + json_stdout, err = proc.communicate(json_stdin) + self.assertEqual(json_stdin.splitlines(), json_stdout.splitlines()) self.assertEqual(err, b'')