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-112730: Use color to highlight error locations by pablogsal · Pull Request #112732 · python/cpython

Conversation

@pablogsal

@pablogsal pablogsal commented Dec 4, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
  • Issue: Use color to highlight error locations #112730

Sorry, something went wrong.

Uh oh!

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

hugovk, ikocs, and barneygale reacted with hooray emoji xggrnx reacted with rocket emoji
All reactions
  • 🎉 3 reactions
  • 🚀 1 reaction
@pablogsal
pablogsal requested a review from iritkatriel as a code owner December 4, 2023 23:08
@bedevere-app bedevere-app Bot added the awaiting core review label Dec 4, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Dec 4, 2023
Use color to highlight error locations #112730
Closed
@pablogsal
pablogsal force-pushed the gh-112730 branch from b0d9a03 to bc6e729 Compare December 4, 2023 23:09
@pablogsal
pablogsal requested a review from ambv December 4, 2023 23:09
@pablogsal
pablogsal force-pushed the gh-112730 branch 5 times, most recently from 182fae6 to 9c7387e Compare December 4, 2023 23:31
iritkatriel
iritkatriel reviewed Dec 4, 2023
View reviewed changes
Comment thread Doc/using/cmdline.rst Resolved

Uh oh!

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

@pablogsal
pythongh-112730: Use color to highlight error locations
9bc06ed
Signed-off-by: Pablo Galindo <[email protected]>
@pablogsal
pablogsal force-pushed the gh-112730 branch from 9c7387e to 9bc06ed Compare December 5, 2023 00:30
@terryjreedy

terryjreedy commented Dec 5, 2023

Copy link
Copy Markdown
Member

On Windows in both CommandPrompt and PowerShell, 1/0 becomes ←[31m1←[0m←[1;31m/←[0m←[31m0←[0m in the traceback. They need to be switched to interpret the ANSI codes (possible, I believe) or treated as 'dumb', at least for this purpose.

pablogsal reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@pablogsal

pablogsal commented Dec 5, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

On Windows in both CommandPrompt and PowerShell, 1/0 becomes ←[31m1←[0m←[1;31m/←[0m←[31m0←[0m in the traceback. They need to be switched to interpret the ANSI codes (possible, I believe) or treated as 'dumb', at least for this purpose.

Will investigate how to properly set this up for windows. I know the more modern Windows terminal (https://apps.microsoft.com/detail/9N0DX20HK701?hl=en-gb&gl=US) handles this correctly. We may need to deactivate this unless that new terminal is detected

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal

pablogsal commented Dec 5, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

I think for the start I will look at WT_SESSION as discussed here: microsoft/terminal#1040. More complicated solutions are possible (rich does this here but I don't want to start using ctypes and complications when printing exceptions, so i want to keep this dumb as possible).

We can iterate afterwards if someone want to add support to detecting other (working) environments.

Or maybe we can expose some C calls to windows and check for ENABLE_VIRTUAL_TERMINAL_PROCESSING....

All reactions

Sorry, something went wrong.

Uh oh!

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

@terryjreedy

terryjreedy commented Dec 5, 2023

Copy link
Copy Markdown
Member

According to https://en.wikipedia.org/wiki/Windows_Terminal, the first stable release of Windows Terminal was 18 days ago. It can run Command Prompt, Powershell, WSL Bash, and more. It is becoming default on Win 11 and can be installed on up-to-date Win 10. I will see if either an option 'cumulative update preview' or the next monthly update (in a week) installs it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@aroberge

aroberge commented Dec 5, 2023

Copy link
Copy Markdown
Contributor

Excellent idea. Based on my experience in trying to support this type of feature in various terminal themes for friendly-traceback, I think it would be helpful if the colors in

class _ANSIColors:
    RED = '\x1b[31m'
    BOLD_RED = '\x1b[1;31m'

could also be user-configurable via environment variables or some other method.

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal

pablogsal commented Dec 5, 2023

Copy link
Copy Markdown
Member Author

Excellent idea. Based on my experience in trying to support this type of feature in various terminal themes for friendly-traceback, I think it would be helpful if the colors in

class _ANSIColors:
    RED = '\x1b[31m'
    BOLD_RED = '\x1b[1;31m'

could also be user-configurable via environment variables or some other method.

Thanks a lot for the suggestion. I will consider it, but for the first version, I am going to center on getting the defaults right for most environments and then we can iterate to improve the situation in different systems.

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal
Make it work on windows
7671e2d
@pablogsal

pablogsal commented Dec 5, 2023

Copy link
Copy Markdown
Member Author

@terryjreedy I have exposed a call to GetConsoleMode in the nt module and I am using that to check if the given terminal supports colors. Can you try to give it a go. Here are my local results:

powershell

powershell

windows terminal

windows_terminal

All reactions

Sorry, something went wrong.

Uh oh!

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

hugovk
hugovk reviewed Dec 5, 2023
View reviewed changes
Comment thread Lib/traceback.py Resolved

Uh oh!

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

@pablogsal
Reorder env variable checks
ff19eec
@pablogsal
pablogsal force-pushed the gh-112730 branch from 0b52aad to 2201d8f Compare December 5, 2023 12:45
@pablogsal
Add more docs
9ae4c77
Signed-off-by: Pablo Galindo <[email protected]>
@pablogsal
pablogsal force-pushed the gh-112730 branch 2 times, most recently from 58ce57a to 9ae4c77 Compare December 5, 2023 12:47
@hugovk

hugovk commented Dec 5, 2023

Copy link
Copy Markdown
Member

Some Python programs use https://github.com/tartley/colorama for colour, and some others do the colour themselves but still depend on colorama just to call their colorama.init() helper function to initialise Windows:

https://github.com/tartley/colorama#initialisation

You could check what that does, or their newer colorama.just_fix_windows_console(): https://github.com/tartley/colorama/blob/136808718af8b9583cb2eed1756ed6972eda4975/colorama/initialise.py#L72

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal

pablogsal commented Dec 5, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Some Python programs use https://github.com/tartley/colorama for colour, and some others do the colour themselves but still depend on colorama just to call their colorama.init() helper function to initialise Windows:

That seems to wrap stderr and stdout to translate between ANSI and whatever windows uses. Given that this is critical code (we are printing errors) I don't want to mess with the standard streams not call complicated code.

Also, it is not clear to me what will happen if the interpreter does this wrapping and then something else does it on top (notice the only reason they claim is safe to call multiple times is because they record global state). As mentioned before, I want to leave this as dumb as possible and the current solution that detects if is possible to use ANSI sequences seems to work nicely to detect when is safe to do.

Edit:

Also seems that the future for Windows is the "Windows terminal" that supports this out of the box.

hugovk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@pablogsal
Add more colors
e4a0717
Signed-off-by: Pablo Galindo <[email protected]>
@pablogsal
Add test for syntax error (and fix colorization)
1838f1c
@pablogsal
pablogsal force-pushed the gh-112730 branch from 949da49 to 1838f1c Compare December 5, 2023 15:11
@pablogsal pablogsal mentioned this pull request Dec 6, 2023
Support multi-line error locations in traceback and other related improvements (PEP-657, 3.11) #106922
Closed
@pablogsal
Fix multiline output
a84f929
@terryjreedy

terryjreedy commented Dec 6, 2023

Copy link
Copy Markdown
Member

With fresh update, after "Fix multiline output", tracebacks look normal (b/w, not ansi) in Python started in CommandPrompt and in Windows Console (when started from icon or Explorer).

pablogsal reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

barneygale
barneygale reviewed Dec 6, 2023
View reviewed changes
Comment thread Doc/using/cmdline.rst Resolved

Uh oh!

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

ambv added 3 commits December 6, 2023 22:15
@ambv
Merge branch 'main' into pythongh-112730
158c09f
@ambv
s/PY_COLORS/PYTHON_COLORS/
8a07163
@ambv
Make PYTHON_COLORS higher precedence than NO_COLOR and FORCE_COLOR
f0aea49
@barneygale

barneygale commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

Python-specific environment variables usually don't include underscores in their names (e.g. we have PYTHONWARNINGS, not PYTHON_WARNINGS). Should that apply here too, so the environment variable might be PYTHONCOLORS rather than PYTHON_COLORS?

All reactions

Sorry, something went wrong.

Uh oh!

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

@ambv

ambv commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

@barneygale, if you look at the current makeup of PYTHON env variables, you'll notice we are currently changing this naming scheme. New variables have an underscore to improve readability. We leave old ones as is for backwards compatibility.

barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@ambv
ambv merged commit 16448ca into python:main Dec 6, 2023
@bedevere-app bedevere-app Bot removed the awaiting core review label Dec 6, 2023
@barneygale

barneygale commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

Ah ha! Thanks :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal
pablogsal deleted the gh-112730 branch December 6, 2023 22:31
@hugovk hugovk mentioned this pull request Jan 7, 2024
gh-73965: New environment variable PYTHON_HISTORY #13208
Merged
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@pablogsal @ambv @aisk
pythongh-112730: Use color to highlight error locations (pythongh-112732
d5a88ec
)

Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
@hugovk hugovk mentioned this pull request Apr 6, 2024
gh-117225: Add color to doctest output #117583
Merged
methane
methane reviewed Apr 19, 2024
View reviewed changes
Comment thread Lib/traceback.py
return self._str

def format_exception_only(self, *, show_group=False, _depth=0):
def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):

@methane methane Apr 19, 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.

Is there any reason to use **kwargs instead of *, colorlize=False?

Sorry, something went wrong.

Uh oh!

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

All reactions

@maxeonyx maxeonyx Oct 17, 2025

Copy link
Copy Markdown

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.

The colorize parameter currently doesn't appear anywhere in the docs and it would now be a backwards-incompatible change to replace **kwargs with colorize=False

This was a good comment.

Sorry, something went wrong.

Uh oh!

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

All reactions
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@pablogsal @ambv @Glyphack
pythongh-112730: Use color to highlight error locations (pythongh-112732
37e5f1e
)

Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
mccullerlp
mccullerlp reviewed Dec 17, 2025
View reviewed changes
Comment thread Lib/traceback.py
occurred with a caret on the next line indicating the approximate
position of the error.
"""
colorize = kwargs.get("colorize", False)

@mccullerlp mccullerlp Dec 17, 2025

Copy link
Copy Markdown

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.

Should add documentation for this argument. See #142896 142896

Sorry, something went wrong.

Uh oh!

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

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@methane methane methane left review comments
@barneygale barneygale barneygale left review comments
@iritkatriel iritkatriel iritkatriel left review comments
@hugovk hugovk hugovk left review comments
@ambv ambv Awaiting requested review from ambv
+2 more reviewers
@mccullerlp mccullerlp mccullerlp left review comments
@maxeonyx maxeonyx maxeonyx left review comments
Reviewers whose approvals may not affect merge requirements

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.

10 participants

@pablogsal @terryjreedy @aroberge @hugovk @barneygale @ambv @methane @mccullerlp @iritkatriel @maxeonyx
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.