| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import gc |
| 4 |
import sys |
| 5 |
from traceback import extract_tb |
| 6 |
from typing import TYPE_CHECKING, NoReturn |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from .._concat_tb import concat_tb |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from collections.abc import Callable |
| 14 |
from types import TracebackType |
| 15 |
|
| 16 |
if sys.version_info < (3, 11): |
| 17 |
from exceptiongroup import ExceptionGroup |
| 18 |
|
| 19 |
|
| 20 |
def raiser1() -> NoReturn: |
| 21 |
raiser1_2() |
| 22 |
|
| 23 |
|
| 24 |
def raiser1_2() -> NoReturn: |
| 25 |
raiser1_3() |
| 26 |
|
| 27 |
|
| 28 |
def raiser1_3() -> NoReturn: |
| 29 |
raise ValueError("raiser1_string") |
| 30 |
|
| 31 |
|
| 32 |
def raiser2() -> NoReturn: |
| 33 |
raiser2_2() |
| 34 |
|
| 35 |
|
| 36 |
def raiser2_2() -> NoReturn: |
| 37 |
raise KeyError("raiser2_string") |
| 38 |
|
| 39 |
|
| 40 |
def get_exc(raiser: Callable[[], NoReturn]) -> Exception: |
| 41 |
try: |
| 42 |
raiser() |
| 43 |
except Exception as exc: |
| 44 |
return exc |
| 45 |
raise AssertionError("raiser should always raise") # pragma: no cover |
| 46 |
|
| 47 |
|
| 48 |
def get_tb(raiser: Callable[[], NoReturn]) -> TracebackType | None: |
| 49 |
return get_exc(raiser).__traceback__ |
| 50 |
|
| 51 |
|
| 52 |
def test_concat_tb() -> None: |
| 53 |
tb1 = get_tb(raiser1) |
| 54 |
tb2 = get_tb(raiser2) |
| 55 |
|
| 56 |
# These return a list of (filename, lineno, fn name, text) tuples |
| 57 |
# https://docs.python.org/3/library/traceback.html#traceback.extract_tb |
| 58 |
entries1 = extract_tb(tb1) |
| 59 |
entries2 = extract_tb(tb2) |
| 60 |
|
| 61 |
tb12 = concat_tb(tb1, tb2) |
| 62 |
assert extract_tb(tb12) == entries1 + entries2 |
| 63 |
|
| 64 |
tb21 = concat_tb(tb2, tb1) |
| 65 |
assert extract_tb(tb21) == entries2 + entries1 |
| 66 |
|
| 67 |
# Check degenerate cases |
| 68 |
assert extract_tb(concat_tb(None, tb1)) == entries1 |
| 69 |
assert extract_tb(concat_tb(tb1, None)) == entries1 |
| 70 |
assert concat_tb(None, None) is None |
| 71 |
|
| 72 |
# Make sure the original tracebacks didn't get mutated by mistake |
| 73 |
assert extract_tb(get_tb(raiser1)) == entries1 |
| 74 |
assert extract_tb(get_tb(raiser2)) == entries2 |
| 75 |
|
| 76 |
|
| 77 |
# Unclear if this can still fail, removing the `del` from _concat_tb.copy_tb does not seem |
| 78 |
# to trigger it (on a platform where the `del` is executed) |
| 79 |
@pytest.mark.skipif( |
| 80 |
sys.implementation.name != "cpython", |
| 81 |
reason="Only makes sense with refcounting GC", |
| 82 |
) |
| 83 |
def test_ExceptionGroup_catch_doesnt_create_cyclic_garbage() -> None: |
| 84 |
# https://github.com/python-trio/trio/pull/2063 |
| 85 |
gc.collect() |
| 86 |
old_flags = gc.get_debug() |
| 87 |
|
| 88 |
def make_multi() -> NoReturn: |
| 89 |
raise ExceptionGroup("", [get_exc(raiser1), get_exc(raiser2)]) |
| 90 |
|
| 91 |
try: |
| 92 |
gc.set_debug(gc.DEBUG_SAVEALL) |
| 93 |
with pytest.raises(ExceptionGroup) as excinfo: |
| 94 |
# covers ~~MultiErrorCatcher.__exit__ and~~ _concat_tb.copy_tb |
| 95 |
# TODO: is the above comment true anymore? as this no longer uses MultiError.catch |
| 96 |
raise make_multi() |
| 97 |
for exc in excinfo.value.exceptions: |
| 98 |
assert isinstance(exc, (ValueError, KeyError)) |
| 99 |
gc.collect() |
| 100 |
assert not gc.garbage |
| 101 |
finally: |
| 102 |
gc.set_debug(old_flags) |
| 103 |
gc.garbage.clear() |