Skip to content

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

Closed

Description

@serhiy-storchaka
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']

    Activity

    1. serhiy-storchaka commented on Oct 10, 2016

      @serhiy-storchaka
      MemberAuthor

      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.

    2. ncoghlan commented on Oct 11, 2016

      @ncoghlan
      Contributor

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

    3. serhiy-storchaka commented on Oct 11, 2016

      @serhiy-storchaka
      MemberAuthor

      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.

    4. serhiy-storchaka commented on Oct 18, 2016

      @serhiy-storchaka
      MemberAuthor

      Fixed error message in tests.

    5. ncoghlan commented on Oct 18, 2016

      @ncoghlan
      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.

    6. matrixise commented on Oct 18, 2016

      @matrixise
      Member

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

    7. Vgr255 commented on Oct 18, 2016

      Vgr255mannequin
      Mannequin

      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)

    8. serhiy-storchaka commented on Oct 18, 2016

      @serhiy-storchaka
      MemberAuthor

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

    9. ncoghlan commented on Oct 19, 2016

      @ncoghlan
      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).

    10. python-dev commented on Oct 21, 2016

      python-devmannequin
      Mannequin

      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

    11. python-dev commented on Oct 21, 2016

      python-devmannequin
      Mannequin

      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

    12. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions