Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Make PYTHON_COLORS higher precedence than NO_COLOR and FORCE_COLOR
  • Loading branch information
ambv committed Dec 6, 2023
commit f0aea495834e9ad2bc514574bcea7249ebf63e66
3 changes: 2 additions & 1 deletion Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ in the output. This takes precedence over ``FORCE_COLOR``.
All these environment variables are used also by other tools to control color
output. To control the color output only in the Python interpreter, the
:envvar:`PYTHON_COLORS` environment variable can be used. This variable takes
less precedence than ``NO_COLOR`` and ``FORCE_COLOR``.
precedence over ``NO_COLOR``, which in turn takes precedence over
``FORCE_COLOR``.

Options you shouldn't use
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4354,11 +4354,13 @@ def test_colorized_detection_checks_for_environment_variables(self):
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
self.assertEqual(traceback._can_colorize(), False)
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PYTHON_COLORS": '1'}):
self.assertEqual(traceback._can_colorize(), False)
self.assertEqual(traceback._can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
self.assertEqual(traceback._can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
self.assertEqual(traceback._can_colorize(), False)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(traceback._can_colorize(), False)
isatty_mock.return_value = False
self.assertEqual(traceback._can_colorize(), False)

Expand Down
8 changes: 4 additions & 4 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ def _can_colorize():
except (ImportError, AttributeError):
return False

if "NO_COLOR" in os.environ:
return False
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 "FORCE_COLOR" in os.environ:
return True
if os.environ.get("PYTHON_COLORS") == "1":
return True
if os.environ.get("TERM") == "dumb":
return False
try:
Expand Down