@@ -467,12 +467,40 @@ Functions and classes provided:
467467 statements. If this is not the case, then the original construct with the
468468 explicit :keyword: `!with ` statement inside the function should be used.
469469
470+ When the decorated callable is a generator function, coroutine function, or
471+ asynchronous generator function, the returned wrapper is of the same kind
472+ and keeps the context manager open for the lifetime of the iteration or
473+ await rather than only for the call that creates the generator or coroutine
474+ object. Wrapped generators and asynchronous generators are explicitly
475+ closed when iteration ends, as if by :func: `closing ` or :func: `aclosing `.
476+
477+ .. note ::
478+ For asynchronous generators the wrapper re-yields each value with
479+ ``async for ``; values sent with :meth: `~agen.asend ` and exceptions
480+ thrown with :meth: `~agen.athrow ` are not forwarded to the wrapped
481+ generator.
482+
470483 .. versionadded :: 3.2
471484
485+ .. versionchanged :: next
486+ Decorating a generator function, coroutine function, or asynchronous
487+ generator function now keeps the context manager open across iteration
488+ or await. Previously the context manager exited as soon as the
489+ generator or coroutine object was created.
490+
472491
473492.. class :: AsyncContextDecorator
474493
475- Similar to :class: `ContextDecorator ` but only for asynchronous functions.
494+ Similar to :class: `ContextDecorator `, but the context manager is entered
495+ and exited with :keyword: `async with `. Decorate coroutine functions and
496+ asynchronous generator functions with this class; the returned wrapper is
497+ of the same kind.
498+
499+ .. note ::
500+ Synchronous functions and generators are accepted, but the wrapper is
501+ always asynchronous, so the decorated callable must then be awaited or
502+ iterated with ``async for ``. If that change of calling convention is
503+ not intended, use :class: `ContextDecorator ` instead.
476504
477505 Example of ``AsyncContextDecorator ``::
478506
@@ -510,6 +538,13 @@ Functions and classes provided:
510538
511539 .. versionadded :: 3.10
512540
541+ .. versionchanged :: next
542+ Decorating an asynchronous generator function now keeps the context
543+ manager open across iteration. Previously the context manager exited
544+ as soon as the generator object was created. Synchronous functions
545+ and synchronous generator functions are also now accepted, with an
546+ asynchronous wrapper returned.
547+
513548
514549.. class :: ExitStack()
515550
0 commit comments