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

Conversation

s3bw
Copy link
Contributor
@s3bw s3bw commented Jul 29, 2020

File objects were not closed after reading their contents resulting in the following resource warning:

$ ./python.exe -Wall -m pickle mypickle
{'a': 1}
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle'>

This change ensures that the file is closed after reading the contents.

$ ./python.exe -Wall -m pickle mypickle
{'a': 1}

closes: https://bugs.python.org/issue41395

https://bugs.python.org/issue41395

@the-knights-who-say-ni
Copy link

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 username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@Foxyblue

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!

s3bw added 2 commits July 29, 2020 19:18
    Using both pickle and pickletools from the
    command line raised a resource warning
    indicating that the file objects were not
    closed after use.
@s3bw s3bw force-pushed the bpo-41395-close-file branch from cb64f86 to f09f836 Compare July 29, 2020 18:19
@s3bw s3bw changed the title bpo-41395: Close file object after reading contents bpo-41395: Close file objects in pickle and pickletools Jul 29, 2020
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0