8000 bpo-41395: Close file objects in pickle and pickletools by s3bw · Pull Request #21676 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41395: Close file objects in pickle and pickletools #21676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
s3bw committed Jul 29, 2020
commit 8c3991b332b64dcbaf530105c893b13a5fe507fc
1 change: 1 addition & 0 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,4 +1814,5 @@ def _test():
import pprint
for f in args.pickle_file:
obj = load(f)
f.close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not be called if load causes exception for cases like invalid payload. Similar case with the approach in pickletools too. Sometimes even if one file causes exception the other files in the loop will not be iterated and closed.

$ echo invalid > mypickle.pickle
$ ./python -Wall -m pickle mypickle.pickle
Traceback (most recent call last):
  File "/root/cpython/Lib/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/root/cpython/Lib/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/root/cpython/Lib/pickle.py", line 1816, in <module>
    obj = load(f)
_pickle.UnpicklingError: could not find MARK
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle.pickle'>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. good catch. I'll use another approach.

pprint.pprint(obj)
2 changes: 2 additions & 0 deletions Lib/pickletools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
0