From e04f47c79badff4e34ca9cb29a02203f156e4ed8 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Thu, 17 Apr 2025 15:08:00 +0300 Subject: [PATCH 1/4] Fix 'I/O operation on closed file' when parsing JSON Lines file --- Lib/json/tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 1ba91384c81f27..1369ec6264ebed 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -55,7 +55,7 @@ def main(): infile = open(options.infile, encoding='utf-8') try: if options.json_lines: - objs = (json.loads(line) for line in infile) + objs = tuple(json.loads(line) for line in infile) else: objs = (json.load(infile),) finally: From 7ea74ed959571cdcb624780422b73c98897a09d1 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Thu, 17 Apr 2025 15:26:44 +0300 Subject: [PATCH 2/4] Add blurb --- .../next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst diff --git a/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst b/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst new file mode 100644 index 00000000000000..864de38bdbf87a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst @@ -0,0 +1,2 @@ +Fix "I/O operation on closed file" when parsing JSON Lines file with JSON +CLI. From dbe289502f63523d8f0919fe26bc6b8a4a7e6395 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Fri, 18 Apr 2025 13:44:05 +0300 Subject: [PATCH 3/4] Link to docs Co-authored-by: Brian Schubert --- .../Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst b/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst index 864de38bdbf87a..9cc1d5a389c085 100644 --- a/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst +++ b/Misc/NEWS.d/next/Library/2025-04-17-15-26-35.gh-issue-132631.IDFZfb.rst @@ -1,2 +1,2 @@ -Fix "I/O operation on closed file" when parsing JSON Lines file with JSON -CLI. +Fix "I/O operation on closed file" when parsing JSON Lines file with +:mod:`JSON CLI `. From ed60cdae91416b7c3bc83bb8833138381954a987 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 24 Jun 2025 15:05:11 +0300 Subject: [PATCH 4/4] Add test case that fails on main --- Lib/test/test_json/json_lines.jsonl | 2 ++ Lib/test/test_json/test_tool.py | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 Lib/test/test_json/json_lines.jsonl diff --git a/Lib/test/test_json/json_lines.jsonl b/Lib/test/test_json/json_lines.jsonl new file mode 100644 index 00000000000000..d2f292111956f3 --- /dev/null +++ b/Lib/test/test_json/json_lines.jsonl @@ -0,0 +1,2 @@ +{"ingredients":["frog", "water", "chocolate", "glucose"]} +{"ingredients":["chocolate","steel bolts"]} diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index 9ea2679c77ec17..ba609aaa2159ea 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -1,4 +1,5 @@ import errno +import pathlib import os import sys import textwrap @@ -156,6 +157,14 @@ def test_jsonlines(self): self.assertEqual(process.stdout, self.jsonlines_expect) self.assertEqual(process.stderr, '') + @force_not_colorized + def test_jsonlines_from_file(self): + jsonl = pathlib.Path(__file__).parent / 'json_lines.jsonl' + args = sys.executable, '-m', self.module, '--json-lines', jsonl + process = subprocess.run(args, capture_output=True, text=True, check=True) + self.assertEqual(process.stdout, self.jsonlines_expect) + self.assertEqual(process.stderr, '') + def test_help_flag(self): rc, out, err = assert_python_ok('-m', self.module, '-h', PYTHON_COLORS='0')