2015-09-29 20:13:53 -03:00
|
|
|
import faulthandler
|
2023-09-14 15:33:18 -03:00
|
|
|
import gc
|
2015-09-29 20:13:53 -03:00
|
|
|
import os
|
2023-09-12 00:35:08 -03:00
|
|
|
import random
|
2015-09-29 20:13:53 -03:00
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from test import support
|
2022-04-25 11:35:14 -03:00
|
|
|
from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII
|
2015-09-29 20:13:53 -03:00
|
|
|
|
2023-09-11 04:02:35 -03:00
|
|
|
from .runtests import RunTests
|
|
|
|
from .utils import (
|
2023-09-11 00:27:37 -03:00
|
|
|
setup_unraisable_hook, setup_threading_excepthook, fix_umask,
|
|
|
|
replace_stdout, adjust_rlimit_nofile)
|
2019-06-12 20:09:04 -03:00
|
|
|
|
2015-09-29 20:13:53 -03:00
|
|
|
|
2021-07-16 10:24:02 -03:00
|
|
|
UNICODE_GUARD_ENV = "PYTHONREGRTEST_UNICODE_GUARD"
|
|
|
|
|
|
|
|
|
2023-09-09 06:18:14 -03:00
|
|
|
def setup_test_dir(testdir: str | None) -> None:
|
2023-09-08 22:37:48 -03:00
|
|
|
if testdir:
|
|
|
|
# Prepend test directory to sys.path, so runtest() will be able
|
|
|
|
# to locate tests
|
|
|
|
sys.path.insert(0, os.path.abspath(testdir))
|
|
|
|
|
|
|
|
|
2023-09-11 00:27:37 -03:00
|
|
|
def setup_process():
|
2023-09-10 20:11:22 -03:00
|
|
|
fix_umask()
|
|
|
|
|
2017-10-13 16:59:12 -03:00
|
|
|
try:
|
|
|
|
stderr_fd = sys.__stderr__.fileno()
|
|
|
|
except (ValueError, AttributeError):
|
|
|
|
# Catch ValueError to catch io.UnsupportedOperation on TextIOBase
|
|
|
|
# and ValueError on a closed stream.
|
|
|
|
#
|
|
|
|
# Catch AttributeError for stderr being None.
|
|
|
|
stderr_fd = None
|
|
|
|
else:
|
|
|
|
# Display the Python traceback on fatal errors (e.g. segfault)
|
|
|
|
faulthandler.enable(all_threads=True, file=stderr_fd)
|
|
|
|
|
|
|
|
# Display the Python traceback on SIGALRM or SIGUSR1 signal
|
|
|
|
signals = []
|
|
|
|
if hasattr(signal, 'SIGALRM'):
|
|
|
|
signals.append(signal.SIGALRM)
|
|
|
|
if hasattr(signal, 'SIGUSR1'):
|
|
|
|
signals.append(signal.SIGUSR1)
|
|
|
|
for signum in signals:
|
|
|
|
faulthandler.register(signum, chain=True, file=stderr_fd)
|
2015-09-29 20:13:53 -03:00
|
|
|
|
2023-09-11 00:27:37 -03:00
|
|
|
adjust_rlimit_nofile()
|
2015-09-29 20:13:53 -03:00
|
|
|
replace_stdout()
|
|
|
|
support.record_original_stdout(sys.stdout)
|
|
|
|
|
|
|
|
# Some times __path__ and __file__ are not absolute (e.g. while running from
|
|
|
|
# Lib/) and, if we change the CWD to run the tests in a temporary dir, some
|
|
|
|
# imports might fail. This affects only the modules imported before os.chdir().
|
|
|
|
# These modules are searched first in sys.path[0] (so '' -- the CWD) and if
|
|
|
|
# they are found in the CWD their __file__ and __path__ will be relative (this
|
|
|
|
# happens before the chdir). All the modules imported after the chdir, are
|
|
|
|
# not found in the CWD, and since the other paths in sys.path[1:] are absolute
|
|
|
|
# (site.py absolutize them), the __file__ and __path__ will be absolute too.
|
|
|
|
# Therefore it is necessary to absolutize manually the __file__ and __path__ of
|
|
|
|
# the packages to prevent later imports to fail when the CWD is different.
|
|
|
|
for module in sys.modules.values():
|
|
|
|
if hasattr(module, '__path__'):
|
2016-03-15 19:08:44 -03:00
|
|
|
for index, path in enumerate(module.__path__):
|
|
|
|
module.__path__[index] = os.path.abspath(path)
|
2018-03-28 02:57:13 -03:00
|
|
|
if getattr(module, '__file__', None):
|
2015-09-29 20:13:53 -03:00
|
|
|
module.__file__ = os.path.abspath(module.__file__)
|
|
|
|
|
2019-05-23 12:45:22 -03:00
|
|
|
if hasattr(sys, 'addaudithook'):
|
|
|
|
# Add an auditing hook for all tests to ensure PySys_Audit is tested
|
|
|
|
def _test_audit_hook(name, args):
|
|
|
|
pass
|
|
|
|
sys.addaudithook(_test_audit_hook)
|
|
|
|
|
2019-06-12 20:09:04 -03:00
|
|
|
setup_unraisable_hook()
|
2021-04-16 09:33:10 -03:00
|
|
|
setup_threading_excepthook()
|
2019-06-12 20:09:04 -03:00
|
|
|
|
2023-09-11 00:27:37 -03:00
|
|
|
# Ensure there's a non-ASCII character in env vars at all times to force
|
|
|
|
# tests consider this case. See BPO-44647 for details.
|
|
|
|
if TESTFN_UNDECODABLE and os.supports_bytes_environ:
|
|
|
|
os.environb.setdefault(UNICODE_GUARD_ENV.encode(), TESTFN_UNDECODABLE)
|
|
|
|
elif FS_NONASCII:
|
|
|
|
os.environ.setdefault(UNICODE_GUARD_ENV, FS_NONASCII)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_tests(runtests: RunTests):
|
|
|
|
support.verbose = runtests.verbose
|
|
|
|
support.failfast = runtests.fail_fast
|
|
|
|
support.PGO = runtests.pgo
|
|
|
|
support.PGO_EXTENDED = runtests.pgo_extended
|
|
|
|
|
|
|
|
support.set_match_tests(runtests.match_tests, runtests.ignore_tests)
|
|
|
|
|
|
|
|
if runtests.use_junit:
|
|
|
|
support.junit_xml_list = []
|
|
|
|
from test.support.testresult import RegressionTestResult
|
|
|
|
RegressionTestResult.USE_XML = True
|
|
|
|
else:
|
|
|
|
support.junit_xml_list = None
|
|
|
|
|
|
|
|
if runtests.memory_limit is not None:
|
|
|
|
support.set_memlimit(runtests.memory_limit)
|
|
|
|
|
|
|
|
support.suppress_msvcrt_asserts(runtests.verbose >= 2)
|
|
|
|
|
|
|
|
support.use_resources = runtests.use_resources
|
|
|
|
|
2023-09-08 22:37:48 -03:00
|
|
|
timeout = runtests.timeout
|
|
|
|
if timeout is not None:
|
2019-10-30 08:41:43 -03:00
|
|
|
# For a slow buildbot worker, increase SHORT_TIMEOUT and LONG_TIMEOUT
|
2023-09-08 22:37:48 -03:00
|
|
|
support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, timeout / 40)
|
|
|
|
support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, timeout / 4)
|
2019-10-30 08:41:43 -03:00
|
|
|
|
|
|
|
# If --timeout is short: reduce timeouts
|
2023-09-08 22:37:48 -03:00
|
|
|
support.LOOPBACK_TIMEOUT = min(support.LOOPBACK_TIMEOUT, timeout)
|
|
|
|
support.INTERNET_TIMEOUT = min(support.INTERNET_TIMEOUT, timeout)
|
|
|
|
support.SHORT_TIMEOUT = min(support.SHORT_TIMEOUT, timeout)
|
|
|
|
support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, timeout)
|
2019-10-30 08:41:43 -03:00
|
|
|
|
2023-09-11 00:27:37 -03:00
|
|
|
if runtests.hunt_refleak:
|
|
|
|
unittest.BaseTestSuite._cleanup = False
|
2021-10-21 22:22:25 -03:00
|
|
|
|
2023-09-11 00:27:37 -03:00
|
|
|
if runtests.gc_threshold is not None:
|
|
|
|
gc.set_threshold(runtests.gc_threshold)
|
2023-09-12 00:35:08 -03:00
|
|
|
|
|
|
|
if runtests.randomize:
|
|
|
|
random.seed(runtests.random_seed)
|