| 1 |
import faulthandler |
| 2 |
import gc |
| 3 |
import os |
| 4 |
import random |
| 5 |
import signal |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
from test import support |
| 9 |
from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII |
| 10 |
|
| 11 |
from .filter import set_match_tests |
| 12 |
from .runtests import RunTests |
| 13 |
from .utils import ( |
| 14 |
setup_unraisable_hook, setup_threading_excepthook, fix_umask, |
| 15 |
adjust_rlimit_nofile) |
| 16 |
|
| 17 |
|
| 18 |
UNICODE_GUARD_ENV = "PYTHONREGRTEST_UNICODE_GUARD" |
| 19 |
|
| 20 |
|
| 21 |
def setup_test_dir(testdir: str | None) -> None: |
| 22 |
if testdir: |
| 23 |
|
| 24 |
|
| 25 |
sys.path.insert(0, os.path.abspath(testdir)) |
| 26 |
|
| 27 |
|
| 28 |
def setup_process(): |
| 29 |
fix_umask() |
| 30 |
|
| 31 |
try: |
| 32 |
stderr_fd = sys.__stderr__.fileno() |
| 33 |
except (ValueError, AttributeError): |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
stderr_fd = None |
| 39 |
else: |
| 40 |
|
| 41 |
faulthandler.enable(all_threads=True, file=stderr_fd) |
| 42 |
|
| 43 |
|
| 44 |
signals = [] |
| 45 |
if hasattr(signal, 'SIGALRM'): |
| 46 |
signals.append(signal.SIGALRM) |
| 47 |
if hasattr(signal, 'SIGUSR1'): |
| 48 |
signals.append(signal.SIGUSR1) |
| 49 |
for signum in signals: |
| 50 |
faulthandler.register(signum, chain=True, file=stderr_fd) |
| 51 |
|
| 52 |
adjust_rlimit_nofile() |
| 53 |
|
| 54 |
support.record_original_stdout(sys.stdout) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
for module in sys.modules.values(): |
| 67 |
if hasattr(module, '__path__'): |
| 68 |
for index, path in enumerate(module.__path__): |
| 69 |
module.__path__[index] = os.path.abspath(path) |
| 70 |
if getattr(module, '__file__', None): |
| 71 |
module.__file__ = os.path.abspath(module.__file__) |
| 72 |
|
| 73 |
if hasattr(sys, 'addaudithook'): |
| 74 |
|
| 75 |
def _test_audit_hook(name, args): |
| 76 |
pass |
| 77 |
sys.addaudithook(_test_audit_hook) |
| 78 |
|
| 79 |
setup_unraisable_hook() |
| 80 |
setup_threading_excepthook() |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if TESTFN_UNDECODABLE and os.supports_bytes_environ: |
| 85 |
os.environb.setdefault(UNICODE_GUARD_ENV.encode(), TESTFN_UNDECODABLE) |
| 86 |
elif FS_NONASCII: |
| 87 |
os.environ.setdefault(UNICODE_GUARD_ENV, FS_NONASCII) |
| 88 |
|
| 89 |
|
| 90 |
def setup_tests(runtests: RunTests): |
| 91 |
support.verbose = runtests.verbose |
| 92 |
support.failfast = runtests.fail_fast |
| 93 |
support.PGO = runtests.pgo |
| 94 |
support.PGO_EXTENDED = runtests.pgo_extended |
| 95 |
|
| 96 |
set_match_tests(runtests.match_tests) |
| 97 |
|
| 98 |
if runtests.use_junit: |
| 99 |
support.junit_xml_list = [] |
| 100 |
from .testresult import RegressionTestResult |
| 101 |
RegressionTestResult.USE_XML = True |
| 102 |
else: |
| 103 |
support.junit_xml_list = None |
| 104 |
|
| 105 |
if runtests.memory_limit is not None: |
| 106 |
support.set_memlimit(runtests.memory_limit) |
| 107 |
|
| 108 |
support.suppress_msvcrt_asserts(runtests.verbose >= 2) |
| 109 |
|
| 110 |
support.use_resources = runtests.use_resources |
| 111 |
|
| 112 |
timeout = runtests.timeout |
| 113 |
if timeout is not None: |
| 114 |
|
| 115 |
support.LOOPBACK_TIMEOUT = max(support.LOOPBACK_TIMEOUT, timeout / 120) |
| 116 |
|
| 117 |
support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, timeout / 40) |
| 118 |
support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, timeout / 4) |
| 119 |
|
| 120 |
|
| 121 |
support.LOOPBACK_TIMEOUT = min(support.LOOPBACK_TIMEOUT, timeout) |
| 122 |
support.INTERNET_TIMEOUT = min(support.INTERNET_TIMEOUT, timeout) |
| 123 |
support.SHORT_TIMEOUT = min(support.SHORT_TIMEOUT, timeout) |
| 124 |
support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, timeout) |
| 125 |
|
| 126 |
if runtests.hunt_refleak: |
| 127 |
|
| 128 |
unittest.BaseTestSuite._cleanup = False |
| 129 |
|
| 130 |
if runtests.gc_threshold is not None: |
| 131 |
gc.set_threshold(runtests.gc_threshold) |
| 132 |
|
| 133 |
random.seed(runtests.random_seed) |