classification
Title: Add convenient C API for "raise ... from ..."
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: abarry, matrixise, ncoghlan, python-dev, serhiy.storchaka, vstinner, yselivanov
Priority: normal Keywords: patch

Created on 2016-10-10 19:50 by serhiy.storchaka, last changed 2016-10-21 14:18 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
_PyErr_FormatFromCause.patch serhiy.storchaka, 2016-10-10 19:50 review
_PyErr_FormatFromCause-3.5.patch serhiy.storchaka, 2016-10-18 10:55 Patch for 3.5 review
_PyErr_FormatFromCause-2.patch serhiy.storchaka, 2016-10-18 13:13 review
Messages (11)
msg278441 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-10 19:50
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 issue28214. Maybe there are other opportunities.
msg278451 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-10-11 02:50
Serhiy, how does this version compare/relate to the previous discussion in http://bugs.python.org/issue23188 ?
msg278462 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-11 05:59
I don't know how it is related to issue23188. Maybe solving issue23188 will make _PyErr_ChainExceptions and the function proposed in this issue redundant. Or will make them simpler. But it looks to me that issue23188 is far from the completion.
msg278873 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-18 13:13
Fixed error message in tests.
msg278879 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-10-18 13:50
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 issue 28214.
msg278882 - (view) Author: St├ęphane Wirtel (matrixise) * (Python committer) Date: 2016-10-18 14:08
I have read and executed the tests and for me the patch is good.
msg278893 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-10-18 15:52
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)
msg278900 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-18 16:15
I'm not sure about the function name. Does anybody want to suggest better name?
msg278959 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-10-19 02:47
Issue 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).
msg279125 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-21 13:23
New changeset 81666d3e4a37 by Serhiy Storchaka in branch '3.5':
Issue #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 #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 #28410: Keep the traceback of original exception in _PyErr_ChainExceptions().
https://hg.python.org/cpython/rev/e853492da42c
msg279131 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-21 14:16
New changeset 969c8bfe8872 by Serhiy Storchaka in branch '3.6':
Issue #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 #28410: Added _PyErr_FormatFromCause() -- the helper for raising
https://hg.python.org/cpython/rev/2119cb0beace
History
Date User Action Args
2016-10-21 14:18:00serhiy.storchakasetstatus: open -> closed
assignee: serhiy.storchaka
resolution: fixed
stage: commit review -> resolved
2016-10-21 14:16:11python-devsetmessages: + msg279131
2016-10-21 13:23:03python-devsetnosy: + python-dev
messages: + msg279125
2016-10-19 02:47:16ncoghlansetmessages: + msg278959
2016-10-18 16:15:19serhiy.storchakasetmessages: + msg278900
2016-10-18 15:52:39abarrysetmessages: + msg278893
2016-10-18 15:31:47matrixisesetstage: patch review -> commit review
2016-10-18 14:08:59matrixisesetnosy: + matrixise
messages: + msg278882
2016-10-18 13:50:40ncoghlansetmessages: + msg278879
versions: - Python 3.5
2016-10-18 13:13:17serhiy.storchakasetfiles: + _PyErr_FormatFromCause-2.patch

messages: + msg278873
2016-10-18 10:55:39serhiy.storchakasetfiles: + _PyErr_FormatFromCause-3.5.patch
2016-10-11 05:59:24serhiy.storchakasetmessages: + msg278462
2016-10-11 02:50:14ncoghlansetnosy: + ncoghlan
messages: + msg278451
2016-10-10 23:44:15abarrysetnosy: + abarry
2016-10-10 19:51:44serhiy.storchakalinkissue28214 dependencies
2016-10-10 19:50:27serhiy.storchakacreate