-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-132631: Fix json.tool when using --json-lines and a file as input #138961
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
cc9ee36
f72edc5
2d5e1bf
ed78614
30a3947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,14 +87,11 @@ def main(): | |
| infile = sys.stdin | ||
| else: | ||
| infile = open(options.infile, encoding='utf-8') | ||
| try: | ||
| if options.json_lines: | ||
| objs = (json.loads(line) for line in infile) | ||
| else: | ||
| objs = (json.load(infile),) | ||
| finally: | ||
| if infile is not sys.stdin: | ||
| infile.close() | ||
|
|
||
| if options.json_lines: | ||
| objs = (json.loads(line) for line in infile) | ||
| else: | ||
| objs = (json.load(infile),) | ||
|
|
||
| if options.outfile is None: | ||
| outfile = sys.stdout | ||
|
|
@@ -111,6 +108,8 @@ def main(): | |
| for obj in objs: | ||
| json.dump(obj, outfile, **dump_args) | ||
| outfile.write('\n') | ||
| if infile is not sys.stdin: | ||
|
||
| infile.close() | ||
| except ValueError as e: | ||
| raise SystemExit(e) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one can fail directl, so please be careful that we close the
infilebeforehand (that's the reaason why we have the try-finally in the first place).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're probably using a generator here to save memory. Otherwise we could've just used a list comprehension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment was about the
elsebranch. It's not a generator, it's a regular tuple.