diff --git a/Lib/pickle.py b/Lib/pickle.py index 4f5ad5b71e8899..2508ad8aa2ac1a 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1810,5 +1810,6 @@ def _test(): else: import pprint for f in args.pickle_file: - obj = load(f) + with f: + obj = load(f) pprint.pprint(obj) diff --git a/Lib/pickletools.py b/Lib/pickletools.py index 95706e746c9870..10a4c815b497e7 100644 --- a/Lib/pickletools.py +++ b/Lib/pickletools.py @@ -2880,11 +2880,14 @@ def _test(): if not args.pickle_file: parser.print_help() elif len(args.pickle_file) == 1: - dis(args.pickle_file[0], args.output, None, - args.indentlevel, annotate) + with args.pickle_file[0], args.output: + dis(args.pickle_file[0], args.output, None, + args.indentlevel, annotate) else: memo = {} if args.memo else None - for f in args.pickle_file: - preamble = args.preamble.format(name=f.name) - args.output.write(preamble + '\n') - dis(f, args.output, memo, args.indentlevel, annotate) + with args.output: + for f in args.pickle_file: + with f: + preamble = args.preamble.format(name=f.name) + args.output.write(preamble + '\n') + dis(f, args.output, memo, args.indentlevel, annotate) diff --git a/Misc/ACKS b/Misc/ACKS index 12335c911ae42a..9a1d4fef1a65e2 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1240,6 +1240,7 @@ Zubin Mithra Florian Mladitsch Kevin Modzelewski Doug Moen +Amir Mohammadi Jakub Molinski Juliette Monsel Paul Monson diff --git a/Misc/NEWS.d/next/Library/2020-08-02-11-40-31.bpo-41395.NR20Rh.rst b/Misc/NEWS.d/next/Library/2020-08-02-11-40-31.bpo-41395.NR20Rh.rst new file mode 100644 index 00000000000000..95dd0881948fcd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-08-02-11-40-31.bpo-41395.NR20Rh.rst @@ -0,0 +1,2 @@ +Use context manager to close filetype objects automatically and avoid +ResourceWarning. Patch contributed by Amir Mohammadi.