Skip to content

Emit ResourceWarning when GzipFile is deleted with unwritten data #130806

Description

@cmaloney

Feature or enhancement

Proposal:

This may indicate accidental data loss.

Ways to make sure all data is written:

  1. Use the file-like object as a “With Statement Context Manager”.
  2. Ensure .close() is always called which flushes data before closing.
  3. 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

Links to previous discussion of this feature:

#129726 (comment)

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 4, 2025
  2. added a commit that references this issue on Mar 6, 2025
  3. cmaloney commented on Mar 13, 2025

    @cmaloney
    ContributorAuthor

    Working on getting BufferedWriter warning to work, having a hard time as it involves adding a tp_finalize, and that interacts with the existing tp_dealloc which calls _dealloc_warn on itself and the underlying raw (often FileIO), which is how the FileIO close warning is emitted currently. I got it working well for _pyio but not sure how to get everything to fit for _io.Buffered{Writer,Random}. What I currently have. At the moment struggling to figure out whats the right tweaks, particularly for tests which seem to have often evolved separately from the original need, and this changes behavior (ex. _pyio tests using _io by accident, checks for no warning and unraisable exceptions, etc). Wondering if with tp_finalize as it currently works could replace the existing _dealloc_warn / FileIO system with a simpler one...

    While working on that, found the docs around tp_finalize pointed to the deprecated C APIs PyErr_Fetch + PyErr_Restore, made an issue + PR to update to PyErr_GetRaisedException and PyErr_SetRaisedException: gh-131117

    cc: @vstinner

  4. added a commit that references this issue on Mar 13, 2025
  5. added a commit that references this issue on Mar 17, 2025
  6. changed the title [-]Emit ResourceWarning when GzipFile or BufferedWriter are deleted with unwritten data[/-] [+]Emit ResourceWarning when GzipFile is deleted with unwritten data[/+] on Mar 20, 2025
  7. cmaloney commented on Mar 20, 2025

    @cmaloney
    ContributorAuthor

    Reducing the issue to just GzipFile, after a couple attempts at getting BufferedWriter working, have yet to find a reliable way. Is really neat how _dealloc_warn works with the tp_finalize in IOBase.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions