Skip to content

Commit 9ae4c77

Browse files
committed
Add more docs
Signed-off-by: Pablo Galindo <[email protected]>
1 parent ff19eec commit 9ae4c77

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

Diff for: Doc/using/cmdline.rst

+20
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,26 @@ Miscellaneous options
612612
.. versionadded:: 3.13
613613
The ``-X presite`` option.
614614

615+
Controlling Color
616+
~~~~~~~~~~~~~~~~~
617+
618+
The Python interpreter is configured by default to use colors to highlight
619+
output in certain situations such as when displaying tracebacks. This
620+
behavior can be controlled by setting different environment variables.
621+
622+
Setting the environment variable ``TERM`` to ``dumb`` will disable color.
623+
624+
If the environment variable ``FORCE_COLOR`` is set, then color will be
625+
enabled regardless of the value of TERM. This is useful on CI systems which
626+
aren’t terminals but can none-the-less display ANSI escape sequences.
627+
628+
If the environment variable ``NO_COLOR`` is set, Python will disable all color
629+
in the output. This takes precedence over ``FORCE_COLOR``.
630+
631+
All these environment variables are used also by other tools to control color
632+
output. To control the color output only in the Python interpreter, the
633+
:envvar:`PY_COLORS` environment variable can be used. This variable takes
634+
less precedence than ``NO_COLOR`` and ``FORCE_COLOR``.
615635

616636
Options you shouldn't use
617637
~~~~~~~~~~~~~~~~~~~~~~~~~

Diff for: Lib/test/test_traceback.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import json
2626
import textwrap
2727
import traceback
28+
import contextlib
2829
from functools import partial
2930
from pathlib import Path
3031

@@ -4314,24 +4315,29 @@ def foo():
43144315
self.assertEqual(actual, expected)
43154316

43164317
def test_colorized_detection_checks_for_environment_variables(self):
4317-
with unittest.mock.patch("os.isatty") as isatty_mock:
4318-
isatty_mock.return_value = True
4319-
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
4320-
self.assertEqual(traceback._can_colorize(), False)
4321-
with unittest.mock.patch("os.environ", {'PY_COLORS': '1'}):
4322-
self.assertEqual(traceback._can_colorize(), True)
4323-
with unittest.mock.patch("os.environ", {'PY_COLORS': '0'}):
4324-
self.assertEqual(traceback._can_colorize(), False)
4325-
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
4326-
self.assertEqual(traceback._can_colorize(), False)
4327-
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PY_COLORS": '1'}):
4328-
self.assertEqual(traceback._can_colorize(), False)
4329-
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
4330-
self.assertEqual(traceback._can_colorize(), True)
4331-
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
4318+
if sys.platform == "win32":
4319+
virtual_patching = unittest.mock.patch("nt._supports_virtual_terminal", return_value=True)
4320+
else:
4321+
virtual_patching = contextlib.nullcontext()
4322+
with virtual_patching:
4323+
with unittest.mock.patch("os.isatty") as isatty_mock:
4324+
isatty_mock.return_value = True
4325+
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
4326+
self.assertEqual(traceback._can_colorize(), False)
4327+
with unittest.mock.patch("os.environ", {'PY_COLORS': '1'}):
4328+
self.assertEqual(traceback._can_colorize(), True)
4329+
with unittest.mock.patch("os.environ", {'PY_COLORS': '0'}):
4330+
self.assertEqual(traceback._can_colorize(), False)
4331+
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
4332+
self.assertEqual(traceback._can_colorize(), False)
4333+
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PY_COLORS": '1'}):
4334+
self.assertEqual(traceback._can_colorize(), False)
4335+
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
4336+
self.assertEqual(traceback._can_colorize(), True)
4337+
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
4338+
self.assertEqual(traceback._can_colorize(), False)
4339+
isatty_mock.return_value = False
43324340
self.assertEqual(traceback._can_colorize(), False)
4333-
isatty_mock.return_value = False
4334-
self.assertEqual(traceback._can_colorize(), False)
43354341

43364342
if __name__ == "__main__":
43374343
unittest.main()

0 commit comments

Comments
 (0)