summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-01-05 22:29:31 (GMT)
committerGitHub <noreply@github.com>2024-01-05 22:29:31 (GMT)
commit9cea77126196381e8a8bd23d351d1dec3608372f (patch)
tree61e42e434677365ba1a1060b1c52ff18bb3f3b40
parent6aa86bdcb240501283d1652ee6c54193b32a63b0 (diff)
downloadcpython-9cea77126196381e8a8bd23d351d1dec3608372f.zip
cpython-9cea77126196381e8a8bd23d351d1dec3608372f.tar.gz
cpython-9cea77126196381e8a8bd23d351d1dec3608372f.tar.bz2
[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 bd754b93ca837aa1f239252437a211271d068b71) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Lib/pickle.py10
-rw-r--r--Lib/pickletools.py31
-rw-r--r--Misc/NEWS.d/next/Library/2024-01-01-13-26-02.gh-issue-85567.K4U15m.rst2
3 files changed, 30 insertions, 13 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 1160356..f760bcd 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -1799,7 +1799,7 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='display contents of the pickle files')
parser.add_argument(
- 'pickle_file', type=argparse.FileType('br'),
+ 'pickle_file',
nargs='*', help='the pickle file')
parser.add_argument(
'-t', '--test', action='store_true',
@@ -1815,6 +1815,10 @@ if __name__ == "__main__":
parser.print_help()
else:
import pprint
- for f in args.pickle_file:
- obj = load(f)
+ for fn in args.pickle_file:
+ if fn == '-':
+ obj = load(sys.stdin.buffer)
+ else:
+ with open(fn, 'rb') as f:
+ obj = load(f)
pprint.pprint(obj)
diff --git a/Lib/pickletools.py b/Lib/pickletools.py
index 95706e7..95a77ae 100644
--- a/Lib/pickletools.py
+++ b/Lib/pickletools.py
@@ -2848,10 +2848,10 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='disassemble one or more pickle files')
parser.add_argument(
- 'pickle_file', type=argparse.FileType('br'),
+ 'pickle_file',
nargs='*', help='the pickle file')
parser.add_argument(
- '-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
+ '-o', '--output',
help='the file where the output should be written')
parser.add_argument(
'-m', '--memo', action='store_true',
@@ -2876,15 +2876,26 @@ if __name__ == "__main__":
if args.test:
_test()
else:
- annotate = 30 if args.annotate else 0
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)
else:
+ annotate = 30 if args.annotate else 0
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)
+ if args.output is None:
+ output = sys.stdout
+ else:
+ 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()
diff --git a/Misc/NEWS.d/next/Library/2024-01-01-13-26-02.gh-issue-85567.K4U15m.rst b/Misc/NEWS.d/next/Library/2024-01-01-13-26-02.gh-issue-85567.K4U15m.rst
new file mode 100644
index 0000000..063443e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-01-13-26-02.gh-issue-85567.K4U15m.rst
@@ -0,0 +1,2 @@
+Fix resource warnings for unclosed files in :mod:`pickle` and
+:mod:`pickletools` command line interfaces.