mirror of
https://github.com/python/cpython
synced 2024-10-14 09:43:55 +00:00
gh-85567: Fix resouce warnings in pickle and pickletools CLIs (GH-113618)
Explicitly open and close files instead of using FileType.
This commit is contained in:
parent
3c4e972d6d
commit
bd754b93ca
|
@ -1793,7 +1793,7 @@ def _test():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='display contents of the pickle files')
|
description='display contents of the pickle files')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'pickle_file', type=argparse.FileType('br'),
|
'pickle_file',
|
||||||
nargs='*', help='the pickle file')
|
nargs='*', help='the pickle file')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-t', '--test', action='store_true',
|
'-t', '--test', action='store_true',
|
||||||
|
@ -1809,6 +1809,10 @@ def _test():
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
else:
|
else:
|
||||||
import pprint
|
import pprint
|
||||||
for f in args.pickle_file:
|
for fn in args.pickle_file:
|
||||||
obj = load(f)
|
if fn == '-':
|
||||||
|
obj = load(sys.stdin.buffer)
|
||||||
|
else:
|
||||||
|
with open(fn, 'rb') as f:
|
||||||
|
obj = load(f)
|
||||||
pprint.pprint(obj)
|
pprint.pprint(obj)
|
||||||
|
|
|
@ -2848,10 +2848,10 @@ def _test():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='disassemble one or more pickle files')
|
description='disassemble one or more pickle files')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'pickle_file', type=argparse.FileType('br'),
|
'pickle_file',
|
||||||
nargs='*', help='the pickle file')
|
nargs='*', help='the pickle file')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
|
'-o', '--output',
|
||||||
help='the file where the output should be written')
|
help='the file where the output should be written')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-m', '--memo', action='store_true',
|
'-m', '--memo', action='store_true',
|
||||||
|
@ -2876,15 +2876,26 @@ def _test():
|
||||||
if args.test:
|
if args.test:
|
||||||
_test()
|
_test()
|
||||||
else:
|
else:
|
||||||
annotate = 30 if args.annotate else 0
|
|
||||||
if not args.pickle_file:
|
if not args.pickle_file:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
elif len(args.pickle_file) == 1:
|
|
||||||
dis(args.pickle_file[0], args.output, None,
|
|
||||||
args.indentlevel, annotate)
|
|
||||||
else:
|
else:
|
||||||
|
annotate = 30 if args.annotate else 0
|
||||||
memo = {} if args.memo else None
|
memo = {} if args.memo else None
|
||||||
for f in args.pickle_file:
|
if args.output is None:
|
||||||
preamble = args.preamble.format(name=f.name)
|
output = sys.stdout
|
||||||
args.output.write(preamble + '\n')
|
else:
|
||||||
dis(f, args.output, memo, args.indentlevel, annotate)
|
output = open(args.output, 'w')
|
||||||
|
try:
|
||||||
|
for arg in args.pickle_file:
|
||||||
|
if len(args.pickle_file) > 1:
|
||||||
|
name = '<stdin>' if arg == '-' else arg
|
||||||
|
preamble = args.preamble.format(name=name)
|
||||||
|
output.write(preamble + '\n')
|
||||||
|
if arg == '-':
|
||||||
|
dis(sys.stdin.buffer, output, memo, args.indentlevel, annotate)
|
||||||
|
else:
|
||||||
|
with open(arg, 'rb') as f:
|
||||||
|
dis(f, output, memo, args.indentlevel, annotate)
|
||||||
|
finally:
|
||||||
|
if output is not sys.stdout:
|
||||||
|
output.close()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix resource warnings for unclosed files in :mod:`pickle` and
|
||||||
|
:mod:`pickletools` command line interfaces.
|
Loading…
Reference in a new issue