Skip to content

Commit 9cea771

Browse files
[3.11] gh-85567: Fix resouce warnings in pickle and pickletools CLIs (GH-113618) (GH-113759)
Explicitly open and close files instead of using FileType. (cherry picked from commit bd754b9) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 6aa86bd commit 9cea771

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

Lib/pickle.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ def _test():
17991799
parser = argparse.ArgumentParser(
18001800
description='display contents of the pickle files')
18011801
parser.add_argument(
1802-
'pickle_file', type=argparse.FileType('br'),
1802+
'pickle_file',
18031803
nargs='*', help='the pickle file')
18041804
parser.add_argument(
18051805
'-t', '--test', action='store_true',
@@ -1815,6 +1815,10 @@ def _test():
18151815
parser.print_help()
18161816
else:
18171817
import pprint
1818-
for f in args.pickle_file:
1819-
obj = load(f)
1818+
for fn in args.pickle_file:
1819+
if fn == '-':
1820+
obj = load(sys.stdin.buffer)
1821+
else:
1822+
with open(fn, 'rb') as f:
1823+
obj = load(f)
18201824
pprint.pprint(obj)

Lib/pickletools.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -2848,10 +2848,10 @@ def _test():
28482848
parser = argparse.ArgumentParser(
28492849
description='disassemble one or more pickle files')
28502850
parser.add_argument(
2851-
'pickle_file', type=argparse.FileType('br'),
2851+
'pickle_file',
28522852
nargs='*', help='the pickle file')
28532853
parser.add_argument(
2854-
'-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
2854+
'-o', '--output',
28552855
help='the file where the output should be written')
28562856
parser.add_argument(
28572857
'-m', '--memo', action='store_true',
@@ -2876,15 +2876,26 @@ def _test():
28762876
if args.test:
28772877
_test()
28782878
else:
2879-
annotate = 30 if args.annotate else 0
28802879
if not args.pickle_file:
28812880
parser.print_help()
2882-
elif len(args.pickle_file) == 1:
2883-
dis(args.pickle_file[0], args.output, None,
2884-
args.indentlevel, annotate)
28852881
else:
2882+
annotate = 30 if args.annotate else 0
28862883
memo = {} if args.memo else None
2887-
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)
2884+
if args.output is None:
2885+
output = sys.stdout
2886+
else:
2887+
output = open(args.output, 'w')
2888+
try:
2889+
for arg in args.pickle_file:
2890+
if len(args.pickle_file) > 1:
2891+
name = '<stdin>' if arg == '-' else arg
2892+
preamble = args.preamble.format(name=name)
2893+
output.write(preamble + '\n')
2894+
if arg == '-':
2895+
dis(sys.stdin.buffer, output, memo, args.indentlevel, annotate)
2896+
else:
2897+
with open(arg, 'rb') as f:
2898+
dis(f, output, memo, args.indentlevel, annotate)
2899+
finally:
2900+
if output is not sys.stdout:
2901+
output.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix resource warnings for unclosed files in :mod:`pickle` and
2+
:mod:`pickletools` command line interfaces.

0 commit comments

Comments
 (0)