Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 1 | import atexit |
| 2 | import faulthandler |
| 3 | import os |
| 4 | import signal |
| 5 | import sys |
| 6 | import unittest |
| 7 | from test import support |
| 8 | try: |
| 9 | import gc |
| 10 | except ImportError: |
| 11 | gc = None |
| 12 | |
Victor Stinner | 95f61c8 | 2019-06-13 01:09:04 +0200 | [diff] [blame] | 13 | from test.libregrtest.utils import setup_unraisable_hook |
| 14 | |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 15 | |
Victor Stinner | a204502 | 2015-09-30 02:17:28 +0200 | [diff] [blame] | 16 | def setup_tests(ns): |
Victor Stinner | ccef823 | 2017-10-13 12:59:12 -0700 | [diff] [blame] | 17 | try: |
| 18 | stderr_fd = sys.__stderr__.fileno() |
| 19 | except (ValueError, AttributeError): |
| 20 | # Catch ValueError to catch io.UnsupportedOperation on TextIOBase |
| 21 | # and ValueError on a closed stream. |
| 22 | # |
| 23 | # Catch AttributeError for stderr being None. |
| 24 | stderr_fd = None |
| 25 | else: |
| 26 | # Display the Python traceback on fatal errors (e.g. segfault) |
| 27 | faulthandler.enable(all_threads=True, file=stderr_fd) |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 28 | |
Victor Stinner | ccef823 | 2017-10-13 12:59:12 -0700 | [diff] [blame] | 29 | # Display the Python traceback on SIGALRM or SIGUSR1 signal |
| 30 | signals = [] |
| 31 | if hasattr(signal, 'SIGALRM'): |
| 32 | signals.append(signal.SIGALRM) |
| 33 | if hasattr(signal, 'SIGUSR1'): |
| 34 | signals.append(signal.SIGUSR1) |
| 35 | for signum in signals: |
| 36 | faulthandler.register(signum, chain=True, file=stderr_fd) |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 37 | |
| 38 | replace_stdout() |
| 39 | support.record_original_stdout(sys.stdout) |
| 40 | |
Victor Stinner | 9759dd3 | 2016-03-30 02:32:52 +0200 | [diff] [blame] | 41 | if ns.testdir: |
| 42 | # Prepend test directory to sys.path, so runtest() will be able |
| 43 | # to locate tests |
| 44 | sys.path.insert(0, os.path.abspath(ns.testdir)) |
| 45 | |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 46 | # Some times __path__ and __file__ are not absolute (e.g. while running from |
| 47 | # Lib/) and, if we change the CWD to run the tests in a temporary dir, some |
| 48 | # imports might fail. This affects only the modules imported before os.chdir(). |
| 49 | # These modules are searched first in sys.path[0] (so '' -- the CWD) and if |
| 50 | # they are found in the CWD their __file__ and __path__ will be relative (this |
| 51 | # happens before the chdir). All the modules imported after the chdir, are |
| 52 | # not found in the CWD, and since the other paths in sys.path[1:] are absolute |
| 53 | # (site.py absolutize them), the __file__ and __path__ will be absolute too. |
| 54 | # Therefore it is necessary to absolutize manually the __file__ and __path__ of |
| 55 | # the packages to prevent later imports to fail when the CWD is different. |
| 56 | for module in sys.modules.values(): |
| 57 | if hasattr(module, '__path__'): |
Victor Stinner | 82f04e2 | 2016-03-15 23:08:44 +0100 | [diff] [blame] | 58 | for index, path in enumerate(module.__path__): |
| 59 | module.__path__[index] = os.path.abspath(path) |
Ned Deily | e52ac04 | 2018-03-28 01:57:13 -0400 | [diff] [blame] | 60 | if getattr(module, '__file__', None): |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 61 | module.__file__ = os.path.abspath(module.__file__) |
| 62 | |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 63 | if ns.huntrleaks: |
| 64 | unittest.BaseTestSuite._cleanup = False |
| 65 | |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 66 | if ns.memlimit is not None: |
| 67 | support.set_memlimit(ns.memlimit) |
| 68 | |
| 69 | if ns.threshold is not None: |
| 70 | gc.set_threshold(ns.threshold) |
| 71 | |
Victor Stinner | f6e58ae | 2020-06-10 18:49:23 +0200 | [diff] [blame^] | 72 | support.suppress_msvcrt_asserts(ns.verbose and ns.verbose >= 2) |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 73 | |
Victor Stinner | a204502 | 2015-09-30 02:17:28 +0200 | [diff] [blame] | 74 | support.use_resources = ns.use_resources |
| 75 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 76 | if hasattr(sys, 'addaudithook'): |
| 77 | # Add an auditing hook for all tests to ensure PySys_Audit is tested |
| 78 | def _test_audit_hook(name, args): |
| 79 | pass |
| 80 | sys.addaudithook(_test_audit_hook) |
| 81 | |
Victor Stinner | 95f61c8 | 2019-06-13 01:09:04 +0200 | [diff] [blame] | 82 | setup_unraisable_hook() |
| 83 | |
Victor Stinner | 24c6258 | 2019-10-30 12:41:43 +0100 | [diff] [blame] | 84 | if ns.timeout is not None: |
| 85 | # For a slow buildbot worker, increase SHORT_TIMEOUT and LONG_TIMEOUT |
| 86 | support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, ns.timeout / 40) |
| 87 | support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, ns.timeout / 4) |
| 88 | |
| 89 | # If --timeout is short: reduce timeouts |
| 90 | support.LOOPBACK_TIMEOUT = min(support.LOOPBACK_TIMEOUT, ns.timeout) |
| 91 | support.INTERNET_TIMEOUT = min(support.INTERNET_TIMEOUT, ns.timeout) |
| 92 | support.SHORT_TIMEOUT = min(support.SHORT_TIMEOUT, ns.timeout) |
| 93 | support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, ns.timeout) |
| 94 | |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 95 | |
| 96 | def replace_stdout(): |
| 97 | """Set stdout encoder error handler to backslashreplace (as stderr error |
| 98 | handler) to avoid UnicodeEncodeError when printing a traceback""" |
| 99 | stdout = sys.stdout |
Victor Stinner | ccef823 | 2017-10-13 12:59:12 -0700 | [diff] [blame] | 100 | try: |
| 101 | fd = stdout.fileno() |
| 102 | except ValueError: |
| 103 | # On IDLE, sys.stdout has no file descriptor and is not a TextIOWrapper |
| 104 | # object. Leaving sys.stdout unchanged. |
| 105 | # |
| 106 | # Catch ValueError to catch io.UnsupportedOperation on TextIOBase |
| 107 | # and ValueError on a closed stream. |
| 108 | return |
| 109 | |
| 110 | sys.stdout = open(fd, 'w', |
Victor Stinner | 234cbef | 2015-09-30 01:13:53 +0200 | [diff] [blame] | 111 | encoding=stdout.encoding, |
| 112 | errors="backslashreplace", |
| 113 | closefd=False, |
| 114 | newline='\n') |
| 115 | |
| 116 | def restore_stdout(): |
| 117 | sys.stdout.close() |
| 118 | sys.stdout = stdout |
| 119 | atexit.register(restore_stdout) |