GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-122478: Remove internal frames from tracebacks in REPL by serhiy-storchaka · Pull Request #122528 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 31, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka serhiy-storchaka added needs backport to 3.12 only security fixes needs backport to 3.13 only security fixes labels Jul 31, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Jul 31, 2024
The new REPL outputs different tracebeck when custom sys.excepthook is used #122478
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Jul 31, 2024
@serhiy-storchaka
pythongh-122478: Remove internal frames from tracebacks in REPL
edfee7f
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 force-pushed the InteractiveConsole-excepthook-traceback branch from 8382122 to edfee7f Compare July 31, 2024 18:13
@serhiy-storchaka serhiy-storchaka mentioned this pull request Jul 31, 2024
gh-82378: fix sys.tracebacklimit in pyrepl #122452
Closed
@serhiy-storchaka
serhiy-storchaka requested a review from cfbolz July 31, 2024 18:22
@devdanzin

devdanzin commented Jul 31, 2024

Copy link
Copy Markdown
Member

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jul 31, 2024

Copy link
Copy Markdown
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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jul 31, 2024

Copy link
Copy Markdown
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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@devdanzin

devdanzin commented Jul 31, 2024

Copy link
Copy Markdown
Member

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

Nope, user error on my part. Sorry about the noise.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Aug 1, 2024
View reviewed changes
Comment thread Lib/code.py Outdated
self._call_excepthook(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'):

@iritkatriel iritkatriel Aug 1, 2024

Copy link
Copy Markdown
Member

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Aug 1, 2024

Copy link
Copy Markdown
Member Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
iritkatriel
iritkatriel reviewed Aug 1, 2024
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2024-07-31-20-43-21.gh-issue-122478.sCU2Le.rst
@@ -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`.

@iritkatriel iritkatriel Aug 1, 2024

Copy link
Copy Markdown
Member

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Aug 1, 2024

Copy link
Copy Markdown
Member Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
Simply remove the top frame for run-time errors.
57d0770
@cfbolz

cfbolz commented Aug 1, 2024

Copy link
Copy Markdown
Contributor

@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.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cfbolz
cfbolz approved these changes Aug 1, 2024
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Aug 1, 2024
@cfbolz cfbolz mentioned this pull request Aug 1, 2024
Inconsistency between file names of SyntaxErrors and other Exceptions in the new repl #122546
Closed
@serhiy-storchaka
serhiy-storchaka merged commit e73e7a7 into python:main Aug 7, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Aug 7, 2024
@miss-islington-app

miss-islington-app Bot commented Aug 7, 2024

Copy link
Copy Markdown

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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

miss-islington-app Bot commented Aug 7, 2024

Copy link
Copy Markdown

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned serhiy-storchaka Aug 7, 2024
@miss-islington-app

miss-islington-app Bot commented Aug 7, 2024

Copy link
Copy Markdown

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

miss-islington-app Bot commented Aug 8, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Aug 8, 2024

Copy link
Copy Markdown

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Aug 8, 2024
@serhiy-storchaka
[3.12] pythongh-122478: Remove internal frames from tracebacks in REPL (
12ae7b5
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]>
@bedevere-app

bedevere-app Bot commented Aug 8, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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 only security fixes and removed needs backport to 3.13 only security fixes labels Aug 8, 2024
@miss-islington-app

miss-islington-app Bot commented Aug 8, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 8, 2024
@serhiy-storchaka @miss-islington
pythongh-122478: Remove internal frames from tracebacks in REPL (pyth…
7716e72
…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]>
@bedevere-app

bedevere-app Bot commented Aug 8, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Aug 8, 2024
blhsing pushed a commit to blhsing/cpython that referenced this pull request Aug 22, 2024
@serhiy-storchaka @blhsing
pythongh-122478: Remove internal frames from tracebacks in REPL (pyth…
db762d2
…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
@serhiy-storchaka @cfbolz
[3.13] pythongh-122478: Remove internal frames from tracebacks in REPL (
3ae8b72
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]>
@cfbolz cfbolz mentioned this pull request Aug 22, 2024
[3.13] gh-122478: Remove internal frames from tracebacks in REPL (GH-122528) #123227
Merged
@bedevere-app

bedevere-app Bot commented Aug 22, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pablogsal pushed a commit to cfbolz/cpython that referenced this pull request Aug 22, 2024
@serhiy-storchaka @pablogsal
[3.13] pythongh-122478: Remove internal frames from tracebacks in REPL (
37be1c9
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 pull request Aug 22, 2024
@cfbolz @serhiy-storchaka
[3.13] gh-122478: Remove internal frames from tracebacks in REPL (GH-…
30eee22
…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 pull request Aug 23, 2024
@serhiy-storchaka
[3.12] gh-122478: Remove internal frames from tracebacks in REPL (GH-…
8fe586d
…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)
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel iritkatriel left review comments
@cfbolz cfbolz cfbolz approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@serhiy-storchaka @devdanzin @cfbolz @iritkatriel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.