Skip to content

gh-162340: Color exceptions from while REPL awaits input - #162343

Open
johnslavik wants to merge 3 commits into
python:mainfrom
johnslavik:gh-162340-colorize-repl-keyboardinterrupt
Open

johnslavik wants to merge 3 commits into
python:mainfrom
johnslavik:gh-162340-colorize-repl-keyboardinterrupt

Conversation

@johnslavik

@johnslavik johnslavik commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Before:

image

After:

image

Some notes:

  • The expectation is that ._excepthook() will use .write() at all times
  • ._excepthook() fitted really well here because it addresses all coloring concerns and doesn't touch sys.last_* like ._showtraceback()
  • I've included test_keyboard_interrupt_preserves_last_exception as an invariant test, inspired by a catch in gh-122622: Add PyREPL simple_interact.statement_submitted_hook  #157241.
  • I don't think it's worth it to cover MemoryError here
  • This would look bad if exceptions were chained (because __cause__ and __context__ are still honored), but that is not expected to happen in practice
  • Works in the asyncio REPL 🔥

I've included `test_keyboard_interrupt_preserves_last_exception` as an
invariant test, inspired by a catch in pythonGH-157241.
@johnslavik johnslavik added the topic-repl Related to the interactive shell label Nov 21, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 21, 2026
Comment thread Lib/_pyrepl/simple_interact.py Outdated
console.resetbuffer()
except MemoryError:
console.write("\nMemoryError\n")
except MemoryError as exc:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we keep a fallback here? If the traceback formatter runs out of memory, its MemoryError escapes the input loop.

@johnslavik johnslavik Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. FYI: I knew about this, but wasn't sure about the decision to make. According to some numbers my clankers made, the chance of traceback formatter running out of memory again was rare (but it was a regression indeed). But in retrospective I agree the trivial write is better to avoid regressing over fancy coloring.

@bedevere-app

bedevere-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@johnslavik

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@pablogsal: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from pablogsal November 28, 2026 09:29
@johnslavik
johnslavik force-pushed the gh-162340-colorize-repl-keyboardinterrupt branch from a2fe9f4 to 042a239 Compare November 28, 2026 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review topic-repl Related to the interactive shell type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants