Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/src/_pytest/faulthandler.py

raw
1 import os
2 import sys
3 from typing import Generator
4
5 from _pytest.config import Config
6 from _pytest.config.argparsing import Parser
7 from _pytest.nodes import Item
8 from _pytest.stash import StashKey
9 import pytest
10
11
12 fault_handler_original_stderr_fd_key = StashKey[int]()
13 fault_handler_stderr_fd_key = StashKey[int]()
14
15
16 def pytest_addoption(parser: Parser) -> None:
17 help = (
18 "Dump the traceback of all threads if a test takes "
19 "more than TIMEOUT seconds to finish"
20 )
21 parser.addini("faulthandler_timeout", help, default=0.0)
22
23
24 def pytest_configure(config: Config) -> None:
25 import faulthandler
26
27 # at teardown we want to restore the original faulthandler fileno
28 # but faulthandler has no api to return the original fileno
29 # so here we stash the stderr fileno to be used at teardown
30 # sys.stderr and sys.__stderr__ may be closed or patched during the session
31 # so we can't rely on their values being good at that point (#11572).
32 stderr_fileno = get_stderr_fileno()
33 if faulthandler.is_enabled():
34 config.stash[fault_handler_original_stderr_fd_key] = stderr_fileno
35 config.stash[fault_handler_stderr_fd_key] = os.dup(stderr_fileno)
36 faulthandler.enable(file=config.stash[fault_handler_stderr_fd_key])
37
38
39 def pytest_unconfigure(config: Config) -> None:
40 import faulthandler
41
42 faulthandler.disable()
43 # Close the dup file installed during pytest_configure.
44 if fault_handler_stderr_fd_key in config.stash:
45 os.close(config.stash[fault_handler_stderr_fd_key])
46 del config.stash[fault_handler_stderr_fd_key]
47 # Re-enable the faulthandler if it was originally enabled.
48 if fault_handler_original_stderr_fd_key in config.stash:
49 faulthandler.enable(config.stash[fault_handler_original_stderr_fd_key])
50 del config.stash[fault_handler_original_stderr_fd_key]
51
52
53 def get_stderr_fileno() -> int:
54 try:
55 fileno = sys.stderr.fileno()
56 # The Twisted Logger will return an invalid file descriptor since it is not backed
57 # by an FD. So, let's also forward this to the same code path as with pytest-xdist.
58 if fileno == -1:
59 raise AttributeError()
60 return fileno
61 except (AttributeError, ValueError):
62 # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
63 # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
64 # This is potentially dangerous, but the best we can do.
65 return sys.__stderr__.fileno()
66
67
68 def get_timeout_config_value(config: Config) -> float:
69 return float(config.getini("faulthandler_timeout") or 0.0)
70
71
72 @pytest.hookimpl(wrapper=True, trylast=True)
73 def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
74 timeout = get_timeout_config_value(item.config)
75 if timeout > 0:
76 import faulthandler
77
78 stderr = item.config.stash[fault_handler_stderr_fd_key]
79 faulthandler.dump_traceback_later(timeout, file=stderr)
80 try:
81 return (yield)
82 finally:
83 faulthandler.cancel_dump_traceback_later()
84 else:
85 return (yield)
86
87
88 @pytest.hookimpl(tryfirst=True)
89 def pytest_enter_pdb() -> None:
90 """Cancel any traceback dumping due to timeout before entering pdb."""
91 import faulthandler
92
93 faulthandler.cancel_dump_traceback_later()
94
95
96 @pytest.hookimpl(tryfirst=True)
97 def pytest_exception_interact() -> None:
98 """Cancel any traceback dumping due to an interactive exception being
99 raised."""
100 import faulthandler
101
102 faulthandler.cancel_dump_traceback_later()