New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add convenient C API for "raise ... from ..." #72596
Comments
Sometimes after catching an exception we need to raise new exception of specific type, but save the information about current exception. In Python the syntax "raise ... from ..." serves this purpose (PEP-3134). But in C the code that implements this is too cumbersome. Proposed patch adds private convenient function that raises an exception and sets previously raised exception as its __context__ and __cause__ attributes. This is a generalized version of gen_chain_runtime_error() from Objects/genobject.c. It could be reused in three other places: Objects/abstract.c, Objects/unicodeobject.c, and Modules/zipimport.c (currently only __context__ is set, but setting __cause__ looks preferable), and in new code for bpo-28214. Maybe there are other opportunities. |
Serhiy, how does this version compare/relate to the previous discussion in http://bugs.python.org/issue23188 ? |
Fixed error message in tests. |
The patch looks good to me Serhiy, but I don't think it makes sense to apply this to 3.5 - it touches quite a few places in the code, and there's no reason to risk introducing a regression into a 3.5 maintenance release for a pure maintainer convenience function. Adding it to 3.6+ will be sufficient to help resolve bpo-28214. |
I have read and executed the tests and for me the patch is good. |
I see the function is private; is there any concern with extending the PyErr_* API? I think it'd make sense to expose it, since it's a convenient way to do in C what can already be done easily in Python, but I don't actually have a good reason :) Other than this small nitpick, this LGTM. (Alternatively, we can keep it private for 3.6 and revise that decision for 3.7) |
I'm not sure about the function name. Does anybody want to suggest better name? |
bpo-23188 (referenced above) covers making this a public API for 3.7. I think it's a good idea, but it's too late to add it to 3.6, and getting the documentation right is going to require some discussion (as using this new API isn't necessary if you're already in a Python exception handler and are directly or indirectly calling PyErr_SetObject, but *is* necessary if you're wanting to chain with a just set C level exception). |
New changeset 81666d3e4a37 by Serhiy Storchaka in branch '3.5': New changeset 23a1d9ec35d5 by Serhiy Storchaka in branch '3.6': New changeset e853492da42c by Serhiy Storchaka in branch 'default': |
New changeset 969c8bfe8872 by Serhiy Storchaka in branch '3.6': New changeset 2119cb0beace by Serhiy Storchaka in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: