GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-125862: Context decorator support for generators and async functions by agronholm · Pull Request #136212 · python/cpython

Conversation

@agronholm

@agronholm agronholm commented Jul 2, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Issue: Improve contextlib.ContextDecorator support for generators and async functions #125862

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@agronholm
pythongh-125862: Improve context decorator support for generators and…
bd00fcb
… async functions
@agronholm
agronholm requested a review from 1st1 as a code owner July 2, 2025 16:55
@bedevere-app bedevere-app Bot added the awaiting review label Jul 2, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jul 2, 2025
Improve contextlib.ContextDecorator support for generators and async functions #125862
Closed
agronholm and others added 2 commits July 2, 2025 19:55
@agronholm
Merge branch 'main' into fix-issue-125862
1a0c100
@blurb-it
📜🤖 Added by blurb_it.
9b3ba13
@agronholm

agronholm commented Jul 2, 2025

Copy link
Copy Markdown
Contributor Author

Ack. WASI tests are failing because I used asyncio. Gotta work around that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

agronholm added 2 commits July 2, 2025 20:55
@agronholm
Manually iterate coroutines to avoid asyncio use
8535a21
@agronholm
Merge branch 'main' into fix-issue-125862
6a68ffb
Zac-HD
Zac-HD reviewed Jul 28, 2025
View reviewed changes
Comment thread Lib/contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

agronholm added 5 commits December 8, 2025 15:54
@agronholm
Merge branch 'main' into fix-issue-125862
bbaee0c
@agronholm
Merge branch 'main' into fix-issue-125862
93b30f4
@agronholm
Make sure we at least try to close the generators
1fd52d5
@agronholm
Merge remote-tracking branch 'fork/fix-issue-125862' into fix-issue-1…
e32191e
…25862
@agronholm
Use (a)closing
65c8f50
@agronholm

agronholm commented Dec 9, 2025

Copy link
Copy Markdown
Contributor Author

@ambv I recall that back in July in Prague you said you'd take a look at this PR, yes? Would now be a good time?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@agronholm
Merge branch 'main' into fix-issue-125862
8762048
@github-actions

github-actions Bot commented Apr 26, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 26, 2026
@gpshead
Merge remote-tracking branch 'origin/main' into fix-issue-125862
6fb5bcb
@gpshead gpshead self-assigned this Apr 27, 2026
gpshead added 7 commits April 27, 2026 22:39
@gpshead
Use _private imports
1433534
@gpshead
performance: only define inner funcs on the branch that uses them, la…
a8ee60d
…zy import inspect
@gpshead
do not delete that newline (small diff)
46ffa37
@gpshead
reword reST news entry
ed6d9fa
@gpshead
Preserve generator return value; expand decorator test coverage
876704a
ContextDecorator's generator wrapper now uses ``return (yield from gen)``
so a wrapped generator's StopIteration value is not lost.

Tests: drop the unused run_test() helper, normalize quote style, and add
exception-path and early-close coverage for the new generator and async
generator wrappers so closing()/aclosing() behavior is exercised.
@gpshead
Document ContextDecorator generator/coroutine handling
3b30193
Add versionchanged notes to ContextDecorator and AsyncContextDecorator
describing the new behavior when decorating generator, coroutine, and
async generator functions, and call out that asend()/athrow() are not
forwarded through the async generator wrapper.  Reword the
AsyncContextDecorator intro now that synchronous callables are accepted.
@gpshead
Add What's New entry for ContextDecorator generator support
72d4a8e
@gpshead
gpshead requested a review from AA-Turner as a code owner April 27, 2026 23:41
@gpshead gpshead removed the stale Stale PR or inactive for long period of time. label Apr 27, 2026
@gpshead
simplify lazy import syntax
719c55c
@gpshead

gpshead commented Apr 27, 2026

Copy link
Copy Markdown
Member

I'm testing this out. It is technically a behavior change so I want to understand if this is strictly a buggy behavior fix that nothing would've relied on, or something we'll need a backward compatibility with the prior behavior story for.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead

gpshead commented Apr 28, 2026

Copy link
Copy Markdown
Member

okay, I analyzed things. First, a datapoint, we've been using a monkeypatch style equivalent to this PR on our contextlib decorators internally at work across O(NNNN) uses for ~6 months without issue. among other uses that don't use that fixed variant, some re-invent their own correct versions and could be cleaned up, and a few have been silently buggy.

The contextlib behavior before this PR is that the context manager exits before any line of the decorated body runs. Not semantically something code can usefully depend on. There is nothing to deprecate because the decorator was effectively a no-op on these function kinds. unittest.mock.patch already does this dispatch for coroutine functions (since 3.8). This brings ContextDecorator to parity and extends both patterns to generators and async generators.

There are open issues for unittest.mock.patch to expand to work decorating generators: gh-71528 & gh-88893.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gpshead added 3 commits April 28, 2026 04:39
@gpshead
Reword AsyncContextDecorator docs to lead with intended async use
59422a3
State the primary use (coroutine and async generator functions) up front
and move the synchronous-input behavior into a note that points users at
ContextDecorator when the calling convention change is not wanted.
@gpshead
Reword test comments to state the contract, not the mechanism
7026b14
The early-stop tests asserted ordering with comments that named closing()
and aclosing() as the reason; restate as the invariant being tested so
the comments stay correct if the implementation changes.
@gpshead
Test send/throw forwarding for the sync wrapper; pin async limitation
c6eda4a
The sync generator wrapper uses ``yield from`` so .send() and .throw()
reach the wrapped generator; lock that in.  The async generator wrapper
re-yields with ``async for`` so asend()/athrow() do not reach the
wrapped generator; pin that documented behavior so a future change that
starts forwarding will fail and prompt a doc update.
gpshead
gpshead approved these changes Apr 28, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Apr 28, 2026
@gpshead
also add my name in whatsnew
a3cfb1a
@gpshead
gpshead enabled auto-merge (squash) April 28, 2026 05:02
@gpshead
gpshead merged commit 2925139 into python:main Apr 28, 2026
51 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 28, 2026
@agronholm
agronholm deleted the fix-issue-125862 branch April 28, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead gpshead approved these changes
@1st1 1st1 Awaiting requested review from 1st1
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
+1 more reviewer
@Zac-HD Zac-HD Zac-HD left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gpshead gpshead

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@agronholm @gpshead @Zac-HD
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.