FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-128595: Default to stdout isatty for colour detection instead of stderr by hugovk · Pull Request #128498 · python/cpython · GitHub

/ cpython Public

gh-128595: Default to stdout isatty for colour detection instead of stderr - #128498

Merged
hugovk merged 18 commits into
python:mainfrom
hugovk:3.14-color-default-stdout
Jan 20, 2025
Merged

gh-128595: Default to stdout isatty for colour detection instead of stderr#128498
hugovk merged 18 commits into
python:mainfrom
hugovk:3.14-color-default-stdout

Conversation

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

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

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?

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 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 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
hugovk added the needs backport to 3.13 bugs and security fixes label Jan 7, 2025

hugovk commented Jan 7, 2025

Copy link
Copy Markdown
Member Author

Issue, NEWS and backport label applied.

zanieb commented Jan 7, 2025

Copy link
Copy Markdown
Contributor

Agree this sounds like a bug to me :)

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.

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)

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.

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?

hugovk commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

Sure! Please see PR #128687.

hugovk commented Jan 13, 2025

Copy link
Copy Markdown
Member Author

Sure! Please see PR #128687.

Now merged, making this PR much simpler.

Copy link
Copy Markdown
Member

Please implement what was discussed above.

Comment thread Lib/test/libregrtest/single.py Outdated
Comment thread Lib/unittest/result.py Outdated
Comment thread Lib/unittest/runner.py Outdated
hugovk and others added 4 commits January 13, 2025 19:00
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Comment thread Lib/_colorize.py Outdated
Comment thread Lib/test/libregrtest/single.py Outdated
hugovk and others added 3 commits January 14, 2025 09:19
Co-authored-by: Victor Stinner <vstinner@python.org>

vstinner left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

hugovk commented Jan 20, 2025

Copy link
Copy Markdown
Member Author

Thanks again for the reviews!

Next to update #127877.

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

Copy link
Copy Markdown

Thanks @hugovk for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

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

hugovk added a commit to hugovk/cpython that referenced this pull request Jan 20, 2025
… instead of stderr (pythonGH-128498)

(cherry picked from commit 6f167d7)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
hugovk added a commit to hugovk/cpython that referenced this pull request Jan 20, 2025
… instead of stderr (pythonGH-128498)

(cherry picked from commit 6f167d7)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>

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.

bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jan 20, 2025

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 commented Jan 20, 2025

Copy link
Copy Markdown
Member Author

Please see PR #129070 for a fix.

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 21, 2025
…d of stderr (python#128498)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
hugovk added a commit that referenced this pull request Jan 21, 2025
…ad of stderr (GH-128498) (#129057)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Fix `test__colorize` unexpected keyword argument 'file' on buildbots (#129070)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL