Skip to content
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

bpo-41395: Register a cleanup function to close files for FileType objects in argparse #32257

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Copy link

@achhina achhina commented Apr 2, 2022

BPO-41395

  • Signed CLA
  • Ran tests and make patchcheck
  • Added News Entry

Hello! First-time contribution, I've read through what I believe to be the relevant sections in the Python Developer's Guide, however, feel free to let me know if there is something I missed.

This is a follow-up to PR-21702, where it was suggested to address the ResourceWarning issue directly in argparse itself. However, as that was discussed back in 2020, I've attempted to take a stab at it myself! After reading a suggestion from @facundobatista, I have added a file.close cleanup function with atexit for files opened with FileType objects, to ensure they are always closed.

Previously:

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

❯ ./python.exe -Wall -m pickle p1 p2
/user: ResourceWarning: unclosed file <_io.BufferedReader name='p1'>
  value = [self._get_value(action, v) for v in arg_strings]
ResourceWarning: Enable tracemalloc to get the object allocation traceback
usage: pickle.py [-h] [-t] [-v] [pickle_file ...]
pickle.py: error: argument pickle_file: can't open 'p2': [Errno 2] No such file or directory: 'p2'

With Patch:

❯ ./python.exe -Wall -m pickle p1   
{'a': 1}

❯ ./python.exe -Wall -m pickle p1 p2
usage: pickle.py [-h] [-t] [-v] [pickle_file ...]
pickle.py: error: argument pickle_file: can't open 'p2': [Errno 2] No such file or directory: 'p2'

https://bugs.python.org/issue41395

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

@the-knights-who-say-ni the-knights-who-say-ni commented Apr 2, 2022

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).

CLA Missing

Our records indicate the following people have not signed the CLA:

@achhina

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@achhina
Copy link
Author

@achhina achhina commented Apr 2, 2022

CLA Missing

Our records indicate the following people have not signed the CLA:

@achhina

If you have recently signed the CLA, please wait at least one business day
before our records are updated

I had signed the CLA last night, so just waiting for it to be processed.

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