Title: StopIteration from a generator that returns a value has no `__context__` · Issue #162576 · python/cpython · GitHub
Open Graph Title: StopIteration from a generator that returns a value has no `__context__` · Issue #162576 · python/cpython
X Title: StopIteration from a generator that returns a value has no `__context__` · Issue #162576 · python/cpython
Description: Bug report Bug description: When a generator returns a value other than None while an exception is being handled, the StopIteration raised by next() has no __context__, and its traceback has no "During handling of the above exception" pa...
Open Graph Description: Bug report Bug description: When a generator returns a value other than None while an exception is being handled, the StopIteration raised by next() has no __context__, and its traceback has no "Du...
X Description: Bug report Bug description: When a generator returns a value other than None while an exception is being handled, the StopIteration raised by next() has no __context__, and its traceback has no &qu...
Opengraph URL: https://github.com/python/cpython/issues/162576
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"StopIteration from a generator that returns a value has no `__context__`","articleBody":"# Bug report\n\n### Bug description:\n\nWhen a generator returns a value other than `None` while an exception is being handled, the `StopIteration` raised by `next()` has no `__context__`, and its traceback has no \"During handling of the above exception\" part. With a bare `return` it has the context:\n\n```python\ndef g(v):\n return v\n yield\nfor v in (None, 5):\n try:\n try:\n raise KeyError(\"handled\")\n except KeyError:\n next(g(v))\n except StopIteration as e:\n print(repr(v), repr(e.__context__))\n```\n\n```\nNone KeyError('handled')\n5 None\n```\n\nThe docs say \"When raising a new exception while another exception is already being handled, the new exception's `__context__` attribute is automatically set to the handled exception\" ([Exception context](https://docs.python.org/dev/builtins/exceptions.html#exception-context)). Since gh-128287, `_PyGen_SetStopIterationValue` (`Objects/genobject.c`) creates the exception itself and sets it with `PyErr_SetRaisedException`, which doesn't chain; `PyErr_SetObject(PyExc_StopIteration, exc)` with the instance would chain and still avoid the tuple fast path. I found it while writing a Lean spec of Python.\n\n### CPython versions tested on:\n\n3.15, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n","author":{"url":"https://github.com/Robertboy18","@type":"Person","name":"Robertboy18"},"datePublished":"2026-11-24T17:34:31Z","url":"https://github.com/python/cpython/issues/162576"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8ec9b01b-b3b1-e862-2918-fc0817da207c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B980:B0F20:AA2E3A:E41F98:6B064206 |
| html-safe-nonce | 0d3437d44c2ab14bb4219cf98a06e8d5c2efff13a571163166c2d05192590258 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTgwOkIwRjIwOkFBMkUzQTpFNDFGOTg6NkIwNjQyMDYiLCJ2aXNpdG9yX2lkIjoiNzY3Mjg4MjIxMjM0NTEyMzkwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 44adfe6ec2651a6e9dfa296835c3e1c72eb3972fcfd24ecdce750153c45335cb |
| hovercard-subject-tag | issue:6587721628 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/162576/issue_layout |
| twitter:image | https://opengraph.githubassets.com/41a6bfa2bdadac8d8ba4d40b58a1dfd5a212a72a028833dd8a5596480f2d156d/python/cpython/issues/162576 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/41a6bfa2bdadac8d8ba4d40b58a1dfd5a212a72a028833dd8a5596480f2d156d/python/cpython/issues/162576 |
| og:image:alt | Bug report Bug description: When a generator returns a value other than None while an exception is being handled, the StopIteration raised by next() has no __context__, and its traceback has no "Du... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Robertboy18 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1bff4f6bfaa96dc9c42cacf315424c24ebb548b06142ced5e3aaccb2a42dd652 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ac613d3698d3d8565651455086e1f74da07bf277 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width