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