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

Conversation

PalmtopTiger
Copy link
Contributor

@PalmtopTiger PalmtopTiger commented Nov 28, 2024

I think nt._supports_virtual_terminal call should be placed above the isatty check so that the behavior of can_colorize function on Windows matches its behavior on POSIX OSes.

Screenshot of exception with patched can_colorize function in ConEmu terminal:
241128153324_p

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Thanks for contributing! It's great to see new people :)

Generally, all bug fixes need tests. I'm not 100% sure if we can test for this though; could you look around in the traceback tests?

@PalmtopTiger
Copy link
Contributor Author

Tests added. I split tests for non-Windows and Windows OSes into two different methods, hence the many changes in test__colorize.py.

@ZeroIntensity ZeroIntensity requested a review from hugovk November 30, 2024 01:16
@hugovk hugovk added OS-windows stdlib Python modules in the Lib dir labels Nov 30, 2024
@hugovk
Copy link
Member

hugovk commented Nov 30, 2024

Thank you, looks good.

I don't have Windows, it'd be useful if someone else could do some manual verification on Windows.

@hugovk
Copy link
Member

hugovk commented Dec 12, 2024

I've tested this on GitHub Actions. Currently, adding FORCE_COLOR: 1 does not add colour, but it does with this PR. Let's merge, thanks!

@hugovk hugovk merged commit 365451e into python:main Dec 12, 2024
42 checks passed
@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 12, 2024
@bedevere-app
Copy link

bedevere-app bot commented Dec 12, 2024

GH-127886 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 Dec 12, 2024
@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@ZeroIntensity
Copy link
Member

It looks like we just blew up the build bots.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@hugovk
Copy link
Member

hugovk commented Dec 12, 2024

It looks like we just blew up the build bots.

We did! It's this:

0:02:53 load avg: 9.23 [411/481/1] test__colorize failed (1 failure)
test_colorized_detection_checks_for_environment_variables (test.test__colorize.TestColorizeFunction.test_colorized_detection_checks_for_environment_variables) ... FAIL
test_colorized_detection_checks_for_environment_variables_no_vt (test.test__colorize.TestColorizeFunction.test_colorized_detection_checks_for_environment_variables_no_vt) ... skipped 'Windows only'

======================================================================
FAIL: test_colorized_detection_checks_for_environment_variables (test.test__colorize.TestColorizeFunction.test_colorized_detection_checks_for_environment_variables)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/support/__init__.py", line 2848, in wrapper
    return func(*args, **kwargs)
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test__colorize.py", line 54, in test_colorized_detection_checks_for_environment_variables
    self.assertEqual(_colorize.can_colorize(), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

----------------------------------------------------------------------
Ran 2 tests in 0.007s

FAILED (failures=1, skipped=1)
test test__colorize failed

I've disabled automerge of the backport (#127886), and we'd better revert this. Then we can open a new PR and make sure the buildbots pass before merging.

@bedevere-bot

This comment has been minimized.

@hugovk
Copy link
Member

hugovk commented Dec 12, 2024

Revert PR: #127889

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

hugovk added a commit that referenced this pull request Dec 12, 2024
@@ -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.

@bedevere-bot

This comment has been minimized.

@bedevere-bot

This comment has been minimized.

@PalmtopTiger PalmtopTiger deleted the gh-127353 branch December 13, 2024 15:54
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants