Skip to content

NamedTemporaryFile doesn't issue a ResourceWarning when left unclosed on POSIX #126639

Description

@graingert

Bug report

Bug description:

import sys
import tempfile


def main():
    tempfile.NamedTemporaryFile()

if __name__ == "__main__":
    sys.exit(main())

when run with python -Werror demo.py nothing happens

if you open a file normally you get a ResourceWarning:

import sys
import tempfile


def main():
    open("example", "w")

if __name__ == "__main__":
    sys.exit(main())
 graingert@conscientious  ~/projects/temp_file_resource_warnings  python -Werror demo2.py
Exception ignored in: <_io.FileIO name='example' mode='wb' closefd=True>
Traceback (most recent call last):
  File "/home/graingert/projects/temp_file_resource_warnings/demo2.py", line 6, in main
    open("example", "w")
ResourceWarning: unclosed file <_io.TextIOWrapper name='example' mode='w' encoding='UTF-8'>

it appears that you do get a ResourceWarning on windows, but I've only seen it in CI I havn't reproduced locally yet

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. graingert commented on Nov 10, 2024

    @graingert
    ContributorAuthor

    I think this __del__ needs to issue the resource warning:

    cpython/Lib/tempfile.py

    Lines 471 to 472 in 450db61

    def __del__(self):
    self.cleanup()

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 10, 2024
  3. picnixz commented on Nov 10, 2024

    @picnixz
  4. graingert commented on Nov 10, 2024

    @graingert
    ContributorAuthor

    I don't see how that's related? The resource delete on __del__ is a last resort, and it needs to issue a ResourceWarning so that code that fails to finalise explicitly can be detected

  5. picnixz commented on Nov 10, 2024

    @picnixz
    Member

    Ah sorry, I misunderstood the "open again" in the sense that you can leave the descriptor hanging. My bad. However, non-named temporary files say:

    It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected).

    Should implicit close emit a warning in this case? Actually, why is there a warning on Windows?

  6. graingert commented on Nov 10, 2024

    @graingert
    ContributorAuthor

    Should implicit close emit a warning in this case?

    Yes, all implicit closing of resources on GC should issue a ResourceWarning

  7. graingert commented on Nov 10, 2024

    @graingert
    ContributorAuthor

    I suspect the way to go here is to replace _TemporaryFileCloser with a weakref.finalize

  8. tomasr8 commented on Nov 10, 2024

    @tomasr8
    Member

    I suspect the way to go here is to replace _TemporaryFileCloser with a weakref.finalize

    Looks like it, TemporaryDirectory is doing something similar:

    cpython/Lib/tempfile.py

    Lines 885 to 888 in 450db61

    self._finalizer = _weakref.finalize(
    self, self._cleanup, self.name,
    warn_message="Implicitly cleaning up {!r}".format(self),
    ignore_errors=self._ignore_cleanup_errors, delete=self._delete)

  9. added 2 commits that reference this issue on Nov 11, 2024
  10. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Nov 11, 2024
  11. added a commit that references this issue on Dec 18, 2024
  12. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Dec 19, 2024
  13. picnixz commented on Dec 19, 2024

    @picnixz
    Member

    Categorizing it as a feature as per #126677 (comment).

  14. added a commit that references this issue on Jan 8, 2025
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