|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import collections |
3 | 4 | from collections.abc import Callable
|
4 |
| -from collections.abc import Generator |
| 5 | +import functools |
| 6 | +import gc |
5 | 7 | import sys
|
6 | 8 | import traceback
|
7 |
| -from types import TracebackType |
8 |
| -from typing import Any |
| 9 | +from typing import NamedTuple |
9 | 10 | from typing import TYPE_CHECKING
|
10 | 11 | import warnings
|
11 | 12 |
|
| 13 | +from _pytest.config import Config |
| 14 | +from _pytest.nodes import Item |
| 15 | +from _pytest.stash import StashKey |
| 16 | +from _pytest.tracemalloc import tracemalloc_message |
12 | 17 | import pytest
|
13 | 18 |
|
14 | 19 |
|
15 | 20 | if TYPE_CHECKING:
|
16 |
| - from typing_extensions import Self |
17 |
| - |
18 |
| - |
19 |
| -# Copied from cpython/Lib/test/support/__init__.py, with modifications. |
20 |
| -class catch_unraisable_exception: |
21 |
| - """Context manager catching unraisable exception using sys.unraisablehook. |
22 |
| -
|
23 |
| - Storing the exception value (cm.unraisable.exc_value) creates a reference |
24 |
| - cycle. The reference cycle is broken explicitly when the context manager |
25 |
| - exits. |
26 |
| -
|
27 |
| - Storing the object (cm.unraisable.object) can resurrect it if it is set to |
28 |
| - an object which is being finalized. Exiting the context manager clears the |
29 |
| - stored object. |
30 |
| -
|
31 |
| - Usage: |
32 |
| - with catch_unraisable_exception() as cm: |
33 |
| - # code creating an "unraisable exception" |
34 |
| - ... |
35 |
| - # check the unraisable exception: use cm.unraisable |
36 |
| - ... |
37 |
| - # cm.unraisable attribute no longer exists at this point |
38 |
| - # (to break a reference cycle) |
39 |
| - """ |
40 |
| - |
41 |
| - def __init__(self) -> None: |
42 |
| - self.unraisable: sys.UnraisableHookArgs | None = None |
43 |
| - self._old_hook: Callable[[sys.UnraisableHookArgs], Any] | None = None |
44 |
| - |
45 |
| - def _hook(self, unraisable: sys.UnraisableHookArgs) -> None: |
46 |
| - # Storing unraisable.object can resurrect an object which is being |
47 |
| - # finalized. Storing unraisable.exc_value creates a reference cycle. |
48 |
| - self.unraisable = unraisable |
49 |
| - |
50 |
| - def __enter__(self) -> Self: |
51 |
| - self._old_hook = sys.unraisablehook |
52 |
| - sys.unraisablehook = self._hook |
53 |
| - return self |
54 |
| - |
55 |
| - def __exit__( |
56 |
| - self, |
57 |
| - exc_type: type[BaseException] | None, |
58 |
| - exc_val: BaseException | None, |
59 |
| - exc_tb: TracebackType | None, |
60 |
| - ) -> None: |
61 |
| - assert self._old_hook is not None |
62 |
| - sys.unraisablehook = self._old_hook |
63 |
| - self._old_hook = None |
64 |
| - del self.unraisable |
65 |
| - |
66 |
| - |
67 |
| -def unraisable_exception_runtest_hook() -> Generator[None]: |
68 |
| - with catch_unraisable_exception() as cm: |
69 |
| - try: |
70 |
| - yield |
71 |
| - finally: |
72 |
| - if cm.unraisable: |
73 |
| - if cm.unraisable.err_msg is not None: |
74 |
| - err_msg = cm.unraisable.err_msg |
75 |
| - else: |
76 |
| - err_msg = "Exception ignored in" |
77 |
| - msg = f"{err_msg}: {cm.unraisable.object!r}\n\n" |
78 |
| - msg += "".join( |
79 |
| - traceback.format_exception( |
80 |
| - cm.unraisable.exc_type, |
81 |
| - cm.unraisable.exc_value, |
82 |
| - cm.unraisable.exc_traceback, |
83 |
| - ) |
84 |
| - ) |
85 |
| - warnings.warn(pytest.PytestUnraisableExceptionWarning(msg)) |
| 21 | + pass |
| 22 | + |
| 23 | +if sys.version_info < (3, 11): |
| 24 | + from exceptiongroup import ExceptionGroup |
| 25 | + |
| 26 | + |
| 27 | +def gc_collect_harder() -> None: |
| 28 | + # A single collection doesn't necessarily collect everything. |
| 29 | + # Constant determined experimentally by the Trio project. |
| 30 | + for _ in range(5): |
| 31 | + gc.collect() |
86 | 32 |
|
87 | 33 |
|
88 |
| -@pytest.hookimpl(wrapper=True, tryfirst=True) |
89 |
| -def pytest_runtest_setup() -> Generator[None]: |
90 |
| - yield from unraisable_exception_runtest_hook() |
| 34 | +class UnraisableMeta(NamedTuple): |
| 35 | + msg: str |
| 36 | + cause_msg: str |
| 37 | + exc_value: BaseException | None |
91 | 38 |
|
92 | 39 |
|
93 |
| -@pytest.hookimpl(wrapper=True, tryfirst=True) |
94 |
| -def pytest_runtest_call() -> Generator[None]: |
95 |
| - yield from unraisable_exception_runtest_hook() |
| 40 | +unraisable_exceptions: StashKey[collections.deque[UnraisableMeta | BaseException]] = ( |
| 41 | + StashKey() |
| 42 | +) |
96 | 43 |
|
97 | 44 |
|
98 |
| -@pytest.hookimpl(wrapper=True, tryfirst=True) |
99 |
| -def pytest_runtest_teardown() -> Generator[None]: |
100 |
| - yield from unraisable_exception_runtest_hook() |
| 45 | +def collect_unraisable(config: Config) -> None: |
| 46 | + pop_unraisable = config.stash[unraisable_exceptions].pop |
| 47 | + errors: list[pytest.PytestUnraisableExceptionWarning | RuntimeError] = [] |
| 48 | + meta = None |
| 49 | + hook_error = None |
| 50 | + try: |
| 51 | + while True: |
| 52 | + try: |
| 53 | + meta = pop_unraisable() |
| 54 | + except IndexError: |
| 55 | + break |
| 56 | + |
| 57 | + if isinstance(meta, BaseException): |
| 58 | + hook_error = RuntimeError("Failed to process unraisable exception") |
| 59 | + hook_error.__cause__ = meta |
| 60 | + errors.append(hook_error) |
| 61 | + continue |
| 62 | + |
| 63 | + msg = meta.msg |
| 64 | + try: |
| 65 | + warnings.warn(pytest.PytestUnraisableExceptionWarning(msg)) |
| 66 | + except pytest.PytestUnraisableExceptionWarning as e: |
| 67 | + # This except happens when the warning is treated as an error (e.g. `-Werror`). |
| 68 | + if meta.exc_value is not None: |
| 69 | + # Exceptions have a better way to show the traceback, but |
| 70 | + # warnings do not, so hide the traceback from the msg and |
| 71 | + # set the cause so the traceback shows up in the right place. |
| 72 | + e.args = (meta.cause_msg,) |
| 73 | + e.__cause__ = meta.exc_value |
| 74 | + errors.append(e) |
| 75 | + |
| 76 | + if len(errors) == 1: |
| 77 | + raise errors[0] |
| 78 | + if errors: |
| 79 | + raise ExceptionGroup("multiple unraisable exception warnings", errors) |
| 80 | + finally: |
| 81 | + del errors, meta, hook_error |
| 82 | + |
| 83 | + |
| 84 | +def cleanup( |
| 85 | + *, config: Config, prev_hook: Callable[[sys.UnraisableHookArgs], object] |
| 86 | +) -> None: |
| 87 | + try: |
| 88 | + try: |
| 89 | + gc_collect_harder() |
| 90 | + collect_unraisable(config) |
| 91 | + finally: |
| 92 | + sys.unraisablehook = prev_hook |
| 93 | + finally: |
| 94 | + del config.stash[unraisable_exceptions] |
| 95 | + |
| 96 | + |
| 97 | +def unraisable_hook( |
| 98 | + unraisable: sys.UnraisableHookArgs, |
| 99 | + /, |
| 100 | + *, |
| 101 | + append: Callable[[UnraisableMeta | BaseException], object], |
| 102 | +) -> None: |
| 103 | + try: |
| 104 | + err_msg = ( |
| 105 | + "Exception ignored in" if unraisable.err_msg is None else unraisable.err_msg |
| 106 | + ) |
| 107 | + summary = f"{err_msg}: {unraisable.object!r}" |
| 108 | + traceback_message = "\n\n" + "".join( |
| 109 | + traceback.format_exception( |
| 110 | + unraisable.exc_type, |
| 111 | + unraisable.exc_value, |
| 112 | + unraisable.exc_traceback, |
| 113 | + ) |
| 114 | + ) |
| 115 | + tracemalloc_tb = "\n" + tracemalloc_message(unraisable.object) |
| 116 | + msg = summary + traceback_message + tracemalloc_tb |
| 117 | + cause_msg = summary + tracemalloc_tb |
| 118 | + |
| 119 | + append( |
| 120 | + UnraisableMeta( |
| 121 | + # we need to compute these strings here as they might change after |
| 122 | + # the unraisablehook finishes and before the unraisable object is |
| 123 | + # collected by a hook |
| 124 | + msg=msg, |
| 125 | + cause_msg=cause_msg, |
| 126 | + exc_value=unraisable.exc_value, |
| 127 | + ) |
| 128 | + ) |
| 129 | + except BaseException as e: |
| 130 | + append(e) |
| 131 | + # Raising this will cause the exception to be logged twice, once in our |
| 132 | + # collect_unraisable and once by the unraisablehook calling machinery |
| 133 | + # which is fine - this should never happen anyway and if it does |
| 134 | + # it should probably be reported as a pytest bug. |
| 135 | + raise |
| 136 | + |
| 137 | + |
| 138 | +def pytest_configure(config: Config) -> None: |
| 139 | + prev_hook = sys.unraisablehook |
| 140 | + deque: collections.deque[UnraisableMeta | BaseException] = collections.deque() |
| 141 | + config.stash[unraisable_exceptions] = deque |
| 142 | + config.add_cleanup(functools.partial(cleanup, config=config, prev_hook=prev_hook)) |
| 143 | + sys.unraisablehook = functools.partial(unraisable_hook, append=deque.append) |
| 144 | + |
| 145 | + |
| 146 | +@pytest.hookimpl(trylast=True) |
| 147 | +def pytest_runtest_setup(item: Item) -> None: |
| 148 | + collect_unraisable(item.config) |
| 149 | + |
| 150 | + |
| 151 | +@pytest.hookimpl(trylast=True) |
| 152 | +def pytest_runtest_call(item: Item) -> None: |
| 153 | + collect_unraisable(item.config) |
| 154 | + |
| 155 | + |
| 156 | +@pytest.hookimpl(trylast=True) |
| 157 | +def pytest_runtest_teardown(item: Item) -> None: |
| 158 | + collect_unraisable(item.config) |
0 commit comments