Skip to content

Conversation

@hugovk
Copy link
Member

@hugovkhugovk 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
imageimage
imageimage
imageimage
imageimage

@hugovk
Copy link
MemberAuthor

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.

@hugovkhugovk changed the title Add test class helper to force no terminal colourDefault to stdout isatty for colour detection instead of stderrJan 5, 2025
@hugovkhugovk changed the title Default to stdout isatty for colour detection instead of stderrgh-128595: Default to stdout isatty for colour detection instead of stderrJan 7, 2025
@hugovkhugovk added the needs backport to 3.13 bugs and security fixes label Jan 7, 2025
@hugovk
Copy link
MemberAuthor

Issue, NEWS and backport label applied.

@zanieb
Copy link
Contributor

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
MemberAuthor

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

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

@hugovk
Copy link
MemberAuthor

Sure! Please see PR #128687.

@hugovk
Copy link
MemberAuthor

Sure! Please see PR #128687.

Now merged, making this PR much simpler.

@serhiy-storchaka
Copy link
Member

Please implement what was discussed above.

hugovkand 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>
hugovkand others added 3 commits January 14, 2025 09:19
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinnervstinner 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
MemberAuthor

Thanks again for the reviews!

Next to update #127877.

@hugovkhugovk merged commit 6f167d7 into python:mainJan 20, 2025
46 checks passed
@hugovkhugovk 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

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

@bedevere-appbedevere-appbot removed the needs backport to 3.13 bugs and security fixes label Jan 20, 2025
@picnixz
Copy link
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
Copy link
MemberAuthor

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)
Sign up for freeto 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

@hugovk@erlend-aasland@zanieb@serhiy-storchaka@vstinner@picnixz