Skip to content

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

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

johnslavik wants to merge 2 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting 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.

1 participant