Skip to content

Commit 58ce57a

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

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-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

+18-17
Original file line numberDiff line numberDiff line change
@@ -4314,24 +4314,25 @@ def foo():
43144314
self.assertEqual(actual, expected)
43154315

43164316
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'}):
4317+
with unittest.mock.patch("nt._supports_virtual_terminal", return_value=True):
4318+
with unittest.mock.patch("os.isatty") as isatty_mock:
4319+
isatty_mock.return_value = True
4320+
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
4321+
self.assertEqual(traceback._can_colorize(), False)
4322+
with unittest.mock.patch("os.environ", {'PY_COLORS': '1'}):
4323+
self.assertEqual(traceback._can_colorize(), True)
4324+
with unittest.mock.patch("os.environ", {'PY_COLORS': '0'}):
4325+
self.assertEqual(traceback._can_colorize(), False)
4326+
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
4327+
self.assertEqual(traceback._can_colorize(), False)
4328+
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PY_COLORS": '1'}):
4329+
self.assertEqual(traceback._can_colorize(), False)
4330+
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
4331+
self.assertEqual(traceback._can_colorize(), True)
4332+
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
4333+
self.assertEqual(traceback._can_colorize(), False)
4334+
isatty_mock.return_value = False
43204335
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'}):
4332-
self.assertEqual(traceback._can_colorize(), False)
4333-
isatty_mock.return_value = False
4334-
self.assertEqual(traceback._can_colorize(), False)
43354336

43364337
if __name__ == "__main__":
43374338
unittest.main()

0 commit comments

Comments
 (0)