-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-117225: Move colorize functionality to own internal module #118283
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5a92ed9
Move colorize functionality to own internal module
hugovk a0bf815
Add _colorize.get_colors and refactor
hugovk 005e44e
Precompute stubbed ANSIColors with no colours
hugovk d5b06b3
Merge branch 'main' into doctest-refactor-colorize
hugovk d653c12
Merge branch 'main' into doctest-refactor-colorize
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import io | ||
import os | ||
import sys | ||
|
||
COLORIZE = True | ||
|
||
|
||
class ANSIColors: | ||
BOLD_GREEN = "\x1b[1;32m" | ||
BOLD_MAGENTA = "\x1b[1;35m" | ||
BOLD_RED = "\x1b[1;31m" | ||
GREEN = "\x1b[32m" | ||
GREY = "\x1b[90m" | ||
MAGENTA = "\x1b[35m" | ||
RED = "\x1b[31m" | ||
RESET = "\x1b[0m" | ||
YELLOW = "\x1b[33m" | ||
|
||
|
||
NoColors = ANSIColors() | ||
|
||
for attr in dir(NoColors): | ||
if not attr.startswith("__"): | ||
setattr(NoColors, attr, "") | ||
|
||
|
||
def get_colors(colorize: bool = False) -> ANSIColors: | ||
if colorize or can_colorize(): | ||
return ANSIColors() | ||
else: | ||
return NoColors | ||
|
||
|
||
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 | ||
if os.environ.get("PYTHON_COLORS") == "1": | ||
return True | ||
if "NO_COLOR" in os.environ: | ||
return False | ||
if not COLORIZE: | ||
return False | ||
if not sys.flags.ignore_environment: | ||
if "FORCE_COLOR" in os.environ: | ||
return True | ||
if os.environ.get("TERM") == "dumb": | ||
return False | ||
|
||
if not hasattr(sys.stderr, "fileno"): | ||
return False | ||
|
||
try: | ||
return os.isatty(sys.stderr.fileno()) | ||
except io.UnsupportedOperation: | ||
return sys.stderr.isatty() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import contextlib | ||
import sys | ||
import unittest | ||
import unittest.mock | ||
import _colorize | ||
from test.support import force_not_colorized | ||
|
||
ORIGINAL_CAN_COLORIZE = _colorize.can_colorize | ||
|
||
|
||
def setUpModule(): | ||
_colorize.can_colorize = lambda: False | ||
|
||
|
||
def tearDownModule(): | ||
_colorize.can_colorize = ORIGINAL_CAN_COLORIZE | ||
|
||
|
||
class TestColorizeFunction(unittest.TestCase): | ||
@force_not_colorized | ||
def test_colorized_detection_checks_for_environment_variables(self): | ||
if sys.platform == "win32": | ||
virtual_patching = unittest.mock.patch("nt._supports_virtual_terminal", | ||
return_value=True) | ||
else: | ||
virtual_patching = contextlib.nullcontext() | ||
with virtual_patching: | ||
|
||
flags = unittest.mock.MagicMock(ignore_environment=False) | ||
with (unittest.mock.patch("os.isatty") as isatty_mock, | ||
unittest.mock.patch("sys.flags", flags), | ||
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) | ||
isatty_mock.return_value = False | ||
with unittest.mock.patch("os.environ", {}): | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.