8000 gh-132631: Fix "I/O operation on closed file" when parsing JSON Lines file by hugovk · Pull Request #132632 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132631: Fix "I/O operation on closed file" when parsing JSON Lines file #132632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has significant memory implications for large .jsonl files. It might be better to instead move the finally clause either to the outer try or just after the with outfile block, but that does have implications for the infile == outfile case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, moving it gives a blank file when infile == outfile...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we could load all the lines into a list. This uses more memory than before, but less than the tuple as we're only deserialising each line when consumed from the generator:

Suggested change
objs = tuple(json.loads(line) for line in infile)
lines = infile.readlines()
objs = (json.loads(line) for line in lines)

I don't think it's worth adding much extra complexity for this CLI, as if someone needs to process extremely large .jsonl files they can write their own code to do exactly what they need.

else:
objs = (json.load(infile),)
finally:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_json/json_lines.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"ingredients":["frog", "water", "chocolate", "glucose"]}
{"ingredients":["chocolate","steel bolts"]}
9 changes: 9 additions & 0 deletions Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import errno
import pathlib
import os
import sys
import textwrap
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix "I/O operation on closed file" when parsing JSON Lines file with
:mod:`JSON CLI <json.tool>`.
Loading
0