[3.14] gh-132631: Fix "I/O operation on closed file" when parsing JSON Lines file (GH-132632) (#148921)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
This commit is contained in:
Miss Islington (bot)
2026-04-23 18:32:29 +02:00
committed by GitHub
parent 0f656e2641
commit 31ba91a35e
4 changed files with 15 additions and 1 deletions
+2 -1
View File
@@ -88,7 +88,8 @@ def main():
infile = open(options.infile, encoding='utf-8')
try:
if options.json_lines:
objs = (json.loads(line) for line in infile)
lines = infile.readlines()
objs = (json.loads(line) for line in lines)
else:
objs = (json.load(infile),)
finally:
+2
View File
@@ -0,0 +1,2 @@
{"ingredients":["frog", "water", "chocolate", "glucose"]}
{"ingredients":["chocolate","steel bolts"]}
+9
View File
@@ -1,4 +1,5 @@
import errno
import pathlib
import os
import sys
import textwrap
@@ -157,6 +158,14 @@ class TestMain(unittest.TestCase):
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')
@@ -0,0 +1,2 @@
Fix "I/O operation on closed file" when parsing JSON Lines file with
:mod:`JSON CLI <json.tool>`.