Skip to content

gh-127353: Allow to force color output on Windows #127354

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

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ def get_colors(colorize: bool = False) -> ANSIColors:


def can_colorize() -> bool:
if sys.platform == "win32":
try:
import nt

if not nt._supports_virtual_terminal():
return False
except (ImportError, AttributeError):
return False
if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
Expand All @@ -58,6 +50,15 @@ def can_colorize() -> bool:
if not hasattr(sys.stderr, "fileno"):
return False

if sys.platform == "win32":
try:
import nt

if not nt._supports_virtual_terminal():
return False
except (ImportError, AttributeError):
return False

try:
return os.isatty(sys.stderr.fileno())
except io.UnsupportedOperation:
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,47 @@ def test_colorized_detection_checks_for_environment_variables(self):
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {}):
self.assertEqual(_colorize.can_colorize(), True)
Copy link
Member

Choose a reason for hiding this comment

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

This is the line that failed for buildbots.

We're mocking most of the things in _colorize.can_colorize(), but not this?

    if not hasattr(sys.stderr, "fileno"):
        return False

@PalmtopTiger What do you think? Do we need extra mocking here?

Copy link
Contributor Author

@PalmtopTiger PalmtopTiger Dec 13, 2024

Choose a reason for hiding this comment

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

Apparently so. I'm thinking of two variants.
A simple one:

with (unittest.mock.patch("os.isatty") as isatty_mock,
      unittest.mock.patch("sys.stderr.isatty") as stderr_isatty_mock):
    isatty_mock.return_value = True
    stderr_isatty_mock.return_value = True
    ...
    isatty_mock.return_value = False
    stderr_isatty_mock.return_value = False

Or a more complex one that implements all the necessary stderr methods:

with (unittest.mock.patch("os.isatty") as isatty_mock,
     unittest.mock.patch("sys.stderr") as stderr_mock):
    isatty_mock.return_value = True
    stderr_mock.fileno.return_value = 2
    stderr_mock.isatty.return_value = True
    ...
    isatty_mock.return_value = False
    stderr_mock.isatty.return_value = False

But in this case, the implementation of sys.stderr.isatty becomes unnecessary.
Which option do you think is better?

Also I don't know how to commit the fix now. 😅 To this branch?

Copy link
Member

Choose a reason for hiding this comment

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

I think the simpler one, we can make it more complex if a need arises. Or should we just skip this test line?

Also I don't know how to commit the fix now. 😅 To this branch?

You can commit to any branch you like, but you will need to open a fresh PR. We can use the same issue number.

By default, we only run GitHub Actions on PRs, but we can manually request some buildbots.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested both. Simple one doesn't pass hasattr(sys.stderr, "fileno") check. Used complex one.

Or should we just skip this test line?

Then, the case where color output is supported will not be tested.


isatty_mock.return_value = False
with unittest.mock.patch("os.environ", {}):
self.assertEqual(_colorize.can_colorize(), False)

@force_not_colorized
@unittest.skipUnless(sys.platform == "win32", "Windows only")
def test_colorized_detection_checks_for_environment_variables_no_vt(self):
with (unittest.mock.patch("nt._supports_virtual_terminal", return_value=False),
unittest.mock.patch("os.isatty") as isatty_mock,
unittest.mock.patch("sys.flags", unittest.mock.MagicMock(ignore_environment=False)),
unittest.mock.patch("_colorize.can_colorize", ORIGINAL_CAN_COLORIZE)):
isatty_mock.return_value = True
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ",
{'NO_COLOR': '1', "PYTHON_COLORS": '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {}):
self.assertEqual(_colorize.can_colorize(), False)

isatty_mock.return_value = False
with unittest.mock.patch("os.environ", {}):
self.assertEqual(_colorize.can_colorize(), False)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow to force color output on Windows using environment variables. Patch by
Andrey Efremov.
Loading