Debian Codegraph @

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

raw
1 import sys
2 import traceback
3 from types import TracebackType
4 from typing import Any
5 from typing import Callable
6 from typing import Generator
7 from typing import Optional
8 from typing import Type
9 import warnings
10
11 import pytest
12
13
14 # Copied from cpython/Lib/test/support/__init__.py, with modifications.
15 class catch_unraisable_exception:
16 """Context manager catching unraisable exception using sys.unraisablehook.
17
18 Storing the exception value (cm.unraisable.exc_value) creates a reference
19 cycle. The reference cycle is broken explicitly when the context manager
20 exits.
21
22 Storing the object (cm.unraisable.object) can resurrect it if it is set to
23 an object which is being finalized. Exiting the context manager clears the
24 stored object.
25
26 Usage:
27 with catch_unraisable_exception() as cm:
28 # code creating an "unraisable exception"
29 ...
30 # check the unraisable exception: use cm.unraisable
31 ...
32 # cm.unraisable attribute no longer exists at this point
33 # (to break a reference cycle)
34 """
35
36 def __init__(self) -> None:
37 self.unraisable: Optional["sys.UnraisableHookArgs"] = None
38 self._old_hook: Optional[Callable[["sys.UnraisableHookArgs"], Any]] = None
39
40 def _hook(self, unraisable: "sys.UnraisableHookArgs") -> None:
41 # Storing unraisable.object can resurrect an object which is being
42 # finalized. Storing unraisable.exc_value creates a reference cycle.
43 self.unraisable = unraisable
44
45 def __enter__(self) -> "catch_unraisable_exception":
46 self._old_hook = sys.unraisablehook
47 sys.unraisablehook = self._hook
48 return self
49
50 def __exit__(
51 self,
52 exc_type: Optional[Type[BaseException]],
53 exc_val: Optional[BaseException],
54 exc_tb: Optional[TracebackType],
55 ) -> None:
56 assert self._old_hook is not None
57 sys.unraisablehook = self._old_hook
58 self._old_hook = None
59 del self.unraisable
60
61
62 def unraisable_exception_runtest_hook() -> Generator[None, None, None]:
63 with catch_unraisable_exception() as cm:
64 try:
65 yield
66 finally:
67 if cm.unraisable:
68 if cm.unraisable.err_msg is not None:
69 err_msg = cm.unraisable.err_msg
70 else:
71 err_msg = "Exception ignored in"
72 msg = f"{err_msg}: {cm.unraisable.object!r}\n\n"
73 msg += "".join(
74 traceback.format_exception(
75 cm.unraisable.exc_type,
76 cm.unraisable.exc_value,
77 cm.unraisable.exc_traceback,
78 )
79 )
80 warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
81
82
83 @pytest.hookimpl(wrapper=True, tryfirst=True)
84 def pytest_runtest_setup() -> Generator[None, None, None]:
85 yield from unraisable_exception_runtest_hook()
86
87
88 @pytest.hookimpl(wrapper=True, tryfirst=True)
89 def pytest_runtest_call() -> Generator[None, None, None]:
90 yield from unraisable_exception_runtest_hook()
91
92
93 @pytest.hookimpl(wrapper=True, tryfirst=True)
94 def pytest_runtest_teardown() -> Generator[None, None, None]:
95 yield from unraisable_exception_runtest_hook()