Skip to content

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

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

Merged
merged 2 commits into from
Apr 18, 2022

Conversation

achhina
Copy link
Contributor

@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

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
Contributor Author

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.

@achhina
Copy link
Contributor Author

achhina commented Apr 12, 2022

Hi @facundobatista @serhiy-storchaka,

As both of you had reviewed PR-21702 and this follows up on that, would you be available to review this?

Copy link
Member

@facundobatista facundobatista left a comment

Choose a reason for hiding this comment

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

Hello! Thanks for the work! I annotated a couple of small suggestions.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ghost
Copy link

ghost commented Apr 14, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@achhina
Copy link
Contributor Author

achhina commented Apr 14, 2022

Thanks for taking a look @facundobatista!

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@facundobatista: please review the changes made to this pull request.

@achhina achhina changed the title bpo-41395: Register a cleanup function to close files for FileType objects in argparse gh-85567: Register a cleanup function to close files for FileType objects in argparse Apr 14, 2022
Copy link
Member

@facundobatista facundobatista left a comment

Choose a reason for hiding this comment

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

It looks great! Thanks!

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

Successfully merging this pull request may close these issues.

4 participants