Skip to content
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

Closed
serhiy-storchaka opened this issue Oct 10, 2016 · 11 comments
Closed

Add convenient C API for "raise ... from ..." #72596

serhiy-storchaka opened this issue Oct 10, 2016 · 11 comments
Assignees
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 28410
Nosy @ncoghlan, @vstinner, @serhiy-storchaka, @1st1, @matrixise, @Vgr255
Files
  • _PyErr_FormatFromCause.patch
  • _PyErr_FormatFromCause-3.5.patch: Patch for 3.5
  • _PyErr_FormatFromCause-2.patch
  • 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 = <Date 2016-10-21.14:18:00.453>
    created_at = <Date 2016-10-10.19:50:27.484>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Add convenient C API for "raise ... from ..."'
    updated_at = <Date 2016-10-21.14:18:00.452>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2016-10-21.14:18:00.452>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-10-21.14:18:00.453>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2016-10-10.19:50:27.484>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['45050', '45128', '45132']
    hgrepos = []
    issue_num = 28410
    keywords = ['patch']
    message_count = 11.0
    messages = ['278441', '278451', '278462', '278873', '278879', '278882', '278893', '278900', '278959', '279125', '279131']
    nosy_count = 7.0
    nosy_names = ['ncoghlan', 'vstinner', 'python-dev', 'serhiy.storchaka', 'yselivanov', 'matrixise', 'abarry']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue28410'
    versions = ['Python 3.6', 'Python 3.7']

    @serhiy-storchaka
    Copy link
    Member Author

    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-storchaka serhiy-storchaka added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Oct 10, 2016
    @ncoghlan
    Copy link
    Contributor

    Serhiy, how does this version compare/relate to the previous discussion in http://bugs.python.org/issue23188 ?

    @serhiy-storchaka
    Copy link
    Member Author

    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
    Copy link
    Member Author

    Fixed error message in tests.

    @ncoghlan
    Copy link
    Contributor

    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
    Copy link
    Member

    I have read and executed the tests and for me the patch is good.

    @Vgr255
    Copy link
    Mannequin

    Vgr255 mannequin commented 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
    Copy link
    Member Author

    I'm not sure about the function name. Does anybody want to suggest better name?

    @ncoghlan
    Copy link
    Contributor

    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
    Copy link
    Mannequin

    python-dev mannequin commented 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
    Copy link
    Mannequin

    python-dev mannequin commented 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

    @serhiy-storchaka serhiy-storchaka self-assigned this Oct 21, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants