Skip to content

Commit 34afd7c

Browse files
cmaloneyvstinner
authored andcommitted
pythongh-140082: Forward colorizing from libregrtest to unittest (pythonGH-140083)
libregrtest redirects test output to a file as part of its operation. When `unittest` checks to see if it should colorize with `isatty(sys.stdout)` that fails resulting in no colorizing of the unittest output. Update `libregrtest` to set `FORCE_COLOR=1` when redirecting test output so that unittest will do color printing. (cherry picked from commit 6ff62ac) Co-authored-by: Cody Maloney <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 9a7dccd commit 34afd7c

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Lib/test/libregrtest/setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import unittest
99
from test import support
1010
from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII
11+
from _colorize import can_colorize # type: ignore[import-not-found]
1112

1213
from .filter import set_match_tests
1314
from .runtests import RunTests
@@ -139,3 +140,10 @@ def setup_tests(runtests: RunTests) -> None:
139140
gc.set_threshold(runtests.gc_threshold)
140141

141142
random.seed(runtests.random_seed)
143+
144+
# sys.stdout is redirected to a StringIO in single process mode on which
145+
# color auto-detect fails as StringIO is not a TTY. If the original
146+
# sys.stdout supports color pass that through with FORCE_COLOR so that when
147+
# results are printed, such as with -W, they get color.
148+
if can_colorize(file=sys.stdout):
149+
os.environ['FORCE_COLOR'] = "1"

Lib/test/libregrtest/worker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22
import sys
33
import os
4+
from _colorize import can_colorize # type: ignore[import-not-found]
45
from typing import Any, NoReturn
56

67
from test.support import os_helper, Py_DEBUG
@@ -32,6 +33,12 @@ def create_worker_process(runtests: WorkerRunTests, output_fd: int,
3233
env['TEMP'] = tmp_dir
3334
env['TMP'] = tmp_dir
3435

36+
# The subcommand is run with a temporary output which means it is not a TTY
37+
# and won't auto-color. The test results are printed to stdout so if we can
38+
# color that have the subprocess use color.
39+
if can_colorize(file=sys.stdout):
40+
env['FORCE_COLOR'] = '1'
41+
3542
# Running the child from the same working directory as regrtest's original
3643
# invocation ensures that TEMPDIR for the child is the same when
3744
# sysconfig.is_python_build() is true. See issue 15300.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Update ``python -m test`` to set ``FORCE_COLOR=1`` when being run with color
2+
enabled so that :mod:`unittest` which is run by it with redirected output will
3+
output in color.

0 commit comments

Comments
 (0)