Skip to content
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

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

Merged
merged 18 commits into from
Jan 20, 2025

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Jan 4, 2025

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

@hugovk
Copy link
Member Author

hugovk commented Jan 4, 2025

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?

@erlend-aasland
Copy link
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 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
@hugovk hugovk added the needs backport to 3.13 bugs and security fixes label Jan 7, 2025
@hugovk
Copy link
Member Author

hugovk commented Jan 7, 2025

Issue, NEWS and backport label applied.

@zanieb
Copy link
Contributor

zanieb commented Jan 7, 2025

Agree this sounds like a bug to me :)

@serhiy-storchaka
Copy link
Member

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

@hugovk
Copy link
Member Author

hugovk commented Jan 8, 2025

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)

@serhiy-storchaka
Copy link
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
Copy link
Member

vstinner commented Jan 9, 2025

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

@hugovk
Copy link
Member Author

hugovk commented Jan 9, 2025

Sure! Please see PR #128687.

@hugovk
Copy link
Member Author

hugovk commented Jan 13, 2025

Sure! Please see PR #128687.

Now merged, making this PR much simpler.

@serhiy-storchaka
Copy link
Member

Please implement what was discussed above.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the update (adding file). I just have two minor comments.

Lib/unittest/result.py Outdated Show resolved Hide resolved
Lib/_colorize.py Outdated Show resolved Hide resolved
Lib/test/libregrtest/single.py Outdated Show resolved Hide resolved
Lib/unittest/runner.py Outdated Show resolved Hide resolved
Lib/unittest/runner.py Outdated Show resolved Hide resolved
Lib/unittest/result.py Outdated Show resolved Hide resolved
Lib/test/libregrtest/single.py Outdated Show resolved Hide resolved
Lib/unittest/result.py Outdated Show resolved Hide resolved
Lib/unittest/runner.py Outdated Show resolved Hide resolved
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>
Lib/_colorize.py Outdated Show resolved Hide resolved
hugovk and others added 3 commits January 14, 2025 09:19
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@hugovk
Copy link
Member Author

hugovk commented Jan 20, 2025

Thanks again for the reviews!

Next to update #127877.

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

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

@miss-islington-app
Copy link

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
Copy link

bedevere-app bot commented Jan 20, 2025

GH-129057 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jan 20, 2025
@picnixz
Copy link
Member

picnixz commented Jan 20, 2025

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

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

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