Skip to content

Commit cc9ee36

Browse files
committed
Close file after running the generator
1 parent f96f7c9 commit cc9ee36

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Lib/json/tool.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,11 @@ def main():
8787
infile = sys.stdin
8888
else:
8989
infile = open(options.infile, encoding='utf-8')
90-
try:
91-
if options.json_lines:
92-
objs = (json.loads(line) for line in infile)
93-
else:
94-
objs = (json.load(infile),)
95-
finally:
96-
if infile is not sys.stdin:
97-
infile.close()
90+
91+
if options.json_lines:
92+
objs = (json.loads(line) for line in infile)
93+
else:
94+
objs = (json.load(infile),)
9895

9996
if options.outfile is None:
10097
outfile = sys.stdout
@@ -111,6 +108,8 @@ def main():
111108
for obj in objs:
112109
json.dump(obj, outfile, **dump_args)
113110
outfile.write('\n')
111+
if infile is not sys.stdin:
112+
infile.close()
114113
except ValueError as e:
115114
raise SystemExit(e)
116115

0 commit comments

Comments
 (0)