You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add convenient C API for "raise ... from ..." #72596
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:
assignee='https://github.com/serhiy-storchaka'closed_at=<Date2016-10-21.14:18:00.453>created_at=<Date2016-10-10.19:50:27.484>labels= ['interpreter-core', 'type-feature', '3.7']
title='Add convenient C API for "raise ... from ..."'updated_at=<Date2016-10-21.14:18:00.452>user='https://github.com/serhiy-storchaka'
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.
I don't know how it is related to bpo-23188. Maybe solving bpo-23188 will make _PyErr_ChainExceptions and the function proposed in this issue redundant. Or will make them simpler. But it looks to me that bpo-23188 is far from the completion.
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.
Vgr255 commented on Oct 18, 2016
Vgr255mannequin
Mannequin
More actions
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)
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).
Activity
serhiy-storchaka commentedon Oct 10, 2016
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.
ncoghlan commentedon Oct 11, 2016
Serhiy, how does this version compare/relate to the previous discussion in http://bugs.python.org/issue23188 ?
serhiy-storchaka commentedon Oct 11, 2016
I don't know how it is related to bpo-23188. Maybe solving bpo-23188 will make _PyErr_ChainExceptions and the function proposed in this issue redundant. Or will make them simpler. But it looks to me that bpo-23188 is far from the completion.
serhiy-storchaka commentedon Oct 18, 2016
Fixed error message in tests.
ncoghlan commentedon Oct 18, 2016
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.
matrixise commentedon Oct 18, 2016
I have read and executed the tests and for me the patch is good.
Vgr255 commentedon Oct 18, 2016
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)
serhiy-storchaka commentedon Oct 18, 2016
I'm not sure about the function name. Does anybody want to suggest better name?
ncoghlan commentedon Oct 19, 2016
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).
python-dev commentedon Oct 21, 2016
New changeset 81666d3e4a37 by Serhiy Storchaka in branch '3.5':
Issue bpo-28410: Keep the traceback of original exception in _PyErr_ChainExceptions().
https://hg.python.org/cpython/rev/81666d3e4a37
New changeset 23a1d9ec35d5 by Serhiy Storchaka in branch '3.6':
Issue bpo-28410: Keep the traceback of original exception in _PyErr_ChainExceptions().
https://hg.python.org/cpython/rev/23a1d9ec35d5
New changeset e853492da42c by Serhiy Storchaka in branch 'default':
Issue bpo-28410: Keep the traceback of original exception in _PyErr_ChainExceptions().
https://hg.python.org/cpython/rev/e853492da42c
python-dev commentedon Oct 21, 2016
New changeset 969c8bfe8872 by Serhiy Storchaka in branch '3.6':
Issue bpo-28410: Added _PyErr_FormatFromCause() -- the helper for raising
https://hg.python.org/cpython/rev/969c8bfe8872
New changeset 2119cb0beace by Serhiy Storchaka in branch 'default':
Issue bpo-28410: Added _PyErr_FormatFromCause() -- the helper for raising
https://hg.python.org/cpython/rev/2119cb0beace