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 13 14def setup_tests(ns): 15 try: 16 stderr_fd = sys.__stderr__.fileno() 17 except (ValueError, AttributeError): 18 # Catch ValueError to catch io.UnsupportedOperation on TextIOBase 19 # and ValueError on a closed stream. 20 # 21 # Catch AttributeError for stderr being None. 22 stderr_fd = None 23 else: 24 # Display the Python traceback on fatal errors (e.g. segfault) 25 faulthandler.enable(all_threads=True, file=stderr_fd) 26 27 # Display the Python traceback on SIGALRM or SIGUSR1 signal 28 signals = [] 29 if hasattr(signal, 'SIGALRM'): 30 signals.append(signal.SIGALRM) 31 if hasattr(signal, 'SIGUSR1'): 32 signals.append(signal.SIGUSR1) 33 for signum in signals: 34 faulthandler.register(signum, chain=True, file=stderr_fd) 35 36 replace_stdout() 37 support.record_original_stdout(sys.stdout) 38 39 if ns.testdir: 40 # Prepend test directory to sys.path, so runtest() will be able 41 # to locate tests 42 sys.path.insert(0, os.path.abspath(ns.testdir)) 43 44 # Some times __path__ and __file__ are not absolute (e.g. while running from 45 # Lib/) and, if we change the CWD to run the tests in a temporary dir, some 46 # imports might fail. This affects only the modules imported before os.chdir(). 47 # These modules are searched first in sys.path[0] (so '' -- the CWD) and if 48 # they are found in the CWD their __file__ and __path__ will be relative (this 49 # happens before the chdir). All the modules imported after the chdir, are 50 # not found in the CWD, and since the other paths in sys.path[1:] are absolute 51 # (site.py absolutize them), the __file__ and __path__ will be absolute too. 52 # Therefore it is necessary to absolutize manually the __file__ and __path__ of 53 # the packages to prevent later imports to fail when the CWD is different. 54 for module in sys.modules.values(): 55 if hasattr(module, '__path__'): 56 for index, path in enumerate(module.__path__): 57 module.__path__[index] = os.path.abspath(path) 58 if getattr(module, '__file__', None): 59 module.__file__ = os.path.abspath(module.__file__) 60 61 if ns.huntrleaks: 62 unittest.BaseTestSuite._cleanup = False 63 64 if ns.memlimit is not None: 65 support.set_memlimit(ns.memlimit) 66 67 if ns.threshold is not None: 68 gc.set_threshold(ns.threshold) 69 70 suppress_msvcrt_asserts(ns.verbose and ns.verbose >= 2) 71 72 support.use_resources = ns.use_resources 73 74 if hasattr(sys, 'addaudithook'): 75 # Add an auditing hook for all tests to ensure PySys_Audit is tested 76 def _test_audit_hook(name, args): 77 pass 78 sys.addaudithook(_test_audit_hook) 79 80 81def suppress_msvcrt_asserts(verbose): 82 try: 83 import msvcrt 84 except ImportError: 85 return 86 87 msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS| 88 msvcrt.SEM_NOALIGNMENTFAULTEXCEPT| 89 msvcrt.SEM_NOGPFAULTERRORBOX| 90 msvcrt.SEM_NOOPENFILEERRORBOX) 91 try: 92 msvcrt.CrtSetReportMode 93 except AttributeError: 94 # release build 95 return 96 97 for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]: 98 if verbose: 99 msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE) 100 msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR) 101 else: 102 msvcrt.CrtSetReportMode(m, 0) 103 104 105 106def replace_stdout(): 107 """Set stdout encoder error handler to backslashreplace (as stderr error 108 handler) to avoid UnicodeEncodeError when printing a traceback""" 109 stdout = sys.stdout 110 try: 111 fd = stdout.fileno() 112 except ValueError: 113 # On IDLE, sys.stdout has no file descriptor and is not a TextIOWrapper 114 # object. Leaving sys.stdout unchanged. 115 # 116 # Catch ValueError to catch io.UnsupportedOperation on TextIOBase 117 # and ValueError on a closed stream. 118 return 119 120 sys.stdout = open(fd, 'w', 121 encoding=stdout.encoding, 122 errors="backslashreplace", 123 closefd=False, 124 newline='\n') 125 126 def restore_stdout(): 127 sys.stdout.close() 128 sys.stdout = stdout 129 atexit.register(restore_stdout) 130