ยปCore Development>Code coverage>Lib/test/libregrtest/setup.py

Python code coverage for Lib/test/libregrtest/setup.py

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