]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-125862: Keep ContextDecorator open across generator/coroutine execution (GH-136212)
authorAlex Grönholm <alex.gronholm@nextday.fi>
Tue, 28 Apr 2026 05:26:38 +0000 (08:26 +0300)
committerGitHub <noreply@github.com>
Tue, 28 Apr 2026 05:26:38 +0000 (05:26 +0000)
commit29251396a9d4a91255c8f960d22c027887d40543
treef14989515e46a09c7273b68c2bc6204c9b202848
parentc8799f137a90f8fdfff4439969c09d85ef4bb8b0
gh-125862: Keep ContextDecorator open across generator/coroutine execution (GH-136212)

ContextDecorator and AsyncContextDecorator (and therefore @contextmanager
and @asynccontextmanager used as decorators) now detect generator,
coroutine, and asynchronous generator functions and emit a wrapper of the
matching kind, so the context manager spans iteration or await rather than
just the call that constructs the lazy object.  Wrapped generators are
explicitly closed when iteration ends.

For asynchronous generator wrappers, values passed via asend() and
exceptions via athrow() are not forwarded to the wrapped generator.

AsyncContextDecorator now also accepts synchronous functions and
generators, returning an asynchronous wrapper; ContextDecorator remains
the recommended choice for those.

inspect.isgeneratorfunction(), iscoroutinefunction(), and
isasyncgenfunction() now return True for the decorated result when the
input is of that kind.

---------

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Doc/library/contextlib.rst
Doc/whatsnew/3.15.rst
Lib/contextlib.py
Lib/test/test_contextlib.py
Lib/test/test_contextlib_async.py
Misc/NEWS.d/next/Library/2025-07-02-17-01-17.gh-issue-125862.WgFYj3.rst [new file with mode: 0644]