Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ResourceWarning emitted whenio.TextIOWrapper object yielded from nested contextmanager #144276

Open
Labels
@danielhollas

Description

@danielhollas

Bug report

Bug description:

The following code triggers a ResourceWarning

importioimporttempfileimportcontextlib@contextlib.contextmanagerdefmy_wrapped_open():withtempfile.NamedTemporaryFile('bw')ashandle:handle.write(b'content')handle.seek(0)yieldio.TextIOWrapper(handle,encoding='utf-8')withmy_wrapped_open():pass
uvx python@3.15 -Wd reproduce.pycpython-3.15.0a5-linux-x86_64-gnu/lib/python3.15/tempfile.py:520: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/tmphv2hjg_t' encoding='utf-8'>  return func(*args, **kwargs)ResourceWarning: Enable tracemalloc to get the object allocation traceback

The ResourceWarning disappears if the TextIO object is first assigned to a variable before yielding

@contextlib.contextmanagerdefmy_wrapped_open():withtempfile.NamedTemporaryFile('bw')ashandle:handle.write(b'content')handle.seek(0)textio=io.TextIOWrapper(handle,encoding='utf-8')yieldtextio

It also disappears if the outer context is assigned to a variable like this

withmy_wrapped_open()as_:pass

I am not sure if this is a bug in CPython, but it is for sure a surprising behaviour that we do not fully understand.

This is a full stack trace when enabling tracemalloc

export PYTHONTRACEMALLOC=20uvx python@3.15 -Wd reproduce.pycpython-3.15.0a5-linux-x86_64-gnu/lib/python3.15/tempfile.py:520: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/tmpq48qq4rk' encoding='utf-8'>  return func(*args, **kwargs)Object allocated at (most recent call last):  File "/home/hollas/atmospec/aiida-core/reproduce.py", lineno 13    with my_wrapped_open():  File "/home/hollas/.local/share/uv/python/cpython-3.15.0a5-linux-x86_64-gnu/lib/python3.15/contextlib.py", lineno 141    return next(self.gen)  File "/home/hollas/atmospec/aiida-core/reproduce.py", lineno 11    yield io.TextIOWrapper(handle, encoding='utf-8')

CPython versions tested on:

3.15, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp