Skip to content

gh-108668: Preserve exception state during delegated throw - #157235

Open
joaogabriel15 wants to merge 1 commit into
python:mainfrom
joaogabriel15:fix/preserve-delegated-exception-state
Open

gh-108668: Preserve exception state during delegated throw#157235
joaogabriel15 wants to merge 1 commit into
python:mainfrom
joaogabriel15:fix/preserve-delegated-exception-state

Conversation

@joaogabriel15

@joaogabriel15 joaogabriel15 commented Sep 9, 2026

Copy link
Copy Markdown

Fixes #108668.

When throw() is forwarded through yield from or await, the delegating generator is linked into the frame stack but its handled-exception state is not linked into tstate->exc_info. The delegated code can consequently inherit the caller's exception instead of the delegating generator's exception.

Link and restore gi_exc_state around both native generator/coroutine delegation and calls to a custom iterator's throw method.

Regression tests cover native generators, a custom iterator wrapper, nested await, and restoration of the caller's handled exception. All three regression scenarios fail on the original Windows debug build and pass with the change.

Validation: Windows x64 debug build; test_yield_from, test_generators, test_coroutines, test_asyncgen, test_genexps, test_exceptions, test_contextlib, and test_asyncio. Result: SUCCESS, 3,163 tests run, 92 skipped; 41 test files passed and the Unix-only file was skipped.

@python-cla-bot

python-cla-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exc_info can get lost when throwing into an await or yield from

1 participant