diff options
author | Chris Wesseling <chris.wesseling@protonmail.com> | 2021-11-06 17:11:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-06 17:11:35 (GMT) |
commit | 815dad42d53fc40a6dc057e067f4a8a885c3b858 (patch) | |
tree | 11d228f4d9a4d56460e4a6470d30efba0128acba /Lib/json | |
parent | e9594f6747eaaaa848c26e2bf67d467aabfd62b3 (diff) | |
download | cpython-815dad42d53fc40a6dc057e067f4a8a885c3b858.zip cpython-815dad42d53fc40a6dc057e067f4a8a885c3b858.tar.gz cpython-815dad42d53fc40a6dc057e067f4a8a885c3b858.tar.bz2 |
bpo-45644: Make json.tool read infile before writing to outfile (GH-29273)
so that
$ python -m json.tool foo.json foo.json
doesn't result in an empty foo.json.
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/json')
-rw-r--r-- | Lib/json/tool.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 5dee0a7..0490b8c 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -13,6 +13,7 @@ Usage:: import argparse import json import sys +from pathlib import Path def main(): @@ -25,9 +26,9 @@ def main(): help='a JSON file to be validated or pretty-printed', default=sys.stdin) parser.add_argument('outfile', nargs='?', - type=argparse.FileType('w', encoding="utf-8"), + type=Path, help='write the output of infile to outfile', - default=sys.stdout) + default=None) parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false', @@ -58,15 +59,21 @@ def main(): dump_args['indent'] = None dump_args['separators'] = ',', ':' - with options.infile as infile, options.outfile as outfile: + with options.infile as infile: try: if options.json_lines: objs = (json.loads(line) for line in infile) else: - objs = (json.load(infile), ) - for obj in objs: - json.dump(obj, outfile, **dump_args) - outfile.write('\n') + objs = (json.load(infile),) + + if options.outfile is None: + out = sys.stdout + else: + out = options.outfile.open('w', encoding='utf-8') + with out as outfile: + for obj in objs: + json.dump(obj, outfile, **dump_args) + outfile.write('\n') except ValueError as e: raise SystemExit(e) |