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

Expose _PyErr_GetTopmostException #84725

Closed
jd mannequin opened this issue May 7, 2020 · 10 comments
Closed

Expose _PyErr_GetTopmostException #84725

jd mannequin opened this issue May 7, 2020 · 10 comments
Labels
3.9 only security fixes topic-C-API type-feature A feature request or enhancement

Comments

@jd
Copy link
Mannequin

jd mannequin commented May 7, 2020

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']

    @jd
    Copy link
    Mannequin Author

    jd mannequin commented May 7, 2020

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

    @jd jd mannequin added 3.9 only security fixes topic-C-API type-feature A feature request or enhancement labels May 7, 2020
    @vstinner
    Copy link
    Member

    vstinner commented May 7, 2020

    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.

    @jd
    Copy link
    Mannequin Author

    jd mannequin commented May 7, 2020

    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)

    @vstinner
    Copy link
    Member

    vstinner commented May 7, 2020

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

    @vstinner
    Copy link
    Member

    vstinner commented May 7, 2020

    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

    @markshannon
    Copy link
    Member

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

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

    @jd
    Copy link
    Mannequin Author

    jd mannequin commented May 7, 2020

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

    @markshannon
    Copy link
    Member

    Why do you need to specify a PyThreadState?

    @vstinner
    Copy link
    Member

    vstinner commented May 7, 2020

    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.

    @jd
    Copy link
    Mannequin Author

    jd mannequin commented May 7, 2020

    I confirm what Victor wrote. :)

    @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.9 only security fixes topic-C-API type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants