-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). Recognized GitHub usernameWe couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames: This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
Using both pickle and pickletools from the command line raised a resource warning indicating that the file objects were not closed after use.
cb64f86
to
f09f836
Compare
@@ -1814,4 +1814,5 @@ def _test(): | |||
import pprint | |||
for f in args.pickle_file: | |||
obj = load(f) | |||
f.close() |
There was a problem hiding this comment.
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'>
There was a problem hiding this comment.
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.
File objects were not closed after reading their contents resulting in the following resource warning:
This change ensures that the file is closed after reading the contents.
closes: https://bugs.python.org/issue41395
https://bugs.python.org/issue41395