FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-85567: Register a cleanup function to close files for FileType objects in argparse by achhina · Pull Request #32257 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) No extension  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 7 additions & 3 deletions Lib/argparse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
'ZERO_OR_MORE',
]


import atexit as _atexit
import os as _os
import re as _re
import sys as _sys
Expand Down Expand Up @@ -1268,8 +1268,12 @@ def __call__(self, string):

# all other arguments are used as file names
try:
return open(string, self._mode, self._bufsize, self._encoding,
self._errors)
fh = open(string, self._mode, self._bufsize, self._encoding, self._errors)

# Register cleanup function to close file
_atexit.register(fh.close)

return fh
except OSError as e:
args = {'filename': string, 'error': e}
message = _("can't open '%(filename)s': %(error)s")
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ Nicolas Chauvat
Jerry Chen
Michael Chermside
Ingrid Cheung
Adam Chhina
Terry Chia
Albert Chin-A-Young
Adal Chiriliuc
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FileType objects from argparse may not be closed and lead to
ResourceWarning. Register a file.close function with atexit for FileType
objects to ensure they are closed. Patch Contributed by Adam Chhina.

Back | FazBrowse Home | New Git URL