From 147f4ee67b1d2bcb1cc5ec50873dedfb68c98aff Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Mon, 29 Nov 2021 17:01:57 +0100 Subject: [PATCH 1/6] ignore empty lines for json_lines files --- Lib/json/tool.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 0490b8c0be11df..db092e3260e59c 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -15,6 +15,7 @@ import sys from pathlib import Path +_SENTINEL = object() def main(): prog = 'python -m json.tool' @@ -62,7 +63,7 @@ def main(): with options.infile as infile: try: if options.json_lines: - objs = (json.loads(line) for line in infile) + objs = (json.loads(line) if line.strip() else _SENTINEL for line in infile) else: objs = (json.load(infile),) @@ -72,7 +73,8 @@ def main(): out = options.outfile.open('w', encoding='utf-8') with out as outfile: for obj in objs: - json.dump(obj, outfile, **dump_args) + if obj is not _SENTINEL: + json.dump(obj, outfile, **dump_args) outfile.write('\n') except ValueError as e: raise SystemExit(e) From b8401b794ca373a50402e15101d2a8eeddb31cc8 Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Mon, 29 Nov 2021 17:08:21 +0100 Subject: [PATCH 2/6] add test_jsonlines_emptylines --- Lib/test/test_json/test_tool.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 1d7fca6efb1cc7..1bf40cc58dddd1 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -85,6 +85,24 @@ class TestTool(unittest.TestCase): } """) + jsonlines_emptlylines_raw = textwrap.dedent("""\ + {"foo":1} + + {"bar":2} + + """) + + jsonlines_emptlylines_expect = textwrap.dedent("""\ + { + "foo": 1 + } + + { + "bar": 2 + } + + """) + def test_stdin_stdout(self): args = sys.executable, '-m', 'json.tool' process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True) @@ -146,6 +164,13 @@ def test_jsonlines(self): self.assertEqual(process.stdout, self.jsonlines_expect) self.assertEqual(process.stderr, '') + def test_jsonlines_emptylines(self): + args = sys.executable, '-m', 'json.tool', '--json-lines' + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: + out, err = proc.communicate(self.jsonlines_emptylines_raw.encode()) + self.assertEqual(out.splitlines(), self.jsonlines_emptylines_expect.encode().splitlines()) + self.assertEqual(err, b'') + def test_help_flag(self): rc, out, err = assert_python_ok('-m', 'json.tool', '-h') self.assertEqual(rc, 0) From 1538452e98641f91b9cbb759203e99040c3fd857 Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Mon, 29 Nov 2021 17:28:58 +0100 Subject: [PATCH 3/6] add test_jsonlines_emptylines --- Lib/test/test_json/test_tool.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 1bf40cc58dddd1..93d4bf1538fb54 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -164,13 +164,12 @@ def test_jsonlines(self): self.assertEqual(process.stdout, self.jsonlines_expect) self.assertEqual(process.stderr, '') - def test_jsonlines_emptylines(self): + def test_jsonlines(self): args = sys.executable, '-m', 'json.tool', '--json-lines' - with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: - out, err = proc.communicate(self.jsonlines_emptylines_raw.encode()) - self.assertEqual(out.splitlines(), self.jsonlines_emptylines_expect.encode().splitlines()) - self.assertEqual(err, b'') - + process = subprocess.run(args, input=self.jsonlines_emptylines_raw, capture_output=True, text=True, check=True) + self.assertEqual(process.stdout, self.jsonlines_emptylines_expect) + self.assertEqual(process.stderr, '') + def test_help_flag(self): rc, out, err = assert_python_ok('-m', 'json.tool', '-h') self.assertEqual(rc, 0) From 59dd6aea5767703c2937192b0efb804eacb2a266 Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Tue, 30 Nov 2021 06:32:38 +0100 Subject: [PATCH 4/6] fix typo --- 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 93d4bf1538fb54..1e1459bb099990 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -85,14 +85,14 @@ class TestTool(unittest.TestCase): } """) - jsonlines_emptlylines_raw = textwrap.dedent("""\ + jsonlines_emptylines_raw = textwrap.dedent("""\ {"foo":1} {"bar":2} """) - jsonlines_emptlylines_expect = textwrap.dedent("""\ + jsonlines_emptylines_expect = textwrap.dedent("""\ { "foo": 1 } @@ -164,7 +164,7 @@ def test_jsonlines(self): self.assertEqual(process.stdout, self.jsonlines_expect) self.assertEqual(process.stderr, '') - def test_jsonlines(self): + def test_jsonlines_emptylines(self): args = sys.executable, '-m', 'json.tool', '--json-lines' process = subprocess.run(args, input=self.jsonlines_emptylines_raw, capture_output=True, text=True, check=True) self.assertEqual(process.stdout, self.jsonlines_emptylines_expect) From fde59f3677e781fb80ca8e69ea857b3165df2df5 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 30 Nov 2021 07:15:33 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst diff --git a/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst b/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst new file mode 100644 index 00000000000000..b32901bfade015 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst @@ -0,0 +1 @@ +Let json.tool --json-lines handle empty rows \ No newline at end of file From e9238d1f703451686a0bb848fe84de95cd98d42d Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Sat, 5 Mar 2022 12:50:46 +0100 Subject: [PATCH 6/6] add newline at end of file fix for https://dev.azure.com/Python/cpython/_build/results?buildId=97943&view=logs&j=4db1505a-29e5-5cc0-240b-53a8a2681f75&t=a975920c-8356-5388-147c-613d5fab0171&l=17 --- .../next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst b/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst index b32901bfade015..752e7e56e6548e 100644 --- a/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst +++ b/Misc/NEWS.d/next/Library/2021-11-30-07-15-32.bpo-45929.EeKtTK.rst @@ -1 +1 @@ -Let json.tool --json-lines handle empty rows \ No newline at end of file +Let json.tool --json-lines handle empty rows