blob: b79175944d2bdf3b7bb8e37d4a422f873a31eac4 [file] [log] [blame]
Victor Stinner234cbef2015-09-30 01:13:53 +02001import atexit
2import faulthandler
3import os
4import signal
5import sys
6import unittest
7from test import support
8try:
9 import gc
10except ImportError:
11 gc = None
12
Victor Stinnerb136b1a2021-04-16 14:33:10 +020013from test.libregrtest.utils import (setup_unraisable_hook,
14 setup_threading_excepthook)
Victor Stinner95f61c82019-06-13 01:09:04 +020015
Victor Stinner234cbef2015-09-30 01:13:53 +020016
Miss Islington (bot)93d36a52021-07-16 06:55:26 -070017UNICODE_GUARD_ENV = "PYTHONREGRTEST_UNICODE_GUARD"
18
19
Victor Stinnera2045022015-09-30 02:17:28 +020020def setup_tests(ns):
Victor Stinnerccef8232017-10-13 12:59:12 -070021 try:
22 stderr_fd = sys.__stderr__.fileno()
23 except (ValueError, AttributeError):
24 # Catch ValueError to catch io.UnsupportedOperation on TextIOBase
25 # and ValueError on a closed stream.
26 #
27 # Catch AttributeError for stderr being None.
28 stderr_fd = None
29 else:
30 # Display the Python traceback on fatal errors (e.g. segfault)
31 faulthandler.enable(all_threads=True, file=stderr_fd)
Victor Stinner234cbef2015-09-30 01:13:53 +020032
Victor Stinnerccef8232017-10-13 12:59:12 -070033 # Display the Python traceback on SIGALRM or SIGUSR1 signal
34 signals = []
35 if hasattr(signal, 'SIGALRM'):
36 signals.append(signal.SIGALRM)
37 if hasattr(signal, 'SIGUSR1'):
38 signals.append(signal.SIGUSR1)
39 for signum in signals:
40 faulthandler.register(signum, chain=True, file=stderr_fd)
Victor Stinner234cbef2015-09-30 01:13:53 +020041
Miss Islington (bot)8f6aa482021-10-21 21:47:07 -070042 _adjust_resource_limits()
Victor Stinner234cbef2015-09-30 01:13:53 +020043 replace_stdout()
44 support.record_original_stdout(sys.stdout)
45
Victor Stinner9759dd32016-03-30 02:32:52 +020046 if ns.testdir:
47 # Prepend test directory to sys.path, so runtest() will be able
48 # to locate tests
49 sys.path.insert(0, os.path.abspath(ns.testdir))
50
Victor Stinner234cbef2015-09-30 01:13:53 +020051 # Some times __path__ and __file__ are not absolute (e.g. while running from
52 # Lib/) and, if we change the CWD to run the tests in a temporary dir, some
53 # imports might fail. This affects only the modules imported before os.chdir().
54 # These modules are searched first in sys.path[0] (so '' -- the CWD) and if
55 # they are found in the CWD their __file__ and __path__ will be relative (this
56 # happens before the chdir). All the modules imported after the chdir, are
57 # not found in the CWD, and since the other paths in sys.path[1:] are absolute
58 # (site.py absolutize them), the __file__ and __path__ will be absolute too.
59 # Therefore it is necessary to absolutize manually the __file__ and __path__ of
60 # the packages to prevent later imports to fail when the CWD is different.
61 for module in sys.modules.values():
62 if hasattr(module, '__path__'):
Victor Stinner82f04e22016-03-15 23:08:44 +010063 for index, path in enumerate(module.__path__):
64 module.__path__[index] = os.path.abspath(path)
Ned Deilye52ac042018-03-28 01:57:13 -040065 if getattr(module, '__file__', None):
Victor Stinner234cbef2015-09-30 01:13:53 +020066 module.__file__ = os.path.abspath(module.__file__)
67
Victor Stinner234cbef2015-09-30 01:13:53 +020068 if ns.huntrleaks:
69 unittest.BaseTestSuite._cleanup = False
Pablo Galindoaf5fa132021-02-28 22:41:09 +000070 sys._deactivate_opcache()
Victor Stinner234cbef2015-09-30 01:13:53 +020071
Victor Stinner234cbef2015-09-30 01:13:53 +020072 if ns.memlimit is not None:
73 support.set_memlimit(ns.memlimit)
74
75 if ns.threshold is not None:
76 gc.set_threshold(ns.threshold)
77
Victor Stinnerf6e58ae2020-06-10 18:49:23 +020078 support.suppress_msvcrt_asserts(ns.verbose and ns.verbose >= 2)
Victor Stinner234cbef2015-09-30 01:13:53 +020079
Victor Stinnera2045022015-09-30 02:17:28 +020080 support.use_resources = ns.use_resources
81
Steve Dowerb82e17e2019-05-23 08:45:22 -070082 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
Victor Stinner95f61c82019-06-13 01:09:04 +020088 setup_unraisable_hook()
Victor Stinnerb136b1a2021-04-16 14:33:10 +020089 setup_threading_excepthook()
Victor Stinner95f61c82019-06-13 01:09:04 +020090
Victor Stinner24c62582019-10-30 12:41:43 +010091 if ns.timeout is not None:
92 # For a slow buildbot worker, increase SHORT_TIMEOUT and LONG_TIMEOUT
93 support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, ns.timeout / 40)
94 support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, ns.timeout / 4)
95
96 # If --timeout is short: reduce timeouts
97 support.LOOPBACK_TIMEOUT = min(support.LOOPBACK_TIMEOUT, ns.timeout)
98 support.INTERNET_TIMEOUT = min(support.INTERNET_TIMEOUT, ns.timeout)
99 support.SHORT_TIMEOUT = min(support.SHORT_TIMEOUT, ns.timeout)
100 support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, ns.timeout)
101
Victor Stinner30793e82021-03-23 01:11:31 +0100102 if ns.xmlpath:
103 from test.support.testresult import RegressionTestResult
104 RegressionTestResult.USE_XML = True
105
Miss Islington (bot)93d36a52021-07-16 06:55:26 -0700106 # Ensure there's a non-ASCII character in env vars at all times to force
107 # tests consider this case. See BPO-44647 for details.
108 os.environ.setdefault(
109 UNICODE_GUARD_ENV,
110 "\N{SMILING FACE WITH SUNGLASSES}",
111 )
112
Victor Stinner234cbef2015-09-30 01:13:53 +0200113
114def replace_stdout():
115 """Set stdout encoder error handler to backslashreplace (as stderr error
116 handler) to avoid UnicodeEncodeError when printing a traceback"""
117 stdout = sys.stdout
Victor Stinnerccef8232017-10-13 12:59:12 -0700118 try:
119 fd = stdout.fileno()
120 except ValueError:
121 # On IDLE, sys.stdout has no file descriptor and is not a TextIOWrapper
122 # object. Leaving sys.stdout unchanged.
123 #
124 # Catch ValueError to catch io.UnsupportedOperation on TextIOBase
125 # and ValueError on a closed stream.
126 return
127
128 sys.stdout = open(fd, 'w',
Victor Stinner234cbef2015-09-30 01:13:53 +0200129 encoding=stdout.encoding,
130 errors="backslashreplace",
131 closefd=False,
132 newline='\n')
133
134 def restore_stdout():
135 sys.stdout.close()
136 sys.stdout = stdout
137 atexit.register(restore_stdout)
Miss Islington (bot)8f6aa482021-10-21 21:47:07 -0700138
139
140def _adjust_resource_limits():
141 """Adjust the system resource limits (ulimit) if needed."""
142 try:
143 import resource
144 from resource import RLIMIT_NOFILE, RLIM_INFINITY
145 except ImportError:
146 return
147 fd_limit, max_fds = resource.getrlimit(RLIMIT_NOFILE)
148 # On macOS the default fd limit is sometimes too low (256) for our
149 # test suite to succeed. Raise it to something more reasonable.
150 # 1024 is a common Linux default.
151 desired_fds = 1024
152 if fd_limit < desired_fds and fd_limit < max_fds:
153 new_fd_limit = min(desired_fds, max_fds)
154 try:
155 resource.setrlimit(RLIMIT_NOFILE, (new_fd_limit, max_fds))
156 print(f"Raised RLIMIT_NOFILE: {fd_limit} -> {new_fd_limit}")
157 except (ValueError, OSError) as err:
158 print(f"Unable to raise RLIMIT_NOFILE from {fd_limit} to "
159 f"{new_fd_limit}: {err}.")
160