-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Conversation
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? |
Issue, NEWS and backport label applied. |
Agree this sounds like a bug to me :) |
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 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) |
Yes, except that dynamic value |
Can you extract the code to disable colors as a separated PR and merge it first? |
Sure! Please see PR #128687. |
Now merged, making this PR much simpler. |
Please implement what was discussed above. |
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. Thanks for the update (adding file
). I just have two minor comments.
Misc/NEWS.d/next/Library/2025-01-07-21-48-32.gh-issue-128498.n6jtlW.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
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
Thanks again for the reviews! Next to update #127877. |
Thanks @hugovk for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Sorry, @hugovk, I could not cleanly backport this to
|
… 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>
… 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>
GH-129057 is a backport of this pull request to the 3.13 branch. |
Some build bots are now failing (https://buildbot.python.org/api/v2/logs/12166870/raw_inline):
|
As suggested at #128317 (comment), default to checking
stdout
'sisatty
rather thanstderr
's.Also using the
@force_not_colorized_test_class
decorator from #127877 to run some tests without worrying about colour.