blob: de52bb5e7811bdc3445df7addd17098151274fb7 [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
13from test.libregrtest.refleak import warm_caches
14
15
Victor Stinnera2045022015-09-30 02:17:28 +020016def setup_tests(ns):
Victor Stinner234cbef2015-09-30 01:13:53 +020017 # 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 # Some times __path__ and __file__ are not absolute (e.g. while running from
33 # Lib/) and, if we change the CWD to run the tests in a temporary dir, some
34 # imports might fail. This affects only the modules imported before os.chdir().
35 # These modules are searched first in sys.path[0] (so '' -- the CWD) and if
36 # they are found in the CWD their __file__ and __path__ will be relative (this
37 # happens before the chdir). All the modules imported after the chdir, are
38 # not found in the CWD, and since the other paths in sys.path[1:] are absolute
39 # (site.py absolutize them), the __file__ and __path__ will be absolute too.
40 # Therefore it is necessary to absolutize manually the __file__ and __path__ of
41 # the packages to prevent later imports to fail when the CWD is different.
42 for module in sys.modules.values():
43 if hasattr(module, '__path__'):
Victor Stinner82f04e22016-03-15 23:08:44 +010044 for index, path in enumerate(module.__path__):
45 module.__path__[index] = os.path.abspath(path)
Victor Stinner234cbef2015-09-30 01:13:53 +020046 if hasattr(module, '__file__'):
47 module.__file__ = os.path.abspath(module.__file__)
48
49 # MacOSX (a.k.a. Darwin) has a default stack size that is too small
50 # for deeply recursive regular expressions. We see this as crashes in
51 # the Python test suite when running test_re.py and test_sre.py. The
52 # fix is to set the stack limit to 2048.
53 # This approach may also be useful for other Unixy platforms that
54 # suffer from small default stack limits.
55 if sys.platform == 'darwin':
56 try:
57 import resource
58 except ImportError:
59 pass
60 else:
61 soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
62 newsoft = min(hard, max(soft, 1024*2048))
63 resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
64
65 if ns.huntrleaks:
66 unittest.BaseTestSuite._cleanup = False
67
68 # Avoid false positives due to various caches
69 # filling slowly with random data:
70 warm_caches()
71
72 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
Steve Dower12c29452015-10-08 09:05:36 -070078 try:
Victor Stinner234cbef2015-09-30 01:13:53 +020079 import msvcrt
Steve Dower12c29452015-10-08 09:05:36 -070080 except ImportError:
81 pass
82 else:
Victor Stinner234cbef2015-09-30 01:13:53 +020083 msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS|
84 msvcrt.SEM_NOALIGNMENTFAULTEXCEPT|
85 msvcrt.SEM_NOGPFAULTERRORBOX|
86 msvcrt.SEM_NOOPENFILEERRORBOX)
87 try:
88 msvcrt.CrtSetReportMode
89 except AttributeError:
90 # release build
91 pass
92 else:
93 for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
Steve Dower12c29452015-10-08 09:05:36 -070094 if ns.verbose and ns.verbose >= 2:
95 msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE)
96 msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR)
97 else:
98 msvcrt.CrtSetReportMode(m, 0)
Victor Stinner234cbef2015-09-30 01:13:53 +020099
Victor Stinnera2045022015-09-30 02:17:28 +0200100 support.use_resources = ns.use_resources
101
Victor Stinner234cbef2015-09-30 01:13:53 +0200102
103def replace_stdout():
104 """Set stdout encoder error handler to backslashreplace (as stderr error
105 handler) to avoid UnicodeEncodeError when printing a traceback"""
106 stdout = sys.stdout
107 sys.stdout = open(stdout.fileno(), 'w',
108 encoding=stdout.encoding,
109 errors="backslashreplace",
110 closefd=False,
111 newline='\n')
112
113 def restore_stdout():
114 sys.stdout.close()
115 sys.stdout = stdout
116 atexit.register(restore_stdout)