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

Discrepancy between traceback.print_exception and sys.__excepthook__ #78644

Closed
TimothyMcCurrach mannequin opened this issue Aug 22, 2018 · 6 comments
Closed

Discrepancy between traceback.print_exception and sys.__excepthook__ #78644

TimothyMcCurrach mannequin opened this issue Aug 22, 2018 · 6 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@TimothyMcCurrach
Copy link
Mannequin

TimothyMcCurrach mannequin commented Aug 22, 2018

BPO 34463
Nosy @taleinat, @ambv, @miss-islington, @iritkatriel
PRs
  • bpo-34463: Match python traceback format to the C traceback for a Syn… #23427
  • [3.9] bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427) #23895
  • [3.8] bpo-34463: Make python tracebacks identical to C tracebacks for #23899
  • 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 2021-04-27.06:57:02.371>
    created_at = <Date 2018-08-22.19:01:06.594>
    labels = ['type-bug', 'library', '3.10']
    title = 'Discrepancy between traceback.print_exception and sys.__excepthook__'
    updated_at = <Date 2021-04-27.06:57:02.371>
    user = 'https://bugs.python.org/TimothyMcCurrach'

    bugs.python.org fields:

    activity = <Date 2021-04-27.06:57:02.371>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-27.06:57:02.371>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2018-08-22.19:01:06.594>
    creator = 'Timothy McCurrach'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34463
    keywords = ['patch']
    message_count = 6.0
    messages = ['323902', '380349', '381488', '383610', '383620', '391979']
    nosy_count = 5.0
    nosy_names = ['taleinat', 'lukasz.langa', 'miss-islington', 'Timothy McCurrach', 'iritkatriel']
    pr_nums = ['23427', '23895', '23899']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34463'
    versions = ['Python 3.10']

    @TimothyMcCurrach
    Copy link
    Mannequin Author

    TimothyMcCurrach mannequin commented Aug 22, 2018

    If you have set sys.excepthook to some function which calls traceback.print_exception, then I would expect to get identical traceback/exception messages. If you run raise SyntaxError("some message"), then print_exception has the extra line File "<string>", line None.

    This comes from lines 558-561 of traceback.py:

    # It was a syntax error; show exactly where the problem was found.
    filename = self.filename or "<string>"
    lineno = str(self.lineno) or '?'
    yield '  File "{}", line {}\n'.format(filename, lineno)

    Is it expected behaviour that these two functions behave differently, or should there be something like:
    if self.filename or self.lineno:
    etc.

    Also, if self.lineno is None, then str(self.lineno) evaluates to "None" and so the ? is never used.

    @TimothyMcCurrach TimothyMcCurrach mannequin added the type-bug An unexpected behavior, bug, or error label Aug 22, 2018
    @iritkatriel
    Copy link
    Member

    Reproduced on 3.10:

    Running Release|Win32 interpreter...
    Python 3.10.0a1+ (heads/exceptionGroup-stage1-dirty:928c211ad8, Oct 28 2020, 14:36:37) [MSC v.1916 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> raise SyntaxError("some message")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    SyntaxError: some message
    
    >>> import traceback
    >>> import sys
    >>> sys.excepthook = traceback.print_exception
    >>> raise SyntaxError("some message")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line None
    SyntaxError: some message

    @iritkatriel iritkatriel added stdlib Python modules in the Lib dir 3.10 only security fixes labels Nov 4, 2020
    @iritkatriel
    Copy link
    Member

    After debugging the default excepthook code (C version), I see that the difference is because the SyntaxError's lineno is None.

    It doesn't mind that the filename is None (it defaults it to <string>) but when it can't get the lineno it gives up here:

    goto finally;

    If you run this script:

    ---------------------------------------

    import traceback
    import sys
    
    se = SyntaxError("wrong!")
    se.filename = "myfile.py"
    print("========== lineno is None ==========")
    print('traceback.print_exception:')
    traceback.print_exception(type(se), se, None)
    print('---------------------')
    print('sys.excepthook:')
    sys.excepthook(type(se), se, None)
    print('---------------------')
    
    se.lineno = 55
    
    print("========== lineno is 55 ==========")
    print('traceback.print_exception:')
    traceback.print_exception(type(se), se, None)
    print('---------------------')
    print('sys.excepthook:')
    sys.excepthook(type(se), se, None)
    print('---------------------')

    The output is:
    ---------------------------------------
    Running Release|x64 interpreter...
    ========== lineno is None ==========
    traceback.print_exception:
    File "myfile.py", line None
    SyntaxError: wrong!
    ---------------------
    sys.excepthook: <-- file-lineno missing
    SyntaxError: wrong! (myfile.py)
    ---------------------
    ========== lineno is 55 ==========
    traceback.print_exception:
    File "myfile.py", line 55
    SyntaxError: wrong!
    ---------------------
    sys.excepthook:
    File "myfile.py", line 55 <-- file-lineno is there
    SyntaxError: wrong!
    ---------------------
    ---------------------------------------

    @taleinat
    Copy link
    Contributor

    New changeset 069560b by Irit Katriel in branch 'master':
    bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)
    069560b

    @miss-islington
    Copy link
    Contributor

    New changeset 8e5c61a by Miss Islington (bot) in branch '3.9':
    bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)
    8e5c61a

    @ambv
    Copy link
    Contributor

    ambv commented Apr 26, 2021

    New changeset 727bed6 by Irit Katriel in branch '3.8':
    [3.8] bpo-34463: Make python tracebacks identical to C tracebacks for (bpo-23899)
    727bed6

    @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.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants