X Tutup
The Wayback Machine - https://web.archive.org/web/20250827144034/https://github.com/python/cpython/issues/103070
Skip to content

tempfile.TemporaryFile() emits a ResourceWarning that does not trace back to user code #103070

@mmerickel

Description

@mmerickel

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

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup