()
| 28 | |
| 29 | |
| 30 | def setup_process() -> None: |
| 31 | assert sys.__stderr__ is not None, "sys.__stderr__ is None" |
| 32 | try: |
| 33 | stderr_fd = sys.__stderr__.fileno() |
| 34 | except (ValueError, AttributeError): |
| 35 | # Catch ValueError to catch io.UnsupportedOperation on TextIOBase |
| 36 | # and ValueError on a closed stream. |
| 37 | # |
| 38 | # Catch AttributeError for stderr being None. |
| 39 | pass |
| 40 | else: |
| 41 | # Display the Python traceback on fatal errors (e.g. segfault) |
| 42 | faulthandler.enable(all_threads=True, file=stderr_fd) |
| 43 | |
| 44 | # Display the Python traceback on SIGALRM or SIGUSR1 signal |
| 45 | signals: list[signal.Signals] = [] |
| 46 | if hasattr(signal, 'SIGALRM'): |
| 47 | signals.append(signal.SIGALRM) |
| 48 | if hasattr(signal, 'SIGUSR1'): |
| 49 | signals.append(signal.SIGUSR1) |
| 50 | for signum in signals: |
| 51 | faulthandler.register(signum, chain=True, file=stderr_fd) |
| 52 | |
| 53 | adjust_rlimit_nofile() |
| 54 | |
| 55 | support.record_original_stdout(sys.stdout) |
| 56 | |
| 57 | # Set sys.stdout encoder error handler to backslashreplace, |
| 58 | # similar to sys.stderr error handler, to avoid UnicodeEncodeError |
| 59 | # when printing a traceback or any other non-encodable character. |
| 60 | # |
| 61 | # Use an assertion to fix mypy error. |
| 62 | assert isinstance(sys.stdout, io.TextIOWrapper) |
| 63 | sys.stdout.reconfigure(errors="backslashreplace") |
| 64 | |
| 65 | # Some times __path__ and __file__ are not absolute (e.g. while running from |
| 66 | # Lib/) and, if we change the CWD to run the tests in a temporary dir, some |
| 67 | # imports might fail. This affects only the modules imported before os.chdir(). |
| 68 | # These modules are searched first in sys.path[0] (so '' -- the CWD) and if |
| 69 | # they are found in the CWD their __file__ and __path__ will be relative (this |
| 70 | # happens before the chdir). All the modules imported after the chdir, are |
| 71 | # not found in the CWD, and since the other paths in sys.path[1:] are absolute |
| 72 | # (site.py absolutize them), the __file__ and __path__ will be absolute too. |
| 73 | # Therefore it is necessary to absolutize manually the __file__ and __path__ of |
| 74 | # the packages to prevent later imports to fail when the CWD is different. |
| 75 | for module in sys.modules.values(): |
| 76 | if hasattr(module, '__path__'): |
| 77 | for index, path in enumerate(module.__path__): |
| 78 | module.__path__[index] = os.path.abspath(path) |
| 79 | if getattr(module, '__file__', None): |
| 80 | module.__file__ = os.path.abspath(module.__file__) # type: ignore[type-var] |
| 81 | |
| 82 | if hasattr(sys, 'addaudithook'): |
| 83 | # Add an auditing hook for all tests to ensure PySys_Audit is tested |
| 84 | def _test_audit_hook(name, args): |
| 85 | pass |
| 86 | sys.addaudithook(_test_audit_hook) |
| 87 |
no test coverage detected
searching dependent graphs…