-
-
Notifications
You must be signed in to change notification settings - Fork 34k
Open
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-IOtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
The following code triggers a ResourceWarning
import io
import tempfile
import contextlib
@contextlib.contextmanager
def my_wrapped_open():
with tempfile.NamedTemporaryFile('bw') as handle:
handle.write(b'content')
handle.seek(0)
yield io.TextIOWrapper(handle, encoding='utf-8')
with my_wrapped_open():
pass❯ uvx [email protected] -Wd reproduce.py
cpython-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 tracebackThe ResourceWarning disappears if the TextIO object is first assigned to a variable before yielding
@contextlib.contextmanager
def my_wrapped_open():
with tempfile.NamedTemporaryFile('bw') as handle:
handle.write(b'content')
handle.seek(0)
textio = io.TextIOWrapper(handle, encoding='utf-8')
yield textioIt also disappears if the outer context is assigned to a variable like this
with my_wrapped_open() as _:
passI 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=20
❯ uvx [email protected] -Wd reproduce.py
cpython-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
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-IOtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error