-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Closed
Closed
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
When using tempfile.TemporaryFile a ResourceWarning should be emitted with the source tracing back to user code. Instead it currently traces back to the _io.open invocation inside the tempfile module and is useless for tracking down the code that created the tempfile.
Pitch
Take the following foo.py:
import tempfile
def main():
fp = tempfile.TemporaryFile()
if __name__ == '__main__':
main()I would expect the ResourceWarning to point to the line fp = tempfile.TemporaryFile(). Instead I see the following:
❯ PYTHONTRACEMALLOC=1 PYTHONWARNINGS=default python3 -m foo
/Users/michael/work/foo.py:9: ResourceWarning: unclosed file <_io.BufferedRandom name=3>
main()
Object allocated at (most recent call last):
File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/tempfile.py", lineno 657
file = _io.open(dir, mode, buffering=buffering,
Note that it's pointing at the tempfile module itself which is worthless other than to tell me it's a tempfile that's causing the issue - however for me it's in library code and I have no idea which library!
Please modify tempfile to properly track the source and emit a better warning.
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Projects
Status
Done

