-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-122478: Remove internal frames from tracebacks in REPL #122528
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
gh-122478: Remove internal frames from tracebacks in REPL #122528
Conversation
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.
8382122
to
edfee7f
Compare
This has a very slight side-effect of not showing error position for a call that raises in >>> c = codeop.compile_command(1)
Traceback (most recent call last):
File "<python-input-11>", line 1, in <module>
c = codeop.compile_command(1)
File "~\PycharmProjects\cpython\Lib\codeop.py", line 102, in compile_command
return _maybe_compile(_compile, source, filename, symbol)
File "~\PycharmProjects\cpython\Lib\codeop.py", line 51, in _maybe_compile
for line in source.split("\n"):
^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'split' Versus: >>> codeop.compile_command(1)
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
codeop.compile_command(1)
~~~~~~~~~~~~~~~~~~~~~~^^^
File "~\PycharmProjects\cpython\Lib\codeop.py", line 102, in compile_command
return _maybe_compile(_compile, source, filename, symbol)
File "~\PycharmProjects\cpython\Lib\codeop.py", line 51, in _maybe_compile
for line in source.split("\n"):
^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'split' |
@cfbolz do you mind to make a review of this PR? @iritkatriel, you already did similar work with cleaning up tracebacks. Are you interesting in this issue? |
@devdanzin, the difference is because in one example the code is |
Nope, user error on my part. Sorry about the noise. |
Lib/code.py
Outdated
self._call_exce 8000 pthook(ei[0], ei[1], last_tb) | ||
typ, value, tb = sys.exc_info() | ||
# Remove internal frames (in modules code and codeop) from traceback. | ||
while tb and tb.tb_frame.f_globals.get('__name__') in ('code', 'codeop'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unittest's _clean_tracebacks()
also looks at the chained exceptions.
Maybe it makes sense to implement this in the traceback module, and reuse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, even this loop is an overpower on one side and not enough on other side. I initially added it to handle both compile-time (this is why copeop
) and run-time errors. But then I have found that compile-time errors should not have a traceback at all.
Now I see that we should just remove one level from a traceback -- the one that refers to the code calls exec()
. This is also necessary for IDLE which overrides runcode()
-- in this case the top frame is in the idlelib.pyshell
module.
This will not work if the code calls exec()
not directly but via wrapper. Ideally we should find the frame that calls exec()
(or eval()
) and drop it and above frames, but there is no easy way to do this. It would help if exec()
and eval()
add a special marker in the traceback, but this would be a new feature.
@@ -0,0 +1,3 @@ | |||
Remove internal frames from tracebacks shown in | |||
:class:`code.InteractiveInterpreter` with non-default :func:`sys.excepthook`. | |||
Save correct tracebacks in :attr:`sys.last_traceback` and update ``__traceback__`` attribute of :attr:`sys.last_value` and :attr:`sys.last_exc`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by 'the correct' traceback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With removed frames.
@serhiy-storchaka I think the PR is good, I like the fact that the (This is probably unrelated to your PR, but |
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
Sorry, @serhiy-storchaka, I could not cleanly backport this to
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to
|
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Sorry, @serhiy-storchaka, I could not cleanly backport this to
|
pythonGH-122528) Frames of methods in code and codeop modules was show with non-default s 8000 ys.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 <storchaka@gmail.com>
GH-122816 is a backport of this pull request to the 3.12 branch. |
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…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 <storchaka@gmail.com>
GH-122817 is a backport of this pull request to the 3.13 branch. |
…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.
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 <storchaka@gmail.com>
GH-123227 is a backport of this pull request to the 3.13 branch. |
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 <storchaka@gmail.com>
…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 <storchaka@gmail.com>
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.