Skip to content

Expose _PyErr_GetTopmostException #84725

Closed

Description

@jd
mannequin
BPO 40545
Nosy @scoder, @vstinner, @jd, @markshannon
PRs
  • bpo-40545: Expose _PyErr_GetTopmostException in the public C API #19977
  • bpo-40545: Export _PyErr_GetTopmostException() function #19978
  • 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 = None
    closed_at = <Date 2022-01-06.16:49:34.997>
    created_at = <Date 2020-05-07.09:29:58.433>
    labels = ['expert-C-API', 'type-feature', '3.9']
    title = 'Expose _PyErr_GetTopmostException'
    updated_at = <Date 2022-01-06.16:49:34.996>
    user = 'https://github.com/jd'

    bugs.python.org fields:

    activity = <Date 2022-01-06.16:49:34.996>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-06.16:49:34.997>
    closer = 'iritkatriel'
    components = ['C API']
    creation = <Date 2020-05-07.09:29:58.433>
    creator = 'jd'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40545
    keywords = ['patch']
    message_count = 10.0
    messages = ['368327', '368328', '368332', '368338', '368339', '368340', '368341', '368343', '368345', '368348']
    nosy_count = 4.0
    nosy_names = ['scoder', 'vstinner', 'jd', 'Mark.Shannon']
    pr_nums = ['19977', '19978']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40545'
    versions = ['Python 3.9']

    Activity

    1. jd commented on May 7, 2020

      jdmannequin
      MannequinAuthor

      The function _PyErr_GetTopmostException() is not exported and is useful to get access to exceptions info from a PyThreadState.

    2. vstinner commented on May 7, 2020

      @vstinner
      Member

      The function was added by:

      commit ae3087c
      Author: Mark Shannon <mark@hotpy.org>
      Date: Sun Oct 22 22:41:51 2017 +0100

      Move exc state to generator. Fixes bpo-25612 (bpo-1773)
      
      Move exception state information from frame objects to coroutine (generator/thread) object where it belongs.
      

      Python 3.9 is now built with -fvisibility=hidden, bpo-11410:

      commit 0b60f64
      Author: Vinay Sajip <vinay_sajip@yahoo.co.uk>
      Date: Tue Oct 15 08:26:12 2019 +0100

      bpo-11410: Standardize and use symbol visibility attributes across POSIX and Windows. (GH-16347)
      

      It means that functions which are not explicitly exported are no longer exported.

      So yeah, _PyErr_GetTopmostException() symbol is not properly exported.

      But the function remains private. IMHO if someone wants to add a *public* function, it should return a *strong* reference to the 3 variables (exc type, exc value, exc tb).

      See also bpo-39947: "[C API] Make the PyThreadState structure opaque (move it to the internal C API)". I added multiple public getters for PyThreadState structure in Python 3.9.

    3. jd commented on May 7, 2020

      jdmannequin
      MannequinAuthor

      I would definitely love to have a public API for this.

      Would PyErr_GetTopmostException be a good candidate for this?

      (I can open a different bug report if needed)

    4. vstinner commented on May 7, 2020

      @vstinner
      Member

      New changeset 8963a7f by Victor Stinner in branch 'master':
      bpo-40545: Export _PyErr_GetTopmostException() function (GH-19978)
      8963a7f

    5. vstinner commented on May 7, 2020

      @vstinner
      Member

      I would definitely love to have a public API for this.

      I'm not sure if it's a good idea. It's kind of semi-internal.

      Stephan Behnel and Mark Shannon: What do you think of exposing PyThreadState exception stack (exc_state)? Especially the "top" exception. If I recall correctly, Mark designed it and Stephan "reimplemented" it in Cython.

      I also recall a minor issue in greenlet:
      python-greenlet/greenlet#132

    6. markshannon commented on May 7, 2020

      @markshannon
      Member

      What's wrong with PyErr_GetExcInfo() for accessing the current exception?

      Victor, would you revert #19978 until we decide what to do. Thanks.

    7. jd commented on May 7, 2020

      jdmannequin
      MannequinAuthor

      PyErr_GetExcInfo() does not allow to specify a PyThreadState as argument.

    8. markshannon commented on May 7, 2020

      @markshannon
      Member

      Why do you need to specify a PyThreadState?

    9. vstinner commented on May 7, 2020

      @vstinner
      Member

      Why do you need to specify a PyThreadState?

      From past issues, I understood that Julien's use case is a profiler which inspects memory without calling functions nor switching to other threads. It's like "dump the state of all Python threads".

      I'm fine with exposing _PyErr_GetTopmostException() as a *private* function. Moreover, the function was already exposed in 3.8. It was not exposed because of the visibility change which was made in 3.9.

    10. jd commented on May 7, 2020

      jdmannequin
      MannequinAuthor

      I confirm what Victor wrote. :)

    11. 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

    Assignees

    No one assigned

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions