| 1 |
import faulthandler |
| 2 |
import gc |
| 3 |
import io |
| 4 |
import os |
| 5 |
import random |
| 6 |
import signal |
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
from test import support |
| 10 |
from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII |
| 11 |
from _colorize import can_colorize # type: ignore[import-not-found] |
| 12 |
|
| 13 |
from .filter import set_match_tests |
| 14 |
from .runtests import RunTests |
| 15 |
from .utils import ( |
| 16 |
setup_unraisable_hook, setup_threading_excepthook, |
| 17 |
adjust_rlimit_nofile) |
| 18 |
|
| 19 |
|
| 20 |
UNICODE_GUARD_ENV = "PYTHONREGRTEST_UNICODE_GUARD" |
| 21 |
|
| 22 |
|
| 23 |
def setup_test_dir(testdir: str | None) -> None: |
| 24 |
if testdir: |
| 25 |
# Prepend test directory to sys.path, so runtest() will be able |
| 26 |
# to locate tests |
| 27 |
sys.path.insert(0, os.path.abspath(testdir)) |
| 28 |
|
| 29 |
|
| 30 |
def setup_process() -> None: |
| 31 |
assert sys.__stderr__ is not None, "sys.__stderr__ is None" |
| 32 |
try: |
| 33 |
stderr_fd = sys.__stderr__.fileno() |
| 34 |
except (ValueError, AttributeError): |
| 35 |
# Catch ValueError to catch io.UnsupportedOperation on TextIOBase |
| 36 |
# and ValueError on a closed stream. |
| 37 |
# |
| 38 |
# Catch AttributeError for stderr being None. |
| 39 |
pass |
| 40 |
else: |
| 41 |
# Display the Python traceback on fatal errors (e.g. segfault) |
| 42 |
faulthandler.enable(all_threads=True, file=stderr_fd) |
| 43 |
|
| 44 |
# Display the Python traceback on SIGALRM or SIGUSR1 signal |
| 45 |
signals: list[signal.Signals] = [] |
| 46 |
if hasattr(signal, 'SIGALRM'): |
| 47 |
signals.append(signal.SIGALRM) |
| 48 |
if hasattr(signal, 'SIGUSR1'): |
| 49 |
signals.append(signal.SIGUSR1) |
| 50 |
for signum in signals: |
| 51 |
faulthandler.register(signum, chain=True, file=stderr_fd) |
| 52 |
|
| 53 |
adjust_rlimit_nofile() |
| 54 |
|
| 55 |
support.record_original_stdout(sys.stdout) |
| 56 |
|
| 57 |
# Set sys.stdout encoder error handler to backslashreplace, |
| 58 |
# similar to sys.stderr error handler, to avoid UnicodeEncodeError |
| 59 |
# when printing a traceback or any other non-encodable character. |
| 60 |
# |
| 61 |
# Use an assertion to fix mypy error. |
| 62 |
assert isinstance(sys.stdout, io.TextIOWrapper) |
| 63 |
sys.stdout.reconfigure(errors="backslashreplace") |
| 64 |
|
| 65 |
# Some times __path__ and __file__ are not absolute (e.g. while running from |
| 66 |
# Lib/) and, if we change the CWD to run the tests in a temporary dir, some |
| 67 |
# imports might fail. This affects only the modules imported before os.chdir(). |
| 68 |
# These modules are searched first in sys.path[0] (so '' -- the CWD) and if |
| 69 |
# they are found in the CWD their __file__ and __path__ will be relative (this |
| 70 |
# happens before the chdir). All the modules imported after the chdir, are |
| 71 |
# not found in the CWD, and since the other paths in sys.path[1:] are absolute |
| 72 |
# (site.py absolutize them), the __file__ and __path__ will be absolute too. |
| 73 |
# Therefore it is necessary to absolutize manually the __file__ and __path__ of |
| 74 |
# the packages to prevent later imports to fail when the CWD is different. |
| 75 |
for module in sys.modules.values(): |
| 76 |
if hasattr(module, '__path__'): |
| 77 |
for index, path in enumerate(module.__path__): |
| 78 |
module.__path__[index] = os.path.abspath(path) |
| 79 |
if getattr(module, '__file__', None): |
| 80 |
module.__file__ = os.path.abspath(module.__file__) # type: ignore[type-var] |
| 81 |
|
| 82 |
if hasattr(sys, 'addaudithook'): |
| 83 |
# Add an auditing hook for all tests to ensure PySys_Audit is tested |
| 84 |
def _test_audit_hook(name, args): |
| 85 |
pass |
| 86 |
sys.addaudithook(_test_audit_hook) |
| 87 |
|
| 88 |
setup_unraisable_hook() |
| 89 |
setup_threading_excepthook() |
| 90 |
|
| 91 |
# Ensure there's a non-ASCII character in env vars at all times to force |
| 92 |
# tests consider this case. See BPO-44647 for details. |
| 93 |
if TESTFN_UNDECODABLE and os.supports_bytes_environ: |
| 94 |
os.environb.setdefault(UNICODE_GUARD_ENV.encode(), TESTFN_UNDECODABLE) |
| 95 |
elif FS_NONASCII: |
| 96 |
os.environ.setdefault(UNICODE_GUARD_ENV, FS_NONASCII) |
| 97 |
|
| 98 |
|
| 99 |
def setup_tests(runtests: RunTests) -> None: |
| 100 |
support.verbose = runtests.verbose |
| 101 |
support.failfast = runtests.fail_fast |
| 102 |
support.PGO = runtests.pgo |
| 103 |
support.PGO_EXTENDED = runtests.pgo_extended |
| 104 |
|
| 105 |
set_match_tests(runtests.match_tests) |
| 106 |
|
| 107 |
if runtests.use_junit: |
| 108 |
support.junit_xml_list = [] |
| 109 |
from .testresult import RegressionTestResult |
| 110 |
RegressionTestResult.USE_XML = True |
| 111 |
else: |
| 112 |
support.junit_xml_list = None |
| 113 |
|
| 114 |
if runtests.memory_limit is not None: |
| 115 |
support.set_memlimit(runtests.memory_limit) |
| 116 |
|
| 117 |
support.suppress_msvcrt_asserts(runtests.verbose >= 2) |
| 118 |
|
| 119 |
support.use_resources = runtests.use_resources |
| 120 |
|
| 121 |
timeout = runtests.timeout |
| 122 |
if timeout is not None: |
| 123 |
# For a slow buildbot worker, increase SHORT_TIMEOUT and LONG_TIMEOUT |
| 124 |
support.LOOPBACK_TIMEOUT = max(support.LOOPBACK_TIMEOUT, timeout / 120) |
| 125 |
# don't increase INTERNET_TIMEOUT |
| 126 |
support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, timeout / 40) |
| 127 |
support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, timeout / 4) |
| 128 |
|
| 129 |
# If --timeout is short: reduce timeouts |
| 130 |
support.LOOPBACK_TIMEOUT = min(support.LOOPBACK_TIMEOUT, timeout) |
| 131 |
support.INTERNET_TIMEOUT = min(support.INTERNET_TIMEOUT, timeout) |
| 132 |
support.SHORT_TIMEOUT = min(support.SHORT_TIMEOUT, timeout) |
| 133 |
support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, timeout) |
| 134 |
|
| 135 |
if runtests.hunt_refleak: |
| 136 |
# private attribute that mypy doesn't know about: |
| 137 |
unittest.BaseTestSuite._cleanup = False # type: ignore[attr-defined] |
| 138 |
|
| 139 |
if runtests.gc_threshold is not None: |
| 140 |
gc.set_threshold(runtests.gc_threshold) |
| 141 |
|
| 142 |
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" |