Skip to content

gh-131507: Add support for syntax highlighting in PyREPL #133247

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

Merged
merged 23 commits into from
May 2, 2025

Conversation

ambv
Copy link
Contributor

@ambv ambv commented May 1, 2025

This is a much improved version of gh-131562. It uses the tokenizer for better speed and pattern matching for more robust handling of soft keywords. While it still won't hit all cases correctly, it's better than idlelib's regular expression-based colorizer and unlike our glorious PEG parser it supports incomplete input, which is crucial for an interactive shell.

Relatedly, pasting support was tweaked to be way faster. Now the entire contents of Frankenstein can be pasted within 3 seconds both on Unix and Windows as long as bracketed pasting is supported by the terminal. This is a necessary tweak for syntax highlighting not to cripple performance of pastes above 5kB.

There is experimental support for theming through _colorize.set_theme() that's mentioned in "What's New" but otherwise undocumented so far.


📚 Documentation preview 📚: https://cpython-previews--133247.org.readthedocs.build/

@ambv ambv added 🔨 test-with-buildbots Test PR w/ buildbots; report in status section topic-repl Related to the interactive shell labels May 1, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @ambv for commit ffebbbe 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F133247%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 1, 2025
Copy link
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

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

Sorry for being too nitpicky, but I still find the blue used for keywords to be too dark when coupled with dark-themed terminals (e.g. #131562 (comment)). Do you think we could use a more contrasting color?

@ambv
Copy link
Contributor Author

ambv commented May 1, 2025

I disagree about the blue color, the fact that Ubuntu colors it too dim is not actionable for the Python project. Adjust your terminal.

@ambv
Copy link
Contributor Author

ambv commented May 1, 2025

The refleak failures are unrelated, see #133258.

@chris-eibl
Copy link
Member

Having a closer look, I see you are only reading in chunks in case of getpending, so my PR might have added value. Let's merge this first and then I can rebase my PR if it is worthwile to do chunked reading in the regular case, too?

@ambv
Copy link
Contributor Author

ambv commented May 2, 2025

@chris-eibl I simplified the Unix case to remove the additional buffering since it complicates the codebase. Your PR was adding that same buffering to Windows. Given that we now achieve the same performance without double-buffering, I'd say we don't need the other PR.

@skirpichev
Copy link
Member

Sorry for being too nitpicky, but I still find the blue used for keywords to be too dark when coupled with dark-themed terminals

I think that the default theme polishing can be addressed in following pr(s?), as per pr description:

There is experimental support for theming through _colorize.set_theme() that's mentioned in "What's New" but otherwise undocumented so far.

@ambv
Copy link
Contributor Author

ambv commented May 2, 2025

So this passed all buildbots save for s390x. The s390x failure is unrelated, Eric is dealing with it: #133265.

ambv and others added 3 commits May 2, 2025 14:48
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@chris-eibl
Copy link
Member

@chris-eibl I simplified the Unix case to remove the additional buffering since it complicates the codebase. Your PR was adding that same buffering to Windows.

Sorry for only spotting the windows_console.py change - I must be biased :)

Yeah, simple is always better 👍

Given that we now achieve the same performance without double-buffering, I'd say we don't need the other PR.

I can confirm: this PR is now way faster pasting in the virtual terminal mode on Windows 🚀

done = "\x1b[201~"
data = ""
import time
start = time.time()
Copy link
Member

Choose a reason for hiding this comment

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

Leftover from testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The trace below shows time. I can move the import up.

Copy link
Member

Choose a reason for hiding this comment

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

Then use perf_counter please

Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

I left a bunch of questions and nitpicks but overall this looks fantastic.

I've reviewed the tokenizer-related parts of the implementation and I'm comfortable with the current approach. We have already discussed this offline but for everyone else reading: while the soft keyword detection uses heuristics, this is a reasonable compromise as bringing in a more correct solution would require a full parser run, which would be much heavier and slower for this use case and we don't even have the technology now to do partial input in the PEG parser so whatever we do will be in python and much slower.

I've also run some performance benchmarks and things look really good — the impact on responsiveness is minimal, even with syntax highlighting enabled by default. And on Windows seem to be super fast.

I'm happy to explore further optimizations in the future, such as avoiding repeated tokenization during screen refreshes. But as it stands, this looks solid. Great work!

@pablogsal
Copy link
Member

pablogsal commented May 2, 2025

The only open question is what to do for setting the theme officially (right now is a "experimental" API) but I think since that affects other parts of the interpreter (such as tracebacks) this is out of scope of this particular PR, so I propose to discuss this separately

@chris-eibl
Copy link
Member

In a legacy Windows console, the prompt is no longer colored, but e.g. a SyntaxError still is.

Also, syntax highlighting is turned off (this maybe is wanted behaviour?)

image

@chris-eibl
Copy link
Member

can_colorize is True, but _colorize.theme has empty strings for all values.

@ambv
Copy link
Contributor Author

ambv commented May 2, 2025

Good catch, @chris-eibl. I'll fix it forward in a subsequent PR.

@ambv ambv merged commit fac41f5 into python:main May 2, 2025
48 checks passed
@chris-eibl
Copy link
Member

Calling _colorize.set_theme() right after virtual terminal processing is enabled via

SetConsoleMode(
OutHandle,
ENABLE_WRAP_AT_EOL_OUTPUT
| ENABLE_PROCESSED_OUTPUT
| ENABLE_VIRTUAL_TERMINAL_PROCESSING,
)

does fix it for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-repl Related to the interactive shell
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants