7
7
import trio
8
8
9
9
10
- def move_on_at (deadline : float ) -> trio .CancelScope :
10
+ def move_on_at (deadline : float , * , shield : bool = False ) -> trio .CancelScope :
11
11
"""Use as a context manager to create a cancel scope with the given
12
12
absolute deadline.
13
13
14
14
Args:
15
15
deadline (float): The deadline.
16
+ shield (bool): Initial value for the `~trio.CancelScope.shield` attribute
17
+ of the newly created cancel scope.
16
18
17
19
Raises:
18
20
ValueError: if deadline is NaN.
19
21
20
22
"""
21
23
if math .isnan (deadline ):
22
24
raise ValueError ("deadline must not be NaN" )
23
- return trio .CancelScope (deadline = deadline )
25
+ return trio .CancelScope (deadline = deadline , shield = shield )
24
26
25
27
26
- def move_on_after (seconds : float ) -> trio .CancelScope :
28
+ def move_on_after (seconds : float , * , shield : bool = False ) -> trio .CancelScope :
27
29
"""Use as a context manager to create a cancel scope whose deadline is
28
30
set to now + *seconds*.
29
31
30
32
Args:
31
33
seconds (float): The timeout.
34
+ shield (bool): Initial value for the `~trio.CancelScope.shield` attribute
35
+ of the newly created cancel scope.
32
36
33
37
Raises:
34
38
ValueError: if timeout is less than zero or NaN.
35
39
36
40
"""
37
41
if seconds < 0 :
38
42
raise ValueError ("timeout must be non-negative" )
39
- return move_on_at (trio .current_time () + seconds )
43
+ return move_on_at (trio .current_time () + seconds , shield = shield )
40
44
41
45
42
46
async def sleep_forever () -> None :
@@ -96,7 +100,7 @@ class TooSlowError(Exception):
96
100
97
101
# workaround for PyCharm not being able to infer return type from @contextmanager
98
102
# see https://youtrack.jetbrains.com/issue/PY-36444/PyCharm-doesnt-infer-types-when-using-contextlib.contextmanager-decorator
99
- def fail_at (deadline : float ) -> AbstractContextManager [trio .CancelScope ]: # type: ignore[misc]
103
+ def fail_at (deadline : float , * , shield : bool = False ) -> AbstractContextManager [trio .CancelScope ]: # type: ignore[misc]
100
104
"""Creates a cancel scope with the given deadline, and raises an error if it
101
105
is actually cancelled.
102
106
@@ -110,14 +114,16 @@ def fail_at(deadline: float) -> AbstractContextManager[trio.CancelScope]: # typ
110
114
111
115
Args:
112
116
deadline (float): The deadline.
117
+ shield (bool): Initial value for the `~trio.CancelScope.shield` attribute
118
+ of the newly created cancel scope.
113
119
114
120
Raises:
115
121
TooSlowError: if a :exc:`Cancelled` exception is raised in this scope
116
122
and caught by the context manager.
117
123
ValueError: if deadline is NaN.
118
124
119
125
"""
120
- with move_on_at (deadline ) as scope :
126
+ with move_on_at (deadline , shield = shield ) as scope :
121
127
yield scope
122
128
if scope .cancelled_caught :
123
129
raise TooSlowError
@@ -127,7 +133,9 @@ def fail_at(deadline: float) -> AbstractContextManager[trio.CancelScope]: # typ
127
133
fail_at = contextmanager (fail_at )
128
134
129
135
130
- def fail_after (seconds : float ) -> AbstractContextManager [trio .CancelScope ]:
136
+ def fail_after (
137
+ seconds : float , * , shield : bool = False
138
+ ) -> AbstractContextManager [trio .CancelScope ]:
131
139
"""Creates a cancel scope with the given timeout, and raises an error if
132
140
it is actually cancelled.
133
141
@@ -140,6 +148,8 @@ def fail_after(seconds: float) -> AbstractContextManager[trio.CancelScope]:
140
148
141
149
Args:
142
150
seconds (float): The timeout.
151
+ shield (bool): Initial value for the `~trio.CancelScope.shield` attribute
152
+ of the newly created cancel scope.
143
153
144
154
Raises:
145
155
TooSlowError: if a :exc:`Cancelled` exception is raised in this scope
@@ -149,4 +159,4 @@ def fail_after(seconds: float) -> AbstractContextManager[trio.CancelScope]:
149
159
"""
150
160
if seconds < 0 :
151
161
raise ValueError ("timeout must be non-negative" )
152
- return fail_at (trio .current_time () + seconds )
162
+ return fail_at (trio .current_time () + seconds , shield = shield )
0 commit comments