From 8c3991b332b64dcbaf530105c893b13a5fe507fc Mon Sep 17 00:00:00 2001 From: Sebastien Williams-Wynn Date: Wed, 29 Jul 2020 18:13:22 +0100 Subject: [PATCH 1/2] Close file objects after reading contents Using both pickle and pickletools from the command line raised a resource warning indicating that the file objects were not closed after use. --- Lib/pickle.py | 1 + Lib/pickletools.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Lib/pickle.py b/Lib/pickle.py index cbac5f168b45eb..f446afebe54d00 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1814,4 +1814,5 @@ def _test(): import pprint for f in args.pickle_file: obj = load(f) + f.close() pprint.pprint(obj) diff --git a/Lib/pickletools.py b/Lib/pickletools.py index 95706e746c9870..3a6dd510fd5adf 100644 --- a/Lib/pickletools.py +++ b/Lib/pickletools.py @@ -2882,9 +2882,11 @@ def _test(): elif len(args.pickle_file) == 1: dis(args.pickle_file[0], args.output, None, args.indentlevel, annotate) + args.pickle_file[0].close() 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) + f.close() From f09f836ee12a68bc2ba73d7a97f472b8853c3738 Mon Sep 17 00:00:00 2001 From: Sebastien Williams-Wynn Date: Wed, 29 Jul 2020 18:38:54 +0100 Subject: [PATCH 2/2] Add news snippet --- .../next/Library/2020-07-29-18-38-41.bpo-41395.jl9BXl.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-07-29-18-38-41.bpo-41395.jl9BXl.rst diff --git a/Misc/NEWS.d/next/Library/2020-07-29-18-38-41.bpo-41395.jl9BXl.rst b/Misc/NEWS.d/next/Library/2020-07-29-18-38-41.bpo-41395.jl9BXl.rst new file mode 100644 index 00000000000000..a817c1312ba9c9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-29-18-38-41.bpo-41395.jl9BXl.rst @@ -0,0 +1,2 @@ +Close file object after reading contents in pickle and pickletools cli. +This removes the ResourceWarning.