blob: 1d24531fc26ec544e5f15e3e5cb89e94544b1009 [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
Victor Stinner9759dd32016-03-30 02:32:52 +020032 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
Victor Stinner234cbef2015-09-30 01:13:53 +020037 # 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__'):
Victor Stinner82f04e22016-03-15 23:08:44 +010049 for index, path in enumerate(module.__path__):
50 module.__path__[index] = os.path.abspath(path)
Victor Stinner234cbef2015-09-30 01:13:53 +020051 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
Steve Dower12c29452015-10-08 09:05:36 -070083 try:
Victor Stinner234cbef2015-09-30 01:13:53 +020084 import msvcrt
Steve Dower12c29452015-10-08 09:05:36 -070085 except ImportError:
86 pass
87 else:
Victor Stinner234cbef2015-09-30 01:13:53 +020088 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]:
Steve Dower12c29452015-10-08 09:05:36 -070099 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)
Victor Stinner234cbef2015-09-30 01:13:53 +0200104
Victor Stinnera2045022015-09-30 02:17:28 +0200105 support.use_resources = ns.use_resources
106
Victor Stinner234cbef2015-09-30 01:13:53 +0200107
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)