| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  | import faulthandler | 
					
						
							| 
									
										
										
										
											2023-09-14 19:33:18 +01:00
										 |  |  | import gc | 
					
						
							| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2023-09-12 05:35:08 +02:00
										 |  |  | import random | 
					
						
							| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  | import signal | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2022-04-25 17:35:14 +03:00
										 |  |  | from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII | 
					
						
							| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 09:02:35 +02:00
										 |  |  | from .runtests import RunTests | 
					
						
							|  |  |  | from .utils import ( | 
					
						
							| 
									
										
										
										
											2023-09-11 05:27:37 +02:00
										 |  |  |     setup_unraisable_hook, setup_threading_excepthook, fix_umask, | 
					
						
							| 
									
										
										
										
											2023-09-26 21:34:50 +02:00
										 |  |  |     adjust_rlimit_nofile) | 
					
						
							| 
									
										
										
										
											2019-06-13 01:09:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-16 15:24:02 +02:00
										 |  |  | UNICODE_GUARD_ENV = "PYTHONREGRTEST_UNICODE_GUARD" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-09 11:18:14 +02:00
										 |  |  | def setup_test_dir(testdir: str | None) -> None: | 
					
						
							| 
									
										
										
										
											2023-09-09 03:37:48 +02: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 05:27:37 +02:00
										 |  |  | def setup_process(): | 
					
						
							| 
									
										
										
										
											2023-09-11 01:11:22 +02:00
										 |  |  |     fix_umask() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-13 12:59:12 -07: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-30 01:13:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 05:27:37 +02:00
										 |  |  |     adjust_rlimit_nofile() | 
					
						
							| 
									
										
										
										
											2023-09-26 21:34:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  |     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 23:08:44 +01:00
										 |  |  |             for index, path in enumerate(module.__path__): | 
					
						
							|  |  |  |                 module.__path__[index] = os.path.abspath(path) | 
					
						
							| 
									
										
										
										
											2018-03-28 01:57:13 -04:00
										 |  |  |         if getattr(module, '__file__', None): | 
					
						
							| 
									
										
										
										
											2015-09-30 01:13:53 +02:00
										 |  |  |             module.__file__ = os.path.abspath(module.__file__) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07: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-13 01:09:04 +02:00
										 |  |  |     setup_unraisable_hook() | 
					
						
							| 
									
										
										
										
											2021-04-16 14:33:10 +02:00
										 |  |  |     setup_threading_excepthook() | 
					
						
							| 
									
										
										
										
											2019-06-13 01:09:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 05:27:37 +02: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-09 03:37:48 +02:00
										 |  |  |     timeout = runtests.timeout | 
					
						
							|  |  |  |     if timeout is not None: | 
					
						
							| 
									
										
										
										
											2019-10-30 12:41:43 +01:00
										 |  |  |         # For a slow buildbot worker, increase SHORT_TIMEOUT and LONG_TIMEOUT | 
					
						
							| 
									
										
										
										
											2023-09-09 03:37:48 +02:00
										 |  |  |         support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, timeout / 40) | 
					
						
							|  |  |  |         support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, timeout / 4) | 
					
						
							| 
									
										
										
										
											2019-10-30 12:41:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # If --timeout is short: reduce timeouts | 
					
						
							| 
									
										
										
										
											2023-09-09 03:37:48 +02: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 12:41:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 05:27:37 +02:00
										 |  |  |     if runtests.hunt_refleak: | 
					
						
							|  |  |  |         unittest.BaseTestSuite._cleanup = False | 
					
						
							| 
									
										
										
										
											2021-10-21 18:22:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 05:27:37 +02:00
										 |  |  |     if runtests.gc_threshold is not None: | 
					
						
							|  |  |  |         gc.set_threshold(runtests.gc_threshold) | 
					
						
							| 
									
										
										
										
											2023-09-12 05:35:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 09:42:12 +03:00
										 |  |  |     random.seed(runtests.random_seed) |