Title: `PyErr_SetObject()` behavior is strange and not as documented. · Issue #101578 · python/cpython · GitHub
Open Graph Title: `PyErr_SetObject()` behavior is strange and not as documented. · Issue #101578 · python/cpython
X Title: `PyErr_SetObject()` behavior is strange and not as documented. · Issue #101578 · python/cpython
Description: Briefly: PyErr_SetObject(exc_type, exc_val) does not create a new exception iff isinstance(exc_val, BaseException), but uses exc_val instead. Callers of PyErr_SetObject() need various workarounds to handle this. The long version: Interna...
Open Graph Description: Briefly: PyErr_SetObject(exc_type, exc_val) does not create a new exception iff isinstance(exc_val, BaseException), but uses exc_val instead. Callers of PyErr_SetObject() need various workarounds t...
X Description: Briefly: PyErr_SetObject(exc_type, exc_val) does not create a new exception iff isinstance(exc_val, BaseException), but uses exc_val instead. Callers of PyErr_SetObject() need various workarounds t...
Opengraph URL: https://github.com/python/cpython/issues/101578
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`PyErr_SetObject()` behavior is strange and not as documented.","articleBody":"Briefly:\r\n`PyErr_SetObject(exc_type, exc_val)` does not create a new exception iff `isinstance(exc_val, BaseException)`, but uses `exc_val` instead.\r\n\r\nCallers of `PyErr_SetObject()` need various workarounds to handle this.\r\n\r\nThe long version:\r\n\r\nInternally CPython handles exceptions as a triple `(type, value, traceback)`, but the language treats exceptions as a single value.\r\n\r\nThis a legacy of the olden days before proper exceptions.\r\nTo handle adding proper exceptions to Python, various error handling functions, specifically `_PyErr_SetObject` still treat exceptions as triples, with the convention that if the value is an exception, then the exception is already normalized.\r\n\r\nOne other oddity is that if `exc_val` is a tuple, it is treated as the `*` arguments to `exc_type` when calling it. So, if `isinstance(exc_val, BaseException)` the desired behavior can be achieved by wrapping `exc_val` in a one-tuple.\r\n\r\nAs a consequence, both `_PyErr_SetKeyError` and `_PyGen_SetStopIterationValue` are a lot more complex than they should be to workaround this behavior.\r\n\r\nWe could make `PyErr_SetObject` act as documented, but that is likely to break C extensions, given how old this behavior is, and that it is relied on throughout CPython.\r\n\r\nCode that does the following is common:\r\n```C\r\n exc = new_foo_exception();\r\n PyErr_SetObject(\u0026PyFooException_Type, exc);\r\n```\r\n\r\nWe could just document the current behavior, but the current behavior is strange.\r\nWhat I suggest is this:\r\n* Create a new API function, PyErr_SetException(exc)` that takes a single exception object.\r\n* Document `PyErr_SetObject()` accurately\r\n* Deprecate the old function\r\n\r\n\r\nThis is an old bug going back to the 2 series.\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-101607\r\n* gh-101962\r\n* gh-102057\r\n* gh-102157\r\n* gh-102702\n* gh-113369\n* gh-113606\n\u003c!-- /gh-linked-prs --\u003e\r\n\r\nAlso relevant:\r\n* gh-102502","author":{"url":"https://github.com/markshannon","@type":"Person","name":"markshannon"},"datePublished":"2023-02-05T11:27:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":34},"url":"https://github.com/101578/cpython/issues/101578"}
| 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:83ee9cc8-a264-51ba-cbaa-64b6881d6483 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 804E:31C581:142FF92:1BAD7E2:69694CDF |
| html-safe-nonce | ddfed690fc9735c53bad5157f8f807fb5c39d98395ac050d5e2439519aac0f90 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDRFOjMxQzU4MToxNDJGRjkyOjFCQUQ3RTI6Njk2OTRDREYiLCJ2aXNpdG9yX2lkIjoiNTc4MjU2OTc0ODA0OTUxMzY5NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ce9bd4cc029d0ccfe84f76b252fc18a3014c97cb7d3047cfd2c36eb22dada2c6 |
| hovercard-subject-tag | issue:1571392578 |
| 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/101578/issue_layout |
| twitter:image | https://opengraph.githubassets.com/35c4401b4e5a1131ed5485480c5e867ec1966ec69935ede7525343abe6f9e512/python/cpython/issues/101578 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/35c4401b4e5a1131ed5485480c5e867ec1966ec69935ede7525343abe6f9e512/python/cpython/issues/101578 |
| og:image:alt | Briefly: PyErr_SetObject(exc_type, exc_val) does not create a new exception iff isinstance(exc_val, BaseException), but uses exc_val instead. Callers of PyErr_SetObject() need various workarounds t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markshannon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 54182691a21263b584d2e600b758e081b0ff1d10ffc0d2eefa51cf754b43b51d |
| 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 | d69ac0477df0f87da03b8b06cebd187012d7a930 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width