MCPcopy Index your code
hub / github.com/python/cpython / setup_process

Function setup_process

Lib/test/libregrtest/setup.py:30–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28
29
30def 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

Callers 2

_initMethod · 0.85
worker_processFunction · 0.85

Calls 13

adjust_rlimit_nofileFunction · 0.85
enumerateFunction · 0.85
setup_unraisable_hookFunction · 0.85
filenoMethod · 0.45
enableMethod · 0.45
appendMethod · 0.45
registerMethod · 0.45
reconfigureMethod · 0.45
valuesMethod · 0.45
abspathMethod · 0.45
setdefaultMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…