Skip to content

gh-162576: fix StopIteration.__context__ lost when generator returns … - #162870

Open
BHUVANSH855 wants to merge 1 commit into
python:mainfrom
BHUVANSH855:fix/162576
Open

BHUVANSH855 wants to merge 1 commit into
python:mainfrom
BHUVANSH855:fix/162576

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

_PyGen_SetStopIterationValue() in Objects/genobject.c was using PyErr_SetRaisedException() which skips implicit exception chaining. Switched to PyErr_SetObject() with the already-constructed instance which bypasses the tuple fast-path and gets __context__ chaining for free. Added Py_DECREF(exc) since PyErr_SetObject() doesn't steal the reference.

Added regression test test_return_value_stopiteration_context in Lib/test/test_generators.py.

fixes issue gh-128287

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.

1 participant