| 1 |
import pickle |
| 2 |
import re |
| 3 |
from math import inf |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import trio |
| 8 |
from trio import Cancelled |
| 9 |
from trio.lowlevel import current_task |
| 10 |
from trio.testing import wait_all_tasks_blocked |
| 11 |
|
| 12 |
from .test_ki import ki_self |
| 13 |
|
| 14 |
|
| 15 |
def test_Cancelled_init() -> None: |
| 16 |
with pytest.raises(TypeError, match=r"^trio.Cancelled has no public constructor$"): |
| 17 |
raise Cancelled |
| 18 |
|
| 19 |
with pytest.raises(TypeError, match=r"^trio.Cancelled has no public constructor$"): |
| 20 |
Cancelled(source="explicit") |
| 21 |
|
| 22 |
# private constructor should not raise |
| 23 |
Cancelled._create(source="explicit") |
| 24 |
|
| 25 |
|
| 26 |
async def test_Cancelled_str() -> None: |
| 27 |
cancelled = Cancelled._create(source="explicit") |
| 28 |
assert str(cancelled) == "cancelled due to explicit" |
| 29 |
# note: repr(current_task()) is often fairly verbose |
| 30 |
assert re.fullmatch( |
| 31 |
r"cancelled due to deadline from task " |
| 32 |
r"<Task 'trio._core._tests.test_cancelled.test_Cancelled_str' at 0x\w*>", |
| 33 |
str( |
| 34 |
Cancelled._create( |
| 35 |
source="deadline", |
| 36 |
source_task=repr(current_task()), |
| 37 |
) |
| 38 |
), |
| 39 |
) |
| 40 |
|
| 41 |
assert re.fullmatch( |
| 42 |
rf"cancelled due to nursery with reason 'pigs flying' from task {current_task()!r}", |
| 43 |
str( |
| 44 |
Cancelled._create( |
| 45 |
source="nursery", |
| 46 |
source_task=repr(current_task()), |
| 47 |
reason="pigs flying", |
| 48 |
) |
| 49 |
), |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
def test_Cancelled_subclass() -> None: |
| 54 |
with pytest.raises(TypeError): |
| 55 |
type("Subclass", (Cancelled,), {}) |
| 56 |
|
| 57 |
|
| 58 |
# https://github.com/python-trio/trio/issues/3248 |
| 59 |
def test_Cancelled_pickle() -> None: |
| 60 |
cancelled = Cancelled._create(source="KeyboardInterrupt") |
| 61 |
pickled_cancelled = pickle.loads(pickle.dumps(cancelled)) |
| 62 |
assert isinstance(pickled_cancelled, Cancelled) |
| 63 |
assert cancelled.source == pickled_cancelled.source |
| 64 |
assert cancelled.source_task == pickled_cancelled.source_task |
| 65 |
assert cancelled.reason == pickled_cancelled.reason |
| 66 |
|
| 67 |
|
| 68 |
async def test_cancel_reason() -> None: |
| 69 |
with trio.CancelScope() as cs: |
| 70 |
cs.cancel(reason="hello") |
| 71 |
with pytest.raises( |
| 72 |
Cancelled, |
| 73 |
match=rf"^cancelled due to explicit with reason 'hello' from task {current_task()!r}$", |
| 74 |
) as excinfo: |
| 75 |
await trio.lowlevel.checkpoint() |
| 76 |
assert excinfo.value.source == "explicit" |
| 77 |
assert excinfo.value.reason == "hello" |
| 78 |
assert excinfo.value.source_task == repr(current_task()) |
| 79 |
|
| 80 |
with trio.CancelScope(deadline=-inf) as cs: |
| 81 |
with pytest.raises(Cancelled, match=r"^cancelled due to deadline"): |
| 82 |
await trio.lowlevel.checkpoint() |
| 83 |
|
| 84 |
with trio.CancelScope() as cs: |
| 85 |
cs.deadline = -inf |
| 86 |
with pytest.raises( |
| 87 |
Cancelled, |
| 88 |
match=r"^cancelled due to deadline", |
| 89 |
): |
| 90 |
await trio.lowlevel.checkpoint() |
| 91 |
|
| 92 |
|
| 93 |
match_str = r"^cancelled due to nursery with reason 'child task raised exception ValueError\(\)' from task {0!r}$" |
| 94 |
|
| 95 |
|
| 96 |
async def cancelled_task( |
| 97 |
fail_task: trio.lowlevel.Task, task_status: trio.TaskStatus |
| 98 |
) -> None: |
| 99 |
task_status.started() |
| 100 |
with pytest.raises(Cancelled, match=match_str.format(fail_task)): |
| 101 |
await trio.sleep_forever() |
| 102 |
raise TypeError |
| 103 |
|
| 104 |
|
| 105 |
# failing_task raises before cancelled_task is started |
| 106 |
async def test_cancel_reason_nursery() -> None: |
| 107 |
async def failing_task(task_status: trio.TaskStatus[trio.lowlevel.Task]) -> None: |
| 108 |
task_status.started(current_task()) |
| 109 |
raise ValueError |
| 110 |
|
| 111 |
with pytest.RaisesGroup(ValueError, TypeError): |
| 112 |
async with trio.open_nursery() as nursery: |
| 113 |
fail_task = await nursery.start(failing_task) |
| 114 |
with pytest.raises(Cancelled, match=match_str.format(fail_task)): |
| 115 |
await wait_all_tasks_blocked() |
| 116 |
await nursery.start(cancelled_task, fail_task) |
| 117 |
|
| 118 |
|
| 119 |
# wait until both tasks are running before failing_task raises |
| 120 |
async def test_cancel_reason_nursery2() -> None: |
| 121 |
async def failing_task(task_status: trio.TaskStatus[trio.lowlevel.Task]) -> None: |
| 122 |
task_status.started(current_task()) |
| 123 |
await wait_all_tasks_blocked() |
| 124 |
raise ValueError |
| 125 |
|
| 126 |
with pytest.RaisesGroup(ValueError, TypeError): |
| 127 |
async with trio.open_nursery() as nursery: |
| 128 |
fail_task = await nursery.start(failing_task) |
| 129 |
await nursery.start(cancelled_task, fail_task) |
| 130 |
|
| 131 |
|
| 132 |
# failing_task raises before calling task_status.started() |
| 133 |
async def test_cancel_reason_nursery3() -> None: |
| 134 |
async def failing_task(task_status: trio.TaskStatus[None]) -> None: |
| 135 |
raise ValueError |
| 136 |
|
| 137 |
parent_task = current_task() |
| 138 |
|
| 139 |
async def cancelled_task() -> None: |
| 140 |
# We don't have a way of distinguishing that the nursery code block failed |
| 141 |
# because it failed to `start()` a task. |
| 142 |
with pytest.raises( |
| 143 |
Cancelled, |
| 144 |
match=re.escape( |
| 145 |
rf"cancelled due to nursery with reason 'Code block inside nursery contextmanager raised exception ValueError()' from task {parent_task!r}" |
| 146 |
), |
| 147 |
): |
| 148 |
await trio.sleep_forever() |
| 149 |
|
| 150 |
with pytest.RaisesGroup(ValueError): |
| 151 |
async with trio.open_nursery() as nursery: |
| 152 |
nursery.start_soon(cancelled_task) |
| 153 |
await wait_all_tasks_blocked() |
| 154 |
await nursery.start(failing_task) |
| 155 |
|
| 156 |
|
| 157 |
async def test_cancel_reason_not_overwritten() -> None: |
| 158 |
with trio.CancelScope() as cs: |
| 159 |
cs.cancel() |
| 160 |
with pytest.raises( |
| 161 |
Cancelled, |
| 162 |
match=rf"^cancelled due to explicit from task {current_task()!r}$", |
| 163 |
): |
| 164 |
await trio.lowlevel.checkpoint() |
| 165 |
cs.deadline = -inf |
| 166 |
with pytest.raises( |
| 167 |
Cancelled, |
| 168 |
match=rf"^cancelled due to explicit from task {current_task()!r}$", |
| 169 |
): |
| 170 |
await trio.lowlevel.checkpoint() |
| 171 |
|
| 172 |
|
| 173 |
async def test_cancel_reason_not_overwritten_2() -> None: |
| 174 |
with trio.CancelScope() as cs: |
| 175 |
cs.deadline = -inf |
| 176 |
with pytest.raises(Cancelled, match=r"^cancelled due to deadline$"): |
| 177 |
await trio.lowlevel.checkpoint() |
| 178 |
cs.cancel() |
| 179 |
with pytest.raises(Cancelled, match=r"^cancelled due to deadline$"): |
| 180 |
await trio.lowlevel.checkpoint() |
| 181 |
|
| 182 |
|
| 183 |
async def test_nested_child_source() -> None: |
| 184 |
ev = trio.Event() |
| 185 |
parent_task = current_task() |
| 186 |
|
| 187 |
async def child() -> None: |
| 188 |
ev.set() |
| 189 |
with pytest.raises( |
| 190 |
Cancelled, |
| 191 |
match=rf"^cancelled due to nursery with reason 'Code block inside nursery contextmanager raised exception ValueError\(\)' from task {parent_task!r}$", |
| 192 |
): |
| 193 |
await trio.sleep_forever() |
| 194 |
|
| 195 |
with pytest.RaisesGroup(ValueError): |
| 196 |
async with trio.open_nursery() as nursery: |
| 197 |
nursery.start_soon(child) |
| 198 |
await ev.wait() |
| 199 |
raise ValueError |
| 200 |
|
| 201 |
|
| 202 |
async def test_reason_delayed_ki() -> None: |
| 203 |
# simplified version of test_ki.test_ki_protection_works check #2 |
| 204 |
parent_task = current_task() |
| 205 |
|
| 206 |
async def sleeper(name: str) -> None: |
| 207 |
with pytest.raises( |
| 208 |
Cancelled, |
| 209 |
match=rf"^cancelled due to KeyboardInterrupt from task {parent_task!r}$", |
| 210 |
): |
| 211 |
while True: |
| 212 |
await trio.lowlevel.checkpoint() |
| 213 |
|
| 214 |
async def raiser(name: str) -> None: |
| 215 |
ki_self() |
| 216 |
|
| 217 |
with pytest.RaisesGroup(KeyboardInterrupt): |
| 218 |
async with trio.open_nursery() as nursery: |
| 219 |
nursery.start_soon(sleeper, "s1") |
| 220 |
nursery.start_soon(sleeper, "s2") |
| 221 |
nursery.start_soon(trio.lowlevel.enable_ki_protection(raiser), "r1") |
| 222 |
# __aexit__ blocks, and then receives the KI |