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

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

Open
serhiy-storchaka opened this issue Jul 30, 2024 · 3 comments
Labels
topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Jul 30, 2024

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

@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error topic-repl Related to the interactive shell labels Jul 30, 2024
@serhiy-storchaka
Copy link
Member Author

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

@devdanzin
Copy link
Contributor

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()?

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jul 31, 2024
Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
@serhiy-storchaka serhiy-storchaka 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 Jul 31, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jul 31, 2024
Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
@serhiy-storchaka
Copy link
Member Author

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.

serhiy-storchaka added a commit that referenced this issue Aug 7, 2024
Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 8, 2024
pythonGH-122528)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
(cherry picked from commit e73e7a7)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 8, 2024
…onGH-122528)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
(cherry picked from commit e73e7a7)

Co-authored-by: Serhiy Storchaka <[email protected]>
blhsing pushed a commit to blhsing/cpython that referenced this issue Aug 22, 2024
…onGH-122528)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
cfbolz pushed a commit to cfbolz/cpython that referenced this issue Aug 22, 2024
pythonGH-122528)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
(cherry picked from commit e73e7a7)

Co-authored-by: Serhiy Storchaka <[email protected]>
pablogsal pushed a commit to cfbolz/cpython that referenced this issue Aug 22, 2024
pythonGH-122528)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
(cherry picked from commit e73e7a7)

Co-authored-by: Serhiy Storchaka <[email protected]>
pablogsal pushed a commit that referenced this issue Aug 22, 2024
…122528) (#123227)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
(cherry picked from commit e73e7a7)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Aug 23, 2024
…122528) (GH-122816)

Frames of methods in code and codeop modules was show with non-default
sys.excepthook.

Save correct tracebacks in sys.last_traceback and update __traceback__
attribute of sys.last_value and sys.last_exc.
(cherry picked from commit e73e7a7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants