Repository navigation
NamedTemporaryFile doesn't issue a ResourceWarning when left unclosed on POSIX #126639
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Nov 10, 2024 I think this
__del__needs to issue the resource warning:Lines 471 to 472 in 450db61
def __del__(self): self.cleanup() - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Nov 10, 2024 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 detectedAh 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?
Should implicit close emit a warning in this case?
Yes, all implicit closing of resources on GC should issue a ResourceWarning
Reacted by Bénédikt TranI suspect the way to go here is to replace _TemporaryFileCloser with a weakref.finalize
I suspect the way to go here is to replace _TemporaryFileCloser with a weakref.finalize
Looks like it,
TemporaryDirectoryis doing something similar: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) - added3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes
on Nov 11, 2024 - addedtype-featureA feature request or enhancementA feature request or enhancementand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes
on Dec 19, 2024 Categorizing it as a feature as per #126677 (comment).
Bug report
Bug description:
when run with
python -Werror demo.pynothing happensif you open a file normally you get a ResourceWarning:
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