8000 Fix pickle and pickletools not closing files · python/cpython@50208d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50208d4

Browse files
committed
Fix pickle and pickletools not closing files
Closes #85567
1 parent 36fcde6 commit 50208d4

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Lib/pickle.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,5 +1816,9 @@ def _test():
18161816
else:
18171817
import pprint
18181818
for f in args.pickle_file:
1819-
obj = load(f)
1820-
pprint.pprint(obj)
1819+
try:
1820+
obj = load(f)
1821+
pprint.pprint(obj)
1822+
finally:
1823+
f.close()
1824+

Lib/pickletools.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,11 +2880,19 @@ def _test():
28802880
if not args.pickle_file:
28812881
parser.print_help()
28822882
elif len(args.pickle_file) == 1:
2883-
dis(args.pickle_file[0], args.output, None,
2884-
args.indentlevel, annotate)
2883+
try:
2884+
dis(args.pickle_file[0], args.output, None,
2885+
args.indentlevel, annotate)
2886+
finally:
2887+
args.pickle_file[0].close()
2888+
args.output.close()
28852889
else:
28862890
memo = {} if args.memo else None
28872891
for f in args.pickle_file:
2888-
preamble = args.preamble.format(name=f.name)
2889-
args.output.write(preamble + '\n')
2890-
dis(f, args.output, memo, args.indentlevel, annotate)
2892+
try:
2893+
preamble = args.preamble.format(name=f.name)
2894+
args.output.write(preamble + '\n')
2895+
dis(f, args.output, memo, args.indentlevel, annotate)
2896+
finally:
2897+
f.close()
2898+
args.output.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where ``pickle`` and ``pickletools`` command line interfaces do not
2+
close files

0 commit comments

Comments
 (0)
0