From 1be2e8d4f585969eab7812ce102d284ced0bddf5 Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 11:34:57 +0430 Subject: [PATCH 1/7] use context manager to close filetype objects and avoid ResourceWarning --- Lib/pickle.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index d7adc162c98de9..ce45a5007f58a9 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1812,6 +1812,7 @@ def _test(): parser.print_help() else: import pprint - for f in args.pickle_file: - obj = load(f) - pprint.pprint(obj) + for pickle_file in args.pickle_file: + with pickle_file as f: + obj = load(f) + pprint.pprint(obj) From 3d8113e09b9704b5107fe44460d9e6c04b315ec1 Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 11:37:12 +0430 Subject: [PATCH 2/7] use context manager to close filetype objects and avoid ResourceWarning --- Lib/pickletools.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Lib/pickletools.py b/Lib/pickletools.py index 95706e746c9870..73961d27da54fe 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] as pickle_file, args.output as output: + dis(pickle_file, 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 as output: + for pickle_file in args.pickle_file: + with pickle_file as f: + preamble = args.preamble.format(name=f.name) + output.write(preamble + '\n') + dis(f, args.output, memo, args.indentlevel, annotate) From 733a085af0d61d6e356cf1509f9a2e5fd2e82e63 Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 11:41:11 +0430 Subject: [PATCH 3/7] add news --- .../next/Library/2020-08-02-11-40-31.bpo-41395.NR20Rh.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-08-02-11-40-31.bpo-41395.NR20Rh.rst 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..89f5ba0c3efcde --- /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. From 2b4c19229b562f6facbccfd0efeffe65adfeb6a0 Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 12:07:38 +0430 Subject: [PATCH 4/7] update ACKS entry --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 37cf7af0c2ec51..97e9162d671910 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1148,6 +1148,7 @@ Tim Mitchell Zubin Mithra Florian Mladitsch Doug Moen +Amir Mohammadi Jakub Molinski Juliette Monsel Paul Monson From 4b52ff37905a114c01ae16ff15d562212d62dfeb Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 16:47:32 +0430 Subject: [PATCH 5/7] move pprint outside of context manager --- Lib/pickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index ce45a5007f58a9..f5d42604a0ec5e 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1815,4 +1815,4 @@ def _test(): for pickle_file in args.pickle_file: with pickle_file as f: obj = load(f) - pprint.pprint(obj) + pprint.pprint(obj) From ce19169cff3acad61468dee469c5e4cbcd6a0b2d Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 20:29:18 +0430 Subject: [PATCH 6/7] simplify `with` statements --- Lib/pickle.py | 4 ++-- Lib/pickletools.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index f5d42604a0ec5e..a0de2a1084bc57 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1812,7 +1812,7 @@ def _test(): parser.print_help() else: import pprint - for pickle_file in args.pickle_file: - with pickle_file as f: + for f in args.pickle_file: + with f: obj = load(f) pprint.pprint(obj) diff --git a/Lib/pickletools.py b/Lib/pickletools.py index 73961d27da54fe..10a4c815b497e7 100644 --- a/Lib/pickletools.py +++ b/Lib/pickletools.py @@ -2880,14 +2880,14 @@ def _test(): if not args.pickle_file: parser.print_help() elif len(args.pickle_file) == 1: - with args.pickle_file[0] as pickle_file, args.output as output: - dis(pickle_file, output, None, + 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 - with args.output as output: - for pickle_file in args.pickle_file: - with pickle_file as f: + with args.output: + for f in args.pickle_file: + with f: preamble = args.preamble.format(name=f.name) - output.write(preamble + '\n') + args.output.write(preamble + '\n') dis(f, args.output, memo, args.indentlevel, annotate) From 8991975f0d5b7f116fbeaa01378a7575bed28535 Mon Sep 17 00:00:00 2001 From: amiremohamadi Date: Sun, 2 Aug 2020 20:29:58 +0430 Subject: [PATCH 7/7] update news entry --- .../next/Library/2020-08-02-11-40-31.bpo-41395.NR20Rh.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 89f5ba0c3efcde..95dd0881948fcd 100644 --- 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 @@ -1,2 +1,2 @@ Use context manager to close filetype objects automatically and avoid -ResourceWarning. +ResourceWarning. Patch contributed by Amir Mohammadi.