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-128595: Default to stdout isatty for colour detection instead of stderr by hugovk · Pull Request #128498 · python/cpython

Conversation

@hugovk

@hugovk hugovk commented Jan 4, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

As suggested at #128317 (comment), default to checking stdout's isatty rather than stderr's.

Also using the @force_not_colorized_test_class decorator from #127877 to run some tests without worrying about colour.

BeforeAfter
image image
image image
image image
image image
  • Issue: Default to stdout isatty for colour detection instead of stderr #128595

Sorry, something went wrong.

Uh oh!

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

zanieb reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
hugovk added 3 commits January 4, 2025 17:46
@hugovk
Add test class helper to force no terminal colour
4ad090d
@hugovk
Refactor
ab76e1b
@hugovk
Default to stdout isatty for colour detection instead of stderr
bb90f89
@bedevere-app bedevere-app Bot added the awaiting core review label Jan 4, 2025
@hugovk hugovk added skip issue skip news and removed skip news skip issue labels Jan 4, 2025
@hugovk hugovk mentioned this pull request Jan 4, 2025
Highlight today in colour in calendar's CLI output #128317
Closed
@hugovk

hugovk commented Jan 4, 2025

Copy link
Copy Markdown
Member Author

Should this be considered a bugfix to backport to 3.13, with its own issue and NEWS, or piggyback on gh-128317 for 3.14 only?

All reactions

Sorry, something went wrong.

Uh oh!

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

@erlend-aasland

erlend-aasland commented Jan 5, 2025

Copy link
Copy Markdown
Contributor

Should this be considered a bugfix to backport to 3.13, with its own issue and NEWS, or piggyback on gh-128317 for 3.14 only?

I would say it deserves an issue and a NEWS entry. I'm fine with adding it to #128317 if it's not going to be backported. I'm leaning into calling it a bugfix.

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.

@hugovk hugovk changed the title Add test class helper to force no terminal colour Default to stdout isatty for colour detection instead of stderr Jan 5, 2025
@hugovk hugovk changed the title Default to stdout isatty for colour detection instead of stderr gh-128595: Default to stdout isatty for colour detection instead of stderr Jan 7, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jan 7, 2025
Default to stdout isatty for colour detection instead of stderr #128595
Closed
@hugovk
Add NEWS
172f8c3
@hugovk hugovk added the needs backport to 3.13 only security fixes label Jan 7, 2025
@hugovk

hugovk commented Jan 7, 2025

Copy link
Copy Markdown
Member Author

Issue, NEWS and backport label applied.

erlend-aasland reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@zanieb

zanieb commented Jan 7, 2025

Copy link
Copy Markdown
Contributor

Agree this sounds like a bug to me :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Jan 8, 2025

Copy link
Copy Markdown
Member

Why not check this for stdout and stderr separately? If the traceback is written to terminal, it should be colorized.

All reactions

Sorry, something went wrong.

Uh oh!

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

@hugovk

hugovk commented Jan 8, 2025

Copy link
Copy Markdown
Member Author

Why not check this for stdout and stderr separately? If the traceback is written to terminal, it should be colorized.

For example, something like this?

-def can_colorize() -> bool:
+def can_colorize(file=sys.stdout) -> bool:
     if not sys.flags.ignore_environment:
         if os.environ.get("PYTHON_COLORS") == "0":
             return False
@@ -49,7 +49,7 @@ def can_colorize() -> bool:
         if os.environ.get("TERM") == "dumb":
             return False
 
-    if not hasattr(sys.stdout, "fileno"):
+    if not hasattr(file, "fileno"):
         return False
 
     if sys.platform == "win32":
@@ -62,6 +62,6 @@ def can_colorize() -> bool:
             return False
 
     try:
-        return os.isatty(sys.stdout.fileno())
+        return os.isatty(file.fileno())
     except io.UnsupportedOperation:
         return sys.stdout.isatty()

And then places like traceback.py can do something like this?

 def _print_exception_bltin(exc, /):
     file = sys.stderr if sys.stderr is not None else sys.__stderr__
-    colorize = _colorize.can_colorize()
+    colorize = _colorize.can_colorize(file=file)
     return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Jan 8, 2025

Copy link
Copy Markdown
Member

Yes, except that dynamic value sys.stdout should not be used as the default value. Use None and then replace it with the current value of sys.stdout inside the function.

zanieb, hugovk, erlend-aasland, and vstinner reacted with thumbs up emoji
All reactions
  • 👍 4 reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Jan 9, 2025

Copy link
Copy Markdown
Member

Can you extract the code to disable colors as a separated PR and merge it first?

All reactions

Sorry, something went wrong.

Uh oh!

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

@hugovk hugovk mentioned this pull request Jan 9, 2025
gh-128595: Add test class helper to force no terminal colour #128687
Merged
@hugovk

hugovk commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

Sure! Please see PR #128687.

All reactions

Sorry, something went wrong.

Uh oh!

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

hugovk added 3 commits January 13, 2025 13:34
@hugovk
Merge branch 'main' into 3.14-color-default-stdout
bf31943
@hugovk
Fix merge conflict
b7855ad
@hugovk
Remove unused import
8e24cc8
@hugovk

hugovk commented Jan 13, 2025

Copy link
Copy Markdown
Member Author

Sure! Please see PR #128687.

Now merged, making this PR much simpler.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Jan 13, 2025

Copy link
Copy Markdown
Member

Please implement what was discussed above.

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.

@hugovk
Check can_colorize using same output stream as traceback
2b88c85
hugovk
hugovk commented Jan 13, 2025
View reviewed changes
Comment thread Lib/test/libregrtest/single.py Resolved

Uh oh!

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

Comment thread Lib/unittest/result.py Resolved

Uh oh!

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

Comment thread Lib/unittest/runner.py Resolved

Uh oh!

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

hugovk and others added 4 commits January 13, 2025 19:00
@hugovk @serhiy-storchaka @vstinner
Apply suggestions from code review
7debe69
Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@hugovk
Add missing parenthesis
6faf53c
@hugovk
Name the argument
e1547d0
@hugovk
Also print skips to stderr
7612bff
vstinner
vstinner reviewed Jan 13, 2025
View reviewed changes
Comment thread Lib/_colorize.py Resolved

Uh oh!

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

vstinner
vstinner reviewed Jan 13, 2025
View reviewed changes
Comment thread Lib/test/libregrtest/single.py Resolved

Uh oh!

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

hugovk and others added 3 commits January 14, 2025 09:19
@hugovk @vstinner
Check file not sys.stdout
44828f0
Co-authored-by: Victor Stinner <[email protected]>
@hugovk
file=stream
36eb18e
@hugovk
Print to stdout
f5857b5
vstinner
vstinner approved these changes Jan 14, 2025
View reviewed changes

@vstinner vstinner left a comment

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.

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
serhiy-storchaka
serhiy-storchaka reviewed Jan 14, 2025
View reviewed changes
Comment thread Lib/test/libregrtest/single.py Resolved

Uh oh!

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

@hugovk
Log skipped to stdout, log failures to stderr
3047d2a
@hugovk

hugovk commented Jan 20, 2025

Copy link
Copy Markdown
Member Author

Thanks again for the reviews!

Next to update #127877.

All reactions

Sorry, something went wrong.

Uh oh!

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

@hugovk
hugovk merged commit 6f167d7 into python:main Jan 20, 2025
@hugovk
hugovk deleted the 3.14-color-default-stdout branch January 20, 2025 10:52
@miss-islington-app

miss-islington-app Bot commented Jan 20, 2025

Copy link
Copy Markdown

Thanks @hugovk 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.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jan 20, 2025
@miss-islington-app

miss-islington-app Bot commented Jan 20, 2025

Copy link
Copy Markdown

Sorry, @hugovk, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 6f167d71347de6717d9f6b64026e21f23d41ef0b 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 hugovk Jan 20, 2025
hugovk added a commit to hugovk/cpython that referenced this pull request Jan 20, 2025
@hugovk @serhiy-storchaka @vstinner
[3.13] pythongh-128595: Default to stdout isatty for colour detection…
55cb946
… instead of stderr (pythonGH-128498)

(cherry picked from commit 6f167d7)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
hugovk added a commit to hugovk/cpython that referenced this pull request Jan 20, 2025
@hugovk @serhiy-storchaka @vstinner
[3.13] pythongh-128595: Default to stdout isatty for colour detection…
0ce7116
… instead of stderr (pythonGH-128498)

(cherry picked from commit 6f167d7)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@bedevere-app

bedevere-app Bot commented Jan 20, 2025

Copy link
Copy Markdown

GH-129057 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 Jan 20, 2025
@picnixz

picnixz commented Jan 20, 2025

Copy link
Copy Markdown
Member

Some build bots are now failing (https://buildbot.python.org/api/v2/logs/12166870/raw_inline):

TypeError: setUpModule..() got an unexpected keyword argument 'file'

hugovk reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@hugovk hugovk mentioned this pull request Jan 20, 2025
gh-128595: Fix test__colorize unexpected keyword argument 'file' on buildbots #129070
Merged
@hugovk

hugovk commented Jan 20, 2025

Copy link
Copy Markdown
Member Author

Please see PR #129070 for a fix.

All reactions

Sorry, something went wrong.

Uh oh!

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

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 21, 2025
@hugovk @serhiy-storchaka
@vstinner @srinivasreddy
pythongh-128595: Default to stdout isatty for colour detection instea…
9b45be8
…d of stderr (python#128498)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
hugovk added a commit that referenced this pull request Jan 21, 2025
@hugovk @serhiy-storchaka @vstinner
[3.13] gh-128595: Default to stdout isatty for colour detection inste…
383af39
…ad of stderr (GH-128498) (#129057)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Fix `test__colorize` unexpected keyword argument 'file' on buildbots (#129070)
@hugovk hugovk mentioned this pull request Jan 22, 2025
Add colour to GitHub Actions output #129196
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments

Uh oh!

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

Assignees

@hugovk hugovk

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.

6 participants

@hugovk @erlend-aasland @zanieb @serhiy-storchaka @vstinner @picnixz
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.