1import 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 13from test.libregrtest.refleak import warm_caches 14 15 16def setup_tests(ns): 17 # Display the Python traceback on fatal errors (e.g. segfault) 18 faulthandler.enable(all_threads=True) 19 20 # Display the Python traceback on SIGALRM or SIGUSR1 signal 21 signals = [] 22 if hasattr(signal, 'SIGALRM'): 23 signals.append(signal.SIGALRM) 24 if hasattr(signal, 'SIGUSR1'): 25 signals.append(signal.SIGUSR1) 26 for signum in signals: 27 faulthandler.register(signum, chain=True) 28 29 replace_stdout() 30 support.record_original_stdout(sys.stdout) 31 32 if ns.testdir: 33 # Prepend test directory to sys.path, so runtest() will be able 34 # to locate tests 35 sys.path.insert(0, os.path.abspath(ns.testdir)) 36 37 # Some times __path__ and __file__ are not absolute (e.g. while running from 38 # Lib/) and, if we change the CWD to run the tests in a temporary dir, some 39 # imports might fail. This affects only the modules imported before os.chdir(). 40 # These modules are searched first in sys.path[0] (so '' -- the CWD) and if 41 # they are found in the CWD their __file__ and __path__ will be relative (this 42 # happens before the chdir). All the modules imported after the chdir, are 43 # not found in the CWD, and since the other paths in sys.path[1:] are absolute 44 # (site.py absolutize them), the __file__ and __path__ will be absolute too. 45 # Therefore it is necessary to absolutize manually the __file__ and __path__ of 46 # the packages to prevent later imports to fail when the CWD is different. 47 for module in sys.modules.values(): 48 if hasattr(module, '__path__'): 49 for index, path in enumerate(module.__path__): 50 module.__path__[index] = os.path.abspath(path) 51 if hasattr(module, '__file__'): 52 module.__file__ = os.path.abspath(module.__file__) 53 54 # MacOSX (a.k.a. Darwin) has a default stack size that is too small 55 # for deeply recursive regular expressions. We see this as crashes in 56 # the Python test suite when running test_re.py and test_sre.py. The 57 # fix is to set the stack limit to 2048. 58 # This approach may also be useful for other Unixy platforms that 59 # suffer from small default stack limits. 60 if sys.platform == 'darwin': 61 try: 62 import resource 63 except ImportError: 64 pass 65 else: 66 soft, hard = resource.getrlimit(resource.RLIMIT_STACK) 67 newsoft = min(hard, max(soft, 1024*2048)) 68 resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard)) 69 70 if ns.huntrleaks: 71 unittest.BaseTestSuite._cleanup = False 72 73 # Avoid false positives due to various caches 74 # filling slowly with random data: 75 warm_caches() 76 77 if ns.memlimit is not None: 78 support.set_memlimit(ns.memlimit) 79 80 if ns.threshold is not None: 81 gc.set_threshold(ns.threshold) 82 83 try: 84 import msvcrt 85 except ImportError: 86 pass 87 else: 88 msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS| 89 msvcrt.SEM_NOALIGNMENTFAULTEXCEPT| 90 msvcrt.SEM_NOGPFAULTERRORBOX| 91 msvcrt.SEM_NOOPENFILEERRORBOX) 92 try: 93 msvcrt.CrtSetReportMode 94 except AttributeError: 95 # release build 96 pass 97 else: 98 for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]: 99 if ns.verbose and ns.verbose >= 2: 100 msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE) 101 msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR) 102 else: 103 msvcrt.CrtSetReportMode(m, 0) 104 105 support.use_resources = ns.use_resources 106 107 108def replace_stdout(): 109 """Set stdout encoder error handler to backslashreplace (as stderr error 110 handler) to avoid UnicodeEncodeError when printing a traceback""" 111 stdout = sys.stdout 112 sys.stdout = open(stdout.fileno(), 'w', 113 encoding=stdout.encoding, 114 errors="backslashreplace", 115 closefd=False, 116 newline='\n') 117 118 def restore_stdout(): 119 sys.stdout.close() 120 sys.stdout = stdout 121 atexit.register(restore_stdout) 122