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 sys.exception() #90486

Closed
iritkatriel opened this issue Jan 10, 2022 · 9 comments
Closed

add sys.exception() #90486

iritkatriel opened this issue Jan 10, 2022 · 9 comments
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@iritkatriel
Copy link
Member

BPO 46328
Nosy @warsaw, @ericsnowcurrently, @sweeneyde, @iritkatriel, @AlexWaygood
PRs
  • bpo-46328: added sys.exception() #30514
  • 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/iritkatriel'
    closed_at = <Date 2022-01-13.12:36:20.282>
    created_at = <Date 2022-01-10.11:50:17.000>
    labels = ['type-feature', 'library', '3.11']
    title = 'add sys.exception()'
    updated_at = <Date 2022-01-13.12:36:20.282>
    user = 'https://github.com/iritkatriel'

    bugs.python.org fields:

    activity = <Date 2022-01-13.12:36:20.282>
    actor = 'iritkatriel'
    assignee = 'iritkatriel'
    closed = True
    closed_date = <Date 2022-01-13.12:36:20.282>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2022-01-10.11:50:17.000>
    creator = 'iritkatriel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46328
    keywords = ['patch']
    message_count = 9.0
    messages = ['410204', '410216', '410222', '410224', '410225', '410255', '410258', '410260', '410489']
    nosy_count = 5.0
    nosy_names = ['barry', 'eric.snow', 'Dennis Sweeney', 'iritkatriel', 'AlexWaygood']
    pr_nums = ['30514']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46328'
    versions = ['Python 3.11']

    @iritkatriel
    Copy link
    Member Author

    Following to changes in bpo-45711, the interpreter's internal representation of the active exception is just the exception instance (the exc_type and exc_traceback fields were removed).

    For backwards compatibility, sys.exc_info() constructs the (typ, val, tb) tuple from the instance and this will continue to be the case for some time because this tuple has leaked into quite a few APIs.

    However, now that the redundancy in the exc_info tuple is guaranteed by the way it's constructed, we can confidently add a sys.exception() method that returns just the exception instance (as suggested in PEP-3134's section on future improvements).

    This small change will make a difference to learners because the (typ, val, tb) tuple looks quite cryptic to those who don't know about tracebacks, and the redundancy in it is confusing for those who do.

    @iritkatriel iritkatriel added the 3.11 only security fixes label Jan 10, 2022
    @iritkatriel iritkatriel self-assigned this Jan 10, 2022
    @iritkatriel iritkatriel added type-feature A feature request or enhancement 3.11 only security fixes labels Jan 10, 2022
    @iritkatriel iritkatriel self-assigned this Jan 10, 2022
    @iritkatriel iritkatriel added the type-feature A feature request or enhancement label Jan 10, 2022
    @iritkatriel
    Copy link
    Member Author

    This is part of a larger plan to reduce the footprint of the exc_info triplet on the language. See https://gist.github.com/iritkatriel/3927147548b10a7929cb0b680e3adc52
    History

    @iritkatriel iritkatriel added stdlib Python modules in the Lib dir labels Jan 10, 2022
    @sweeneyde
    Copy link
    Member

    Would there be any value in spelling this as sys.active_exception() or sys.current_exception() or sys.get_exception() or sys.exception_in_flight() or similar?

    My only worry is confusion between sys.exception() versus builtins.Exception.

    @ericsnowcurrently
    Copy link
    Member

    So sys.exception() will be equivalent to sys.exc_info()[1] (or rather, sys.exc_info() will be (type(sys.exception()), sys.exception(), sys.exception().__traceback__))?

    That seems good to me.

    @ericsnowcurrently
    Copy link
    Member

    FWIW, here's an alternative we should not pursue: return just the exception from sys.exc_info(). For compatibility, we would implement __iter__ and __getitem__ on BaseException, to duplicate the behavior of the current tuple. There are a number of good reasons why this is not what we should do.

    @iritkatriel
    Copy link
    Member Author

    I thought of something like sys.active_exception() but it seems like a lot to type. sys.exception() was suggested in PEP-3134.

    Does this change need a pep?

    @warsaw
    Copy link
    Member

    warsaw commented Jan 10, 2022

    sys.exception() seems like a decent enough trade-off. I've always disliked the abbreviations in "exc_info". It doesn't feel big enough for a PEP to me.

    @iritkatriel
    Copy link
    Member Author

    Cool. I just removed the do-not-merge label from the PR and I guess it's ready to be reviewed.

    @iritkatriel
    Copy link
    Member Author

    New changeset c590b58 by Irit Katriel in branch 'main':
    bpo-46328: Add sys.exception() (GH-30514)
    c590b58

    @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.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants