Skip to content

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

Closed
@graingert

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions