Skip to content

Commit 62a3b6b

Browse files
miss-islingtoncmaloneyvstinner
authored
[3.13] gh-140082: Forward colorizing from libregrtest to unittest (GH-140083) (#140755)
gh-140082: Forward colorizing from libregrtest to unittest (GH-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 b3649a7 commit 62a3b6b

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
@@ -141,3 +142,10 @@ def setup_tests(runtests: RunTests) -> None:
141142
gc.set_threshold(runtests.gc_threshold)
142143

143144
random.seed(runtests.random_seed)
145+
146+
# sys.stdout is redirected to a StringIO in single process mode on which
147+
# color auto-detect fails as StringIO is not a TTY. If the original
148+
# sys.stdout supports color pass that through with FORCE_COLOR so that when
149+
# results are printed, such as with -W, they get color.
150+
if can_colorize(file=sys.stdout):
151+
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
@@ -29,6 +30,12 @@ def create_worker_process(runtests: WorkerRunTests, output_fd: int,
2930
env['TEMP'] = tmp_dir
3031
env['TMP'] = tmp_dir
3132

33+
# The subcommand is run with a temporary output which means it is not a TTY
34+
# and won't auto-color. The test results are printed to stdout so if we can
35+
# color that have the subprocess use color.
36+
if can_colorize(file=sys.stdout):
37+
env['FORCE_COLOR'] = '1'
38+
3239
# Running the child from the same working directory as regrtest's original
3340
# invocation ensures that TEMPDIR for the child is the same when
3441
# 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)