Closed
Description
Feature or enhancement
Proposal:
This may indicate accidental data loss.
Ways to make sure all data is written:
- Use the file-like object as a “With Statement Context Manager”.
- All objects which inherit from IOBase support this.
BufferedIOBase
,BufferedWriter
, andGzipFile
all support this.- Ensures
.close()
is called in both exception and regular cases.
- Ensure
.close()
is always called which flushes data before closing. - If the underlying stream need to be kept open, use
.detach()
Since 3.12 flushing has been necessary in GzipFile (see gh-105808 which was a release blocker), this makes that more visible. Users have been encountering as they upgrade to 3.12 (ex. gh-129726).
There are a number of cases of unclosed file-like objects being deleted in CPython libraries and the test suite. This issue includes resolving those cases where the new ResourceWarning is emitted.
cc: @vstinner
Has this already been discussed elsewhere?
No response given