8000 gh-122478: Remove internal frames from tracebacks in REPL by serhiy-storchaka · Pull Request #122528 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Conversation

serhiy-storchaka
Copy link
Member
@serhiy-storchaka serhiy-storchaka commented 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.

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.
@devdanzin
Copy link
Contributor

This has a very slight side-effect of not showing error position for a call that raises in codeop:

>>> 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'

@serhiy-storchaka
Copy link
Member Author

@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?

@serhiy-storchaka
Copy link
Member Author

@devdanzin, the difference is because in one example the code is c = codeop.compile_command(1), and in other example it is codeop.compile_command(1). I get the same results with and without this PR. Did I miss something?

@devdanzin
Copy link
Contributor

I get the same results with and without this PR. Did I miss something?

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'):
Copy link
Member

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?

Copy link
Member Author

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`.
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With removed frames.

@cfbolz
Copy link
Contributor
cfbolz commented Aug 1, 2024

@serhiy-storchaka I think the PR is good, I like the fact that the showtraceback and showsyntaxerror paths are unified.

(This is probably unrelated to your PR, but _pyrepl.console.InteractiveColoredConsole could now be simplified and overwrite only _showtraceback. Then the undocumented colorize keyword arguments that showtraceback and showsyntaxerror have gained for pyrepl could be removed again. This would also fix another tiny bug that I'm about to report.)

@serhiy-storchaka serhiy-storchaka merged commit e73e7a7 into python:main Aug 7, 2024
36 checks passed
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@serhiy-storchaka serhiy-storchaka deleted the InteractiveConsole-excepthook-traceback branch August 7, 2024 20:21
@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker e73e7a7abdc3fed252affcb1629df1b3c8fff2ef 3.13

@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker e73e7a7abdc3fed252affcb1629df1b3c8fff2ef 3.12

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes and removed needs backport to 3.13 bugs and security fixes labels Aug 8, 2024
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker e73e7a7abdc3fed252affcb1629df1b3c8fff2ef 3.13

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Aug 8, 2024
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>
@bedevere-app
Copy link
bedevere-app bot commented Aug 8, 2024

GH-122816 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Aug 8, 2024
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes and removed needs backport to 3.13 bugs and security fixes labels Aug 8, 2024
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request 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 <storchaka@gmail.com>
@bedevere-app
Copy link
bedevere-app bot commented Aug 8, 2024

GH-122817 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Aug 8, 2024
blhsing pushed a commit to blhsing/cpython that referenced this pull request 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 pull request 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 <storchaka@gmail.com>
@bedevere-app
Copy link
bedevere-app bot commented Aug 22, 2024

GH-123227 is a backport of this pull request to the 3.13 branch.

pablogsal pushed a commit to cfbolz/cpython that referenced this pull request 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 <storchaka@gmail.com>
pablogsal pushed a commit that referenced this pull request 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 <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this pull request 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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0