Skip to content

The new REPL outputs different tracebeck when custom sys.excepthook is used #122478

Description

@serhiy-storchaka

Bug report

If sys.systemhook is not sys.__systemhook__, the new REPL outputs the line in the code module in the traceback:

>>> xxx
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    xxx
NameError: name 'xxx' is not defined
>>> import sys; sys.excepthook = lambda *args: sys.__excepthook__(*args)
>>> xxx
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/code.py", line 91, in runcode
    exec(code, self.locals)
    ~~~~^^^^^^^^^^^^^^^^^^^
  File "<python-input-2>", line 1, in <module>
    xxx
NameError: name 'xxx' is not defined

Linked PRs

Activity

  1. serhiy-storchaka commented on Jul 31, 2024

    @serhiy-storchaka
    MemberAuthor

    And for compile-time errors:

    >>> ?
      File "<unknown>", line 1
        ?
        ^
    SyntaxError: invalid syntax
    >>> import sys; sys.excepthook = lambda *args: sys.__excepthook__(*args)
    >>> ?
    Traceback (most recent call last):
      File "/home/serhiy/py/cpython/Lib/_pyrepl/console.py", line 172, in runsource
        tree = ast.parse(source)
      File "/home/serhiy/py/cpython/Lib/ast.py", line 53, in parse
        return compile(source, filename, mode, flags,
                       _feature_version=feature_version, optimize=optimize)
      File "<unknown>", line 1
        ?
        ^
    SyntaxError: invalid syntax
  2. devdanzin commented on Jul 31, 2024

    @devdanzin
    Member

    Seems to be done on purpose in code.InteractiveInterpreter.showsyntaxerror:

    cpython/Lib/code.py

    Lines 126 to 132 in bd3d31f

    if sys.excepthook is sys.__excepthook__:
    lines = traceback.format_exception_only(type, value, colorize=colorize)
    self.write(''.join(lines))
    else:
    # If someone has set sys.excepthook, we let that take precedence
    # over self.write
    self._call_excepthook(type, value, tb)

    I suppose this could be overridden in _pyrepl to always output with .write()?

  3. added a commit that references this issue on Jul 31, 2024
  4. changed the title [-]The new REPL outputs different tracebeck when custom sys.systemhook is used[/-] [+]The new REPL outputs different tracebeck when custom sys.excepthook is used[/+] on Jul 31, 2024
  5. added a commit that references this issue on Jul 31, 2024
  6. serhiy-storchaka commented on Jul 31, 2024

    @serhiy-storchaka
    MemberAuthor

    sys.excepthook is of course called on purpose, but the traceback was not corrected for it. And sys.last_traceback was not always correct even with the default sys.excepthook.

  7. added 2 commits that reference this issue on Aug 7, 2024
  8. added a commit that references this issue on Aug 8, 2024
  9. added a commit that references this issue on Aug 22, 2024
  10. added a commit that references this issue on Aug 22, 2024
  11. added 2 commits that reference this issue on Aug 22, 2024
  12. added a commit that references this issue on Aug 23, 2024
  13. skirpichev commented on Jan 21, 2025

    @skirpichev
    Member

    @serhiy-storchaka, can this be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions