1 | n/a | #include "Python.h" |
---|
2 | n/a | #include "structmember.h" |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | /*[clinic input] |
---|
6 | n/a | module _asyncio |
---|
7 | n/a | [clinic start generated code]*/ |
---|
8 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/ |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | /* identifiers used from some functions */ |
---|
12 | n/a | _Py_IDENTIFIER(add_done_callback); |
---|
13 | n/a | _Py_IDENTIFIER(call_soon); |
---|
14 | n/a | _Py_IDENTIFIER(cancel); |
---|
15 | n/a | _Py_IDENTIFIER(send); |
---|
16 | n/a | _Py_IDENTIFIER(throw); |
---|
17 | n/a | _Py_IDENTIFIER(_step); |
---|
18 | n/a | _Py_IDENTIFIER(_schedule_callbacks); |
---|
19 | n/a | _Py_IDENTIFIER(_wakeup); |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | /* State of the _asyncio module */ |
---|
23 | n/a | static PyObject *all_tasks; |
---|
24 | n/a | static PyObject *current_tasks; |
---|
25 | n/a | static PyObject *traceback_extract_stack; |
---|
26 | n/a | static PyObject *asyncio_get_event_loop; |
---|
27 | n/a | static PyObject *asyncio_future_repr_info_func; |
---|
28 | n/a | static PyObject *asyncio_task_repr_info_func; |
---|
29 | n/a | static PyObject *asyncio_task_get_stack_func; |
---|
30 | n/a | static PyObject *asyncio_task_print_stack_func; |
---|
31 | n/a | static PyObject *asyncio_InvalidStateError; |
---|
32 | n/a | static PyObject *asyncio_CancelledError; |
---|
33 | n/a | static PyObject *inspect_isgenerator; |
---|
34 | n/a | |
---|
35 | n/a | |
---|
36 | n/a | typedef enum { |
---|
37 | n/a | STATE_PENDING, |
---|
38 | n/a | STATE_CANCELLED, |
---|
39 | n/a | STATE_FINISHED |
---|
40 | n/a | } fut_state; |
---|
41 | n/a | |
---|
42 | n/a | #define FutureObj_HEAD(prefix) \ |
---|
43 | n/a | PyObject_HEAD \ |
---|
44 | n/a | PyObject *prefix##_loop; \ |
---|
45 | n/a | PyObject *prefix##_callbacks; \ |
---|
46 | n/a | PyObject *prefix##_exception; \ |
---|
47 | n/a | PyObject *prefix##_result; \ |
---|
48 | n/a | PyObject *prefix##_source_tb; \ |
---|
49 | n/a | fut_state prefix##_state; \ |
---|
50 | n/a | int prefix##_log_tb; \ |
---|
51 | n/a | int prefix##_blocking; \ |
---|
52 | n/a | PyObject *dict; \ |
---|
53 | n/a | PyObject *prefix##_weakreflist; |
---|
54 | n/a | |
---|
55 | n/a | typedef struct { |
---|
56 | n/a | FutureObj_HEAD(fut) |
---|
57 | n/a | } FutureObj; |
---|
58 | n/a | |
---|
59 | n/a | typedef struct { |
---|
60 | n/a | FutureObj_HEAD(task) |
---|
61 | n/a | PyObject *task_fut_waiter; |
---|
62 | n/a | PyObject *task_coro; |
---|
63 | n/a | int task_must_cancel; |
---|
64 | n/a | int task_log_destroy_pending; |
---|
65 | n/a | } TaskObj; |
---|
66 | n/a | |
---|
67 | n/a | typedef struct { |
---|
68 | n/a | PyObject_HEAD |
---|
69 | n/a | TaskObj *sw_task; |
---|
70 | n/a | PyObject *sw_arg; |
---|
71 | n/a | } TaskSendMethWrapper; |
---|
72 | n/a | |
---|
73 | n/a | typedef struct { |
---|
74 | n/a | PyObject_HEAD |
---|
75 | n/a | TaskObj *ww_task; |
---|
76 | n/a | } TaskWakeupMethWrapper; |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | #include "clinic/_asynciomodule.c.h" |
---|
80 | n/a | |
---|
81 | n/a | |
---|
82 | n/a | /*[clinic input] |
---|
83 | n/a | class _asyncio.Future "FutureObj *" "&Future_Type" |
---|
84 | n/a | [clinic start generated code]*/ |
---|
85 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=00d3e4abca711e0f]*/ |
---|
86 | n/a | |
---|
87 | n/a | /* Get FutureIter from Future */ |
---|
88 | n/a | static PyObject* future_new_iter(PyObject *); |
---|
89 | n/a | static inline int future_call_schedule_callbacks(FutureObj *); |
---|
90 | n/a | |
---|
91 | n/a | static int |
---|
92 | n/a | future_schedule_callbacks(FutureObj *fut) |
---|
93 | n/a | { |
---|
94 | n/a | Py_ssize_t len; |
---|
95 | n/a | PyObject* iters; |
---|
96 | n/a | int i; |
---|
97 | n/a | |
---|
98 | n/a | if (fut->fut_callbacks == NULL) { |
---|
99 | n/a | PyErr_SetString(PyExc_RuntimeError, "NULL callbacks"); |
---|
100 | n/a | return -1; |
---|
101 | n/a | } |
---|
102 | n/a | |
---|
103 | n/a | len = PyList_GET_SIZE(fut->fut_callbacks); |
---|
104 | n/a | if (len == 0) { |
---|
105 | n/a | return 0; |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | iters = PyList_GetSlice(fut->fut_callbacks, 0, len); |
---|
109 | n/a | if (iters == NULL) { |
---|
110 | n/a | return -1; |
---|
111 | n/a | } |
---|
112 | n/a | if (PyList_SetSlice(fut->fut_callbacks, 0, len, NULL) < 0) { |
---|
113 | n/a | Py_DECREF(iters); |
---|
114 | n/a | return -1; |
---|
115 | n/a | } |
---|
116 | n/a | |
---|
117 | n/a | for (i = 0; i < len; i++) { |
---|
118 | n/a | PyObject *handle = NULL; |
---|
119 | n/a | PyObject *cb = PyList_GET_ITEM(iters, i); |
---|
120 | n/a | |
---|
121 | n/a | handle = _PyObject_CallMethodIdObjArgs(fut->fut_loop, &PyId_call_soon, |
---|
122 | n/a | cb, fut, NULL); |
---|
123 | n/a | |
---|
124 | n/a | if (handle == NULL) { |
---|
125 | n/a | Py_DECREF(iters); |
---|
126 | n/a | return -1; |
---|
127 | n/a | } |
---|
128 | n/a | else { |
---|
129 | n/a | Py_DECREF(handle); |
---|
130 | n/a | } |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | Py_DECREF(iters); |
---|
134 | n/a | return 0; |
---|
135 | n/a | } |
---|
136 | n/a | |
---|
137 | n/a | static int |
---|
138 | n/a | future_init(FutureObj *fut, PyObject *loop) |
---|
139 | n/a | { |
---|
140 | n/a | PyObject *res = NULL; |
---|
141 | n/a | _Py_IDENTIFIER(get_debug); |
---|
142 | n/a | |
---|
143 | n/a | if (loop == NULL || loop == Py_None) { |
---|
144 | n/a | loop = _PyObject_CallNoArg(asyncio_get_event_loop); |
---|
145 | n/a | if (loop == NULL) { |
---|
146 | n/a | return -1; |
---|
147 | n/a | } |
---|
148 | n/a | } |
---|
149 | n/a | else { |
---|
150 | n/a | Py_INCREF(loop); |
---|
151 | n/a | } |
---|
152 | n/a | Py_CLEAR(fut->fut_loop); |
---|
153 | n/a | fut->fut_loop = loop; |
---|
154 | n/a | |
---|
155 | n/a | res = _PyObject_CallMethodId(fut->fut_loop, &PyId_get_debug, NULL); |
---|
156 | n/a | if (res == NULL) { |
---|
157 | n/a | return -1; |
---|
158 | n/a | } |
---|
159 | n/a | if (PyObject_IsTrue(res)) { |
---|
160 | n/a | Py_CLEAR(res); |
---|
161 | n/a | fut->fut_source_tb = _PyObject_CallNoArg(traceback_extract_stack); |
---|
162 | n/a | if (fut->fut_source_tb == NULL) { |
---|
163 | n/a | return -1; |
---|
164 | n/a | } |
---|
165 | n/a | } |
---|
166 | n/a | else { |
---|
167 | n/a | Py_CLEAR(res); |
---|
168 | n/a | } |
---|
169 | n/a | |
---|
170 | n/a | fut->fut_callbacks = PyList_New(0); |
---|
171 | n/a | if (fut->fut_callbacks == NULL) { |
---|
172 | n/a | return -1; |
---|
173 | n/a | } |
---|
174 | n/a | |
---|
175 | n/a | return 0; |
---|
176 | n/a | } |
---|
177 | n/a | |
---|
178 | n/a | static PyObject * |
---|
179 | n/a | future_set_result(FutureObj *fut, PyObject *res) |
---|
180 | n/a | { |
---|
181 | n/a | if (fut->fut_state != STATE_PENDING) { |
---|
182 | n/a | PyErr_SetString(asyncio_InvalidStateError, "invalid state"); |
---|
183 | n/a | return NULL; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | Py_INCREF(res); |
---|
187 | n/a | fut->fut_result = res; |
---|
188 | n/a | fut->fut_state = STATE_FINISHED; |
---|
189 | n/a | |
---|
190 | n/a | if (future_call_schedule_callbacks(fut) == -1) { |
---|
191 | n/a | return NULL; |
---|
192 | n/a | } |
---|
193 | n/a | Py_RETURN_NONE; |
---|
194 | n/a | } |
---|
195 | n/a | |
---|
196 | n/a | static PyObject * |
---|
197 | n/a | future_set_exception(FutureObj *fut, PyObject *exc) |
---|
198 | n/a | { |
---|
199 | n/a | PyObject *exc_val = NULL; |
---|
200 | n/a | |
---|
201 | n/a | if (fut->fut_state != STATE_PENDING) { |
---|
202 | n/a | PyErr_SetString(asyncio_InvalidStateError, "invalid state"); |
---|
203 | n/a | return NULL; |
---|
204 | n/a | } |
---|
205 | n/a | |
---|
206 | n/a | if (PyExceptionClass_Check(exc)) { |
---|
207 | n/a | exc_val = _PyObject_CallNoArg(exc); |
---|
208 | n/a | if (exc_val == NULL) { |
---|
209 | n/a | return NULL; |
---|
210 | n/a | } |
---|
211 | n/a | } |
---|
212 | n/a | else { |
---|
213 | n/a | exc_val = exc; |
---|
214 | n/a | Py_INCREF(exc_val); |
---|
215 | n/a | } |
---|
216 | n/a | if (!PyExceptionInstance_Check(exc_val)) { |
---|
217 | n/a | Py_DECREF(exc_val); |
---|
218 | n/a | PyErr_SetString(PyExc_TypeError, "invalid exception object"); |
---|
219 | n/a | return NULL; |
---|
220 | n/a | } |
---|
221 | n/a | if ((PyObject*)Py_TYPE(exc_val) == PyExc_StopIteration) { |
---|
222 | n/a | Py_DECREF(exc_val); |
---|
223 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
224 | n/a | "StopIteration interacts badly with generators " |
---|
225 | n/a | "and cannot be raised into a Future"); |
---|
226 | n/a | return NULL; |
---|
227 | n/a | } |
---|
228 | n/a | |
---|
229 | n/a | fut->fut_exception = exc_val; |
---|
230 | n/a | fut->fut_state = STATE_FINISHED; |
---|
231 | n/a | |
---|
232 | n/a | if (future_call_schedule_callbacks(fut) == -1) { |
---|
233 | n/a | return NULL; |
---|
234 | n/a | } |
---|
235 | n/a | |
---|
236 | n/a | fut->fut_log_tb = 1; |
---|
237 | n/a | Py_RETURN_NONE; |
---|
238 | n/a | } |
---|
239 | n/a | |
---|
240 | n/a | static int |
---|
241 | n/a | future_get_result(FutureObj *fut, PyObject **result) |
---|
242 | n/a | { |
---|
243 | n/a | PyObject *exc; |
---|
244 | n/a | |
---|
245 | n/a | if (fut->fut_state == STATE_CANCELLED) { |
---|
246 | n/a | exc = _PyObject_CallNoArg(asyncio_CancelledError); |
---|
247 | n/a | if (exc == NULL) { |
---|
248 | n/a | return -1; |
---|
249 | n/a | } |
---|
250 | n/a | *result = exc; |
---|
251 | n/a | return 1; |
---|
252 | n/a | } |
---|
253 | n/a | |
---|
254 | n/a | if (fut->fut_state != STATE_FINISHED) { |
---|
255 | n/a | PyObject *msg = PyUnicode_FromString("Result is not ready."); |
---|
256 | n/a | if (msg == NULL) { |
---|
257 | n/a | return -1; |
---|
258 | n/a | } |
---|
259 | n/a | |
---|
260 | n/a | exc = PyObject_CallFunctionObjArgs(asyncio_InvalidStateError, msg, NULL); |
---|
261 | n/a | Py_DECREF(msg); |
---|
262 | n/a | if (exc == NULL) { |
---|
263 | n/a | return -1; |
---|
264 | n/a | } |
---|
265 | n/a | |
---|
266 | n/a | *result = exc; |
---|
267 | n/a | return 1; |
---|
268 | n/a | } |
---|
269 | n/a | |
---|
270 | n/a | fut->fut_log_tb = 0; |
---|
271 | n/a | if (fut->fut_exception != NULL) { |
---|
272 | n/a | Py_INCREF(fut->fut_exception); |
---|
273 | n/a | *result = fut->fut_exception; |
---|
274 | n/a | return 1; |
---|
275 | n/a | } |
---|
276 | n/a | |
---|
277 | n/a | Py_INCREF(fut->fut_result); |
---|
278 | n/a | *result = fut->fut_result; |
---|
279 | n/a | return 0; |
---|
280 | n/a | } |
---|
281 | n/a | |
---|
282 | n/a | static PyObject * |
---|
283 | n/a | future_add_done_callback(FutureObj *fut, PyObject *arg) |
---|
284 | n/a | { |
---|
285 | n/a | if (fut->fut_state != STATE_PENDING) { |
---|
286 | n/a | PyObject *handle = _PyObject_CallMethodIdObjArgs(fut->fut_loop, |
---|
287 | n/a | &PyId_call_soon, |
---|
288 | n/a | arg, fut, NULL); |
---|
289 | n/a | |
---|
290 | n/a | if (handle == NULL) { |
---|
291 | n/a | return NULL; |
---|
292 | n/a | } |
---|
293 | n/a | else { |
---|
294 | n/a | Py_DECREF(handle); |
---|
295 | n/a | } |
---|
296 | n/a | } |
---|
297 | n/a | else { |
---|
298 | n/a | int err = PyList_Append(fut->fut_callbacks, arg); |
---|
299 | n/a | if (err != 0) { |
---|
300 | n/a | return NULL; |
---|
301 | n/a | } |
---|
302 | n/a | } |
---|
303 | n/a | Py_RETURN_NONE; |
---|
304 | n/a | } |
---|
305 | n/a | |
---|
306 | n/a | static PyObject * |
---|
307 | n/a | future_cancel(FutureObj *fut) |
---|
308 | n/a | { |
---|
309 | n/a | if (fut->fut_state != STATE_PENDING) { |
---|
310 | n/a | Py_RETURN_FALSE; |
---|
311 | n/a | } |
---|
312 | n/a | fut->fut_state = STATE_CANCELLED; |
---|
313 | n/a | |
---|
314 | n/a | if (future_call_schedule_callbacks(fut) == -1) { |
---|
315 | n/a | return NULL; |
---|
316 | n/a | } |
---|
317 | n/a | |
---|
318 | n/a | Py_RETURN_TRUE; |
---|
319 | n/a | } |
---|
320 | n/a | |
---|
321 | n/a | /*[clinic input] |
---|
322 | n/a | _asyncio.Future.__init__ |
---|
323 | n/a | |
---|
324 | n/a | * |
---|
325 | n/a | loop: 'O' = NULL |
---|
326 | n/a | |
---|
327 | n/a | This class is *almost* compatible with concurrent.futures.Future. |
---|
328 | n/a | |
---|
329 | n/a | Differences: |
---|
330 | n/a | |
---|
331 | n/a | - result() and exception() do not take a timeout argument and |
---|
332 | n/a | raise an exception when the future isn't done yet. |
---|
333 | n/a | |
---|
334 | n/a | - Callbacks registered with add_done_callback() are always called |
---|
335 | n/a | via the event loop's call_soon_threadsafe(). |
---|
336 | n/a | |
---|
337 | n/a | - This class is not compatible with the wait() and as_completed() |
---|
338 | n/a | methods in the concurrent.futures package. |
---|
339 | n/a | [clinic start generated code]*/ |
---|
340 | n/a | |
---|
341 | n/a | static int |
---|
342 | n/a | _asyncio_Future___init___impl(FutureObj *self, PyObject *loop) |
---|
343 | n/a | /*[clinic end generated code: output=9ed75799eaccb5d6 input=8e1681f23605be2d]*/ |
---|
344 | n/a | |
---|
345 | n/a | { |
---|
346 | n/a | return future_init(self, loop); |
---|
347 | n/a | } |
---|
348 | n/a | |
---|
349 | n/a | static int |
---|
350 | n/a | FutureObj_clear(FutureObj *fut) |
---|
351 | n/a | { |
---|
352 | n/a | Py_CLEAR(fut->fut_loop); |
---|
353 | n/a | Py_CLEAR(fut->fut_callbacks); |
---|
354 | n/a | Py_CLEAR(fut->fut_result); |
---|
355 | n/a | Py_CLEAR(fut->fut_exception); |
---|
356 | n/a | Py_CLEAR(fut->fut_source_tb); |
---|
357 | n/a | Py_CLEAR(fut->dict); |
---|
358 | n/a | return 0; |
---|
359 | n/a | } |
---|
360 | n/a | |
---|
361 | n/a | static int |
---|
362 | n/a | FutureObj_traverse(FutureObj *fut, visitproc visit, void *arg) |
---|
363 | n/a | { |
---|
364 | n/a | Py_VISIT(fut->fut_loop); |
---|
365 | n/a | Py_VISIT(fut->fut_callbacks); |
---|
366 | n/a | Py_VISIT(fut->fut_result); |
---|
367 | n/a | Py_VISIT(fut->fut_exception); |
---|
368 | n/a | Py_VISIT(fut->fut_source_tb); |
---|
369 | n/a | Py_VISIT(fut->dict); |
---|
370 | n/a | return 0; |
---|
371 | n/a | } |
---|
372 | n/a | |
---|
373 | n/a | /*[clinic input] |
---|
374 | n/a | _asyncio.Future.result |
---|
375 | n/a | |
---|
376 | n/a | Return the result this future represents. |
---|
377 | n/a | |
---|
378 | n/a | If the future has been cancelled, raises CancelledError. If the |
---|
379 | n/a | future's result isn't yet available, raises InvalidStateError. If |
---|
380 | n/a | the future is done and has an exception set, this exception is raised. |
---|
381 | n/a | [clinic start generated code]*/ |
---|
382 | n/a | |
---|
383 | n/a | static PyObject * |
---|
384 | n/a | _asyncio_Future_result_impl(FutureObj *self) |
---|
385 | n/a | /*[clinic end generated code: output=f35f940936a4b1e5 input=49ecf9cf5ec50dc5]*/ |
---|
386 | n/a | { |
---|
387 | n/a | PyObject *result; |
---|
388 | n/a | int res = future_get_result(self, &result); |
---|
389 | n/a | |
---|
390 | n/a | if (res == -1) { |
---|
391 | n/a | return NULL; |
---|
392 | n/a | } |
---|
393 | n/a | |
---|
394 | n/a | if (res == 0) { |
---|
395 | n/a | return result; |
---|
396 | n/a | } |
---|
397 | n/a | |
---|
398 | n/a | assert(res == 1); |
---|
399 | n/a | |
---|
400 | n/a | PyErr_SetObject(PyExceptionInstance_Class(result), result); |
---|
401 | n/a | Py_DECREF(result); |
---|
402 | n/a | return NULL; |
---|
403 | n/a | } |
---|
404 | n/a | |
---|
405 | n/a | /*[clinic input] |
---|
406 | n/a | _asyncio.Future.exception |
---|
407 | n/a | |
---|
408 | n/a | Return the exception that was set on this future. |
---|
409 | n/a | |
---|
410 | n/a | The exception (or None if no exception was set) is returned only if |
---|
411 | n/a | the future is done. If the future has been cancelled, raises |
---|
412 | n/a | CancelledError. If the future isn't done yet, raises |
---|
413 | n/a | InvalidStateError. |
---|
414 | n/a | [clinic start generated code]*/ |
---|
415 | n/a | |
---|
416 | n/a | static PyObject * |
---|
417 | n/a | _asyncio_Future_exception_impl(FutureObj *self) |
---|
418 | n/a | /*[clinic end generated code: output=88b20d4f855e0710 input=733547a70c841c68]*/ |
---|
419 | n/a | { |
---|
420 | n/a | if (self->fut_state == STATE_CANCELLED) { |
---|
421 | n/a | PyErr_SetString(asyncio_CancelledError, ""); |
---|
422 | n/a | return NULL; |
---|
423 | n/a | } |
---|
424 | n/a | |
---|
425 | n/a | if (self->fut_state != STATE_FINISHED) { |
---|
426 | n/a | PyErr_SetString(asyncio_InvalidStateError, "Result is not ready."); |
---|
427 | n/a | return NULL; |
---|
428 | n/a | } |
---|
429 | n/a | |
---|
430 | n/a | if (self->fut_exception != NULL) { |
---|
431 | n/a | self->fut_log_tb = 0; |
---|
432 | n/a | Py_INCREF(self->fut_exception); |
---|
433 | n/a | return self->fut_exception; |
---|
434 | n/a | } |
---|
435 | n/a | |
---|
436 | n/a | Py_RETURN_NONE; |
---|
437 | n/a | } |
---|
438 | n/a | |
---|
439 | n/a | /*[clinic input] |
---|
440 | n/a | _asyncio.Future.set_result |
---|
441 | n/a | |
---|
442 | n/a | res: 'O' |
---|
443 | n/a | / |
---|
444 | n/a | |
---|
445 | n/a | Mark the future done and set its result. |
---|
446 | n/a | |
---|
447 | n/a | If the future is already done when this method is called, raises |
---|
448 | n/a | InvalidStateError. |
---|
449 | n/a | [clinic start generated code]*/ |
---|
450 | n/a | |
---|
451 | n/a | static PyObject * |
---|
452 | n/a | _asyncio_Future_set_result(FutureObj *self, PyObject *res) |
---|
453 | n/a | /*[clinic end generated code: output=a620abfc2796bfb6 input=8619565e0503357e]*/ |
---|
454 | n/a | { |
---|
455 | n/a | return future_set_result(self, res); |
---|
456 | n/a | } |
---|
457 | n/a | |
---|
458 | n/a | /*[clinic input] |
---|
459 | n/a | _asyncio.Future.set_exception |
---|
460 | n/a | |
---|
461 | n/a | exception: 'O' |
---|
462 | n/a | / |
---|
463 | n/a | |
---|
464 | n/a | Mark the future done and set an exception. |
---|
465 | n/a | |
---|
466 | n/a | If the future is already done when this method is called, raises |
---|
467 | n/a | InvalidStateError. |
---|
468 | n/a | [clinic start generated code]*/ |
---|
469 | n/a | |
---|
470 | n/a | static PyObject * |
---|
471 | n/a | _asyncio_Future_set_exception(FutureObj *self, PyObject *exception) |
---|
472 | n/a | /*[clinic end generated code: output=f1c1b0cd321be360 input=1377dbe15e6ea186]*/ |
---|
473 | n/a | { |
---|
474 | n/a | return future_set_exception(self, exception); |
---|
475 | n/a | } |
---|
476 | n/a | |
---|
477 | n/a | /*[clinic input] |
---|
478 | n/a | _asyncio.Future.add_done_callback |
---|
479 | n/a | |
---|
480 | n/a | fn: 'O' |
---|
481 | n/a | / |
---|
482 | n/a | |
---|
483 | n/a | Add a callback to be run when the future becomes done. |
---|
484 | n/a | |
---|
485 | n/a | The callback is called with a single argument - the future object. If |
---|
486 | n/a | the future is already done when this is called, the callback is |
---|
487 | n/a | scheduled with call_soon. |
---|
488 | n/a | [clinic start generated code]*/ |
---|
489 | n/a | |
---|
490 | n/a | static PyObject * |
---|
491 | n/a | _asyncio_Future_add_done_callback(FutureObj *self, PyObject *fn) |
---|
492 | n/a | /*[clinic end generated code: output=819e09629b2ec2b5 input=8cce187e32cec6a8]*/ |
---|
493 | n/a | { |
---|
494 | n/a | return future_add_done_callback(self, fn); |
---|
495 | n/a | } |
---|
496 | n/a | |
---|
497 | n/a | /*[clinic input] |
---|
498 | n/a | _asyncio.Future.remove_done_callback |
---|
499 | n/a | |
---|
500 | n/a | fn: 'O' |
---|
501 | n/a | / |
---|
502 | n/a | |
---|
503 | n/a | Remove all instances of a callback from the "call when done" list. |
---|
504 | n/a | |
---|
505 | n/a | Returns the number of callbacks removed. |
---|
506 | n/a | [clinic start generated code]*/ |
---|
507 | n/a | |
---|
508 | n/a | static PyObject * |
---|
509 | n/a | _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) |
---|
510 | n/a | /*[clinic end generated code: output=5ab1fb52b24ef31f input=3fedb73e1409c31c]*/ |
---|
511 | n/a | { |
---|
512 | n/a | PyObject *newlist; |
---|
513 | n/a | Py_ssize_t len, i, j=0; |
---|
514 | n/a | |
---|
515 | n/a | len = PyList_GET_SIZE(self->fut_callbacks); |
---|
516 | n/a | if (len == 0) { |
---|
517 | n/a | return PyLong_FromSsize_t(0); |
---|
518 | n/a | } |
---|
519 | n/a | |
---|
520 | n/a | newlist = PyList_New(len); |
---|
521 | n/a | if (newlist == NULL) { |
---|
522 | n/a | return NULL; |
---|
523 | n/a | } |
---|
524 | n/a | |
---|
525 | n/a | for (i = 0; i < len; i++) { |
---|
526 | n/a | int ret; |
---|
527 | n/a | PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i); |
---|
528 | n/a | |
---|
529 | n/a | if ((ret = PyObject_RichCompareBool(fn, item, Py_EQ)) < 0) { |
---|
530 | n/a | goto fail; |
---|
531 | n/a | } |
---|
532 | n/a | if (ret == 0) { |
---|
533 | n/a | Py_INCREF(item); |
---|
534 | n/a | PyList_SET_ITEM(newlist, j, item); |
---|
535 | n/a | j++; |
---|
536 | n/a | } |
---|
537 | n/a | } |
---|
538 | n/a | |
---|
539 | n/a | if (PyList_SetSlice(newlist, j, len, NULL) < 0) { |
---|
540 | n/a | goto fail; |
---|
541 | n/a | } |
---|
542 | n/a | if (PyList_SetSlice(self->fut_callbacks, 0, len, newlist) < 0) { |
---|
543 | n/a | goto fail; |
---|
544 | n/a | } |
---|
545 | n/a | Py_DECREF(newlist); |
---|
546 | n/a | return PyLong_FromSsize_t(len - j); |
---|
547 | n/a | |
---|
548 | n/a | fail: |
---|
549 | n/a | Py_DECREF(newlist); |
---|
550 | n/a | return NULL; |
---|
551 | n/a | } |
---|
552 | n/a | |
---|
553 | n/a | /*[clinic input] |
---|
554 | n/a | _asyncio.Future.cancel |
---|
555 | n/a | |
---|
556 | n/a | Cancel the future and schedule callbacks. |
---|
557 | n/a | |
---|
558 | n/a | If the future is already done or cancelled, return False. Otherwise, |
---|
559 | n/a | change the future's state to cancelled, schedule the callbacks and |
---|
560 | n/a | return True. |
---|
561 | n/a | [clinic start generated code]*/ |
---|
562 | n/a | |
---|
563 | n/a | static PyObject * |
---|
564 | n/a | _asyncio_Future_cancel_impl(FutureObj *self) |
---|
565 | n/a | /*[clinic end generated code: output=e45b932ba8bd68a1 input=515709a127995109]*/ |
---|
566 | n/a | { |
---|
567 | n/a | return future_cancel(self); |
---|
568 | n/a | } |
---|
569 | n/a | |
---|
570 | n/a | /*[clinic input] |
---|
571 | n/a | _asyncio.Future.cancelled |
---|
572 | n/a | |
---|
573 | n/a | Return True if the future was cancelled. |
---|
574 | n/a | [clinic start generated code]*/ |
---|
575 | n/a | |
---|
576 | n/a | static PyObject * |
---|
577 | n/a | _asyncio_Future_cancelled_impl(FutureObj *self) |
---|
578 | n/a | /*[clinic end generated code: output=145197ced586357d input=943ab8b7b7b17e45]*/ |
---|
579 | n/a | { |
---|
580 | n/a | if (self->fut_state == STATE_CANCELLED) { |
---|
581 | n/a | Py_RETURN_TRUE; |
---|
582 | n/a | } |
---|
583 | n/a | else { |
---|
584 | n/a | Py_RETURN_FALSE; |
---|
585 | n/a | } |
---|
586 | n/a | } |
---|
587 | n/a | |
---|
588 | n/a | /*[clinic input] |
---|
589 | n/a | _asyncio.Future.done |
---|
590 | n/a | |
---|
591 | n/a | Return True if the future is done. |
---|
592 | n/a | |
---|
593 | n/a | Done means either that a result / exception are available, or that the |
---|
594 | n/a | future was cancelled. |
---|
595 | n/a | [clinic start generated code]*/ |
---|
596 | n/a | |
---|
597 | n/a | static PyObject * |
---|
598 | n/a | _asyncio_Future_done_impl(FutureObj *self) |
---|
599 | n/a | /*[clinic end generated code: output=244c5ac351145096 input=28d7b23fdb65d2ac]*/ |
---|
600 | n/a | { |
---|
601 | n/a | if (self->fut_state == STATE_PENDING) { |
---|
602 | n/a | Py_RETURN_FALSE; |
---|
603 | n/a | } |
---|
604 | n/a | else { |
---|
605 | n/a | Py_RETURN_TRUE; |
---|
606 | n/a | } |
---|
607 | n/a | } |
---|
608 | n/a | |
---|
609 | n/a | static PyObject * |
---|
610 | n/a | FutureObj_get_blocking(FutureObj *fut) |
---|
611 | n/a | { |
---|
612 | n/a | if (fut->fut_blocking) { |
---|
613 | n/a | Py_RETURN_TRUE; |
---|
614 | n/a | } |
---|
615 | n/a | else { |
---|
616 | n/a | Py_RETURN_FALSE; |
---|
617 | n/a | } |
---|
618 | n/a | } |
---|
619 | n/a | |
---|
620 | n/a | static int |
---|
621 | n/a | FutureObj_set_blocking(FutureObj *fut, PyObject *val) |
---|
622 | n/a | { |
---|
623 | n/a | int is_true = PyObject_IsTrue(val); |
---|
624 | n/a | if (is_true < 0) { |
---|
625 | n/a | return -1; |
---|
626 | n/a | } |
---|
627 | n/a | fut->fut_blocking = is_true; |
---|
628 | n/a | return 0; |
---|
629 | n/a | } |
---|
630 | n/a | |
---|
631 | n/a | static PyObject * |
---|
632 | n/a | FutureObj_get_log_traceback(FutureObj *fut) |
---|
633 | n/a | { |
---|
634 | n/a | if (fut->fut_log_tb) { |
---|
635 | n/a | Py_RETURN_TRUE; |
---|
636 | n/a | } |
---|
637 | n/a | else { |
---|
638 | n/a | Py_RETURN_FALSE; |
---|
639 | n/a | } |
---|
640 | n/a | } |
---|
641 | n/a | |
---|
642 | n/a | static PyObject * |
---|
643 | n/a | FutureObj_get_loop(FutureObj *fut) |
---|
644 | n/a | { |
---|
645 | n/a | if (fut->fut_loop == NULL) { |
---|
646 | n/a | Py_RETURN_NONE; |
---|
647 | n/a | } |
---|
648 | n/a | Py_INCREF(fut->fut_loop); |
---|
649 | n/a | return fut->fut_loop; |
---|
650 | n/a | } |
---|
651 | n/a | |
---|
652 | n/a | static PyObject * |
---|
653 | n/a | FutureObj_get_callbacks(FutureObj *fut) |
---|
654 | n/a | { |
---|
655 | n/a | if (fut->fut_callbacks == NULL) { |
---|
656 | n/a | Py_RETURN_NONE; |
---|
657 | n/a | } |
---|
658 | n/a | Py_INCREF(fut->fut_callbacks); |
---|
659 | n/a | return fut->fut_callbacks; |
---|
660 | n/a | } |
---|
661 | n/a | |
---|
662 | n/a | static PyObject * |
---|
663 | n/a | FutureObj_get_result(FutureObj *fut) |
---|
664 | n/a | { |
---|
665 | n/a | if (fut->fut_result == NULL) { |
---|
666 | n/a | Py_RETURN_NONE; |
---|
667 | n/a | } |
---|
668 | n/a | Py_INCREF(fut->fut_result); |
---|
669 | n/a | return fut->fut_result; |
---|
670 | n/a | } |
---|
671 | n/a | |
---|
672 | n/a | static PyObject * |
---|
673 | n/a | FutureObj_get_exception(FutureObj *fut) |
---|
674 | n/a | { |
---|
675 | n/a | if (fut->fut_exception == NULL) { |
---|
676 | n/a | Py_RETURN_NONE; |
---|
677 | n/a | } |
---|
678 | n/a | Py_INCREF(fut->fut_exception); |
---|
679 | n/a | return fut->fut_exception; |
---|
680 | n/a | } |
---|
681 | n/a | |
---|
682 | n/a | static PyObject * |
---|
683 | n/a | FutureObj_get_source_traceback(FutureObj *fut) |
---|
684 | n/a | { |
---|
685 | n/a | if (fut->fut_source_tb == NULL) { |
---|
686 | n/a | Py_RETURN_NONE; |
---|
687 | n/a | } |
---|
688 | n/a | Py_INCREF(fut->fut_source_tb); |
---|
689 | n/a | return fut->fut_source_tb; |
---|
690 | n/a | } |
---|
691 | n/a | |
---|
692 | n/a | static PyObject * |
---|
693 | n/a | FutureObj_get_state(FutureObj *fut) |
---|
694 | n/a | { |
---|
695 | n/a | _Py_IDENTIFIER(PENDING); |
---|
696 | n/a | _Py_IDENTIFIER(CANCELLED); |
---|
697 | n/a | _Py_IDENTIFIER(FINISHED); |
---|
698 | n/a | PyObject *ret = NULL; |
---|
699 | n/a | |
---|
700 | n/a | switch (fut->fut_state) { |
---|
701 | n/a | case STATE_PENDING: |
---|
702 | n/a | ret = _PyUnicode_FromId(&PyId_PENDING); |
---|
703 | n/a | break; |
---|
704 | n/a | case STATE_CANCELLED: |
---|
705 | n/a | ret = _PyUnicode_FromId(&PyId_CANCELLED); |
---|
706 | n/a | break; |
---|
707 | n/a | case STATE_FINISHED: |
---|
708 | n/a | ret = _PyUnicode_FromId(&PyId_FINISHED); |
---|
709 | n/a | break; |
---|
710 | n/a | default: |
---|
711 | n/a | assert (0); |
---|
712 | n/a | } |
---|
713 | n/a | Py_INCREF(ret); |
---|
714 | n/a | return ret; |
---|
715 | n/a | } |
---|
716 | n/a | |
---|
717 | n/a | /*[clinic input] |
---|
718 | n/a | _asyncio.Future._repr_info |
---|
719 | n/a | [clinic start generated code]*/ |
---|
720 | n/a | |
---|
721 | n/a | static PyObject * |
---|
722 | n/a | _asyncio_Future__repr_info_impl(FutureObj *self) |
---|
723 | n/a | /*[clinic end generated code: output=fa69e901bd176cfb input=f21504d8e2ae1ca2]*/ |
---|
724 | n/a | { |
---|
725 | n/a | return PyObject_CallFunctionObjArgs( |
---|
726 | n/a | asyncio_future_repr_info_func, self, NULL); |
---|
727 | n/a | } |
---|
728 | n/a | |
---|
729 | n/a | /*[clinic input] |
---|
730 | n/a | _asyncio.Future._schedule_callbacks |
---|
731 | n/a | [clinic start generated code]*/ |
---|
732 | n/a | |
---|
733 | n/a | static PyObject * |
---|
734 | n/a | _asyncio_Future__schedule_callbacks_impl(FutureObj *self) |
---|
735 | n/a | /*[clinic end generated code: output=5e8958d89ea1c5dc input=4f5f295f263f4a88]*/ |
---|
736 | n/a | { |
---|
737 | n/a | int ret = future_schedule_callbacks(self); |
---|
738 | n/a | if (ret == -1) { |
---|
739 | n/a | return NULL; |
---|
740 | n/a | } |
---|
741 | n/a | Py_RETURN_NONE; |
---|
742 | n/a | } |
---|
743 | n/a | |
---|
744 | n/a | static PyObject * |
---|
745 | n/a | FutureObj_repr(FutureObj *fut) |
---|
746 | n/a | { |
---|
747 | n/a | _Py_IDENTIFIER(_repr_info); |
---|
748 | n/a | |
---|
749 | n/a | PyObject *_repr_info = _PyUnicode_FromId(&PyId__repr_info); // borrowed |
---|
750 | n/a | if (_repr_info == NULL) { |
---|
751 | n/a | return NULL; |
---|
752 | n/a | } |
---|
753 | n/a | |
---|
754 | n/a | PyObject *rinfo = PyObject_CallMethodObjArgs((PyObject*)fut, _repr_info, |
---|
755 | n/a | NULL); |
---|
756 | n/a | if (rinfo == NULL) { |
---|
757 | n/a | return NULL; |
---|
758 | n/a | } |
---|
759 | n/a | |
---|
760 | n/a | PyObject *sp = PyUnicode_FromString(" "); |
---|
761 | n/a | if (sp == NULL) { |
---|
762 | n/a | Py_DECREF(rinfo); |
---|
763 | n/a | return NULL; |
---|
764 | n/a | } |
---|
765 | n/a | |
---|
766 | n/a | PyObject *rinfo_s = PyUnicode_Join(sp, rinfo); |
---|
767 | n/a | Py_DECREF(sp); |
---|
768 | n/a | Py_DECREF(rinfo); |
---|
769 | n/a | if (rinfo_s == NULL) { |
---|
770 | n/a | return NULL; |
---|
771 | n/a | } |
---|
772 | n/a | |
---|
773 | n/a | PyObject *rstr = NULL; |
---|
774 | n/a | PyObject *type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut), |
---|
775 | n/a | "__name__"); |
---|
776 | n/a | if (type_name != NULL) { |
---|
777 | n/a | rstr = PyUnicode_FromFormat("<%S %S>", type_name, rinfo_s); |
---|
778 | n/a | Py_DECREF(type_name); |
---|
779 | n/a | } |
---|
780 | n/a | Py_DECREF(rinfo_s); |
---|
781 | n/a | return rstr; |
---|
782 | n/a | } |
---|
783 | n/a | |
---|
784 | n/a | static void |
---|
785 | n/a | FutureObj_finalize(FutureObj *fut) |
---|
786 | n/a | { |
---|
787 | n/a | _Py_IDENTIFIER(call_exception_handler); |
---|
788 | n/a | _Py_IDENTIFIER(message); |
---|
789 | n/a | _Py_IDENTIFIER(exception); |
---|
790 | n/a | _Py_IDENTIFIER(future); |
---|
791 | n/a | _Py_IDENTIFIER(source_traceback); |
---|
792 | n/a | |
---|
793 | n/a | if (!fut->fut_log_tb) { |
---|
794 | n/a | return; |
---|
795 | n/a | } |
---|
796 | n/a | assert(fut->fut_exception != NULL); |
---|
797 | n/a | fut->fut_log_tb = 0;; |
---|
798 | n/a | |
---|
799 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
800 | n/a | /* Save the current exception, if any. */ |
---|
801 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
802 | n/a | |
---|
803 | n/a | PyObject *context = NULL; |
---|
804 | n/a | PyObject *type_name = NULL; |
---|
805 | n/a | PyObject *message = NULL; |
---|
806 | n/a | PyObject *func = NULL; |
---|
807 | n/a | PyObject *res = NULL; |
---|
808 | n/a | |
---|
809 | n/a | context = PyDict_New(); |
---|
810 | n/a | if (context == NULL) { |
---|
811 | n/a | goto finally; |
---|
812 | n/a | } |
---|
813 | n/a | |
---|
814 | n/a | type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut), "__name__"); |
---|
815 | n/a | if (type_name == NULL) { |
---|
816 | n/a | goto finally; |
---|
817 | n/a | } |
---|
818 | n/a | |
---|
819 | n/a | message = PyUnicode_FromFormat( |
---|
820 | n/a | "%S exception was never retrieved", type_name); |
---|
821 | n/a | if (message == NULL) { |
---|
822 | n/a | goto finally; |
---|
823 | n/a | } |
---|
824 | n/a | |
---|
825 | n/a | if (_PyDict_SetItemId(context, &PyId_message, message) < 0 || |
---|
826 | n/a | _PyDict_SetItemId(context, &PyId_exception, fut->fut_exception) < 0 || |
---|
827 | n/a | _PyDict_SetItemId(context, &PyId_future, (PyObject*)fut) < 0) { |
---|
828 | n/a | goto finally; |
---|
829 | n/a | } |
---|
830 | n/a | if (fut->fut_source_tb != NULL) { |
---|
831 | n/a | if (_PyDict_SetItemId(context, &PyId_source_traceback, |
---|
832 | n/a | fut->fut_source_tb) < 0) { |
---|
833 | n/a | goto finally; |
---|
834 | n/a | } |
---|
835 | n/a | } |
---|
836 | n/a | |
---|
837 | n/a | func = _PyObject_GetAttrId(fut->fut_loop, &PyId_call_exception_handler); |
---|
838 | n/a | if (func != NULL) { |
---|
839 | n/a | res = PyObject_CallFunctionObjArgs(func, context, NULL); |
---|
840 | n/a | if (res == NULL) { |
---|
841 | n/a | PyErr_WriteUnraisable(func); |
---|
842 | n/a | } |
---|
843 | n/a | } |
---|
844 | n/a | |
---|
845 | n/a | finally: |
---|
846 | n/a | Py_CLEAR(context); |
---|
847 | n/a | Py_CLEAR(type_name); |
---|
848 | n/a | Py_CLEAR(message); |
---|
849 | n/a | Py_CLEAR(func); |
---|
850 | n/a | Py_CLEAR(res); |
---|
851 | n/a | |
---|
852 | n/a | /* Restore the saved exception. */ |
---|
853 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
854 | n/a | } |
---|
855 | n/a | |
---|
856 | n/a | |
---|
857 | n/a | static PyAsyncMethods FutureType_as_async = { |
---|
858 | n/a | (unaryfunc)future_new_iter, /* am_await */ |
---|
859 | n/a | 0, /* am_aiter */ |
---|
860 | n/a | 0 /* am_anext */ |
---|
861 | n/a | }; |
---|
862 | n/a | |
---|
863 | n/a | static PyMethodDef FutureType_methods[] = { |
---|
864 | n/a | _ASYNCIO_FUTURE_RESULT_METHODDEF |
---|
865 | n/a | _ASYNCIO_FUTURE_EXCEPTION_METHODDEF |
---|
866 | n/a | _ASYNCIO_FUTURE_SET_RESULT_METHODDEF |
---|
867 | n/a | _ASYNCIO_FUTURE_SET_EXCEPTION_METHODDEF |
---|
868 | n/a | _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF |
---|
869 | n/a | _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF |
---|
870 | n/a | _ASYNCIO_FUTURE_CANCEL_METHODDEF |
---|
871 | n/a | _ASYNCIO_FUTURE_CANCELLED_METHODDEF |
---|
872 | n/a | _ASYNCIO_FUTURE_DONE_METHODDEF |
---|
873 | n/a | _ASYNCIO_FUTURE__REPR_INFO_METHODDEF |
---|
874 | n/a | _ASYNCIO_FUTURE__SCHEDULE_CALLBACKS_METHODDEF |
---|
875 | n/a | {NULL, NULL} /* Sentinel */ |
---|
876 | n/a | }; |
---|
877 | n/a | |
---|
878 | n/a | #define FUTURE_COMMON_GETSETLIST \ |
---|
879 | n/a | {"_state", (getter)FutureObj_get_state, NULL, NULL}, \ |
---|
880 | n/a | {"_asyncio_future_blocking", (getter)FutureObj_get_blocking, \ |
---|
881 | n/a | (setter)FutureObj_set_blocking, NULL}, \ |
---|
882 | n/a | {"_loop", (getter)FutureObj_get_loop, NULL, NULL}, \ |
---|
883 | n/a | {"_callbacks", (getter)FutureObj_get_callbacks, NULL, NULL}, \ |
---|
884 | n/a | {"_result", (getter)FutureObj_get_result, NULL, NULL}, \ |
---|
885 | n/a | {"_exception", (getter)FutureObj_get_exception, NULL, NULL}, \ |
---|
886 | n/a | {"_log_traceback", (getter)FutureObj_get_log_traceback, NULL, NULL}, \ |
---|
887 | n/a | {"_source_traceback", (getter)FutureObj_get_source_traceback, NULL, NULL}, |
---|
888 | n/a | |
---|
889 | n/a | static PyGetSetDef FutureType_getsetlist[] = { |
---|
890 | n/a | FUTURE_COMMON_GETSETLIST |
---|
891 | n/a | {NULL} /* Sentinel */ |
---|
892 | n/a | }; |
---|
893 | n/a | |
---|
894 | n/a | static void FutureObj_dealloc(PyObject *self); |
---|
895 | n/a | |
---|
896 | n/a | static PyTypeObject FutureType = { |
---|
897 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
898 | n/a | "_asyncio.Future", |
---|
899 | n/a | sizeof(FutureObj), /* tp_basicsize */ |
---|
900 | n/a | .tp_dealloc = FutureObj_dealloc, |
---|
901 | n/a | .tp_as_async = &FutureType_as_async, |
---|
902 | n/a | .tp_repr = (reprfunc)FutureObj_repr, |
---|
903 | n/a | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
---|
904 | n/a | | Py_TPFLAGS_HAVE_FINALIZE, |
---|
905 | n/a | .tp_doc = _asyncio_Future___init____doc__, |
---|
906 | n/a | .tp_traverse = (traverseproc)FutureObj_traverse, |
---|
907 | n/a | .tp_clear = (inquiry)FutureObj_clear, |
---|
908 | n/a | .tp_weaklistoffset = offsetof(FutureObj, fut_weakreflist), |
---|
909 | n/a | .tp_iter = (getiterfunc)future_new_iter, |
---|
910 | n/a | .tp_methods = FutureType_methods, |
---|
911 | n/a | .tp_getset = FutureType_getsetlist, |
---|
912 | n/a | .tp_dictoffset = offsetof(FutureObj, dict), |
---|
913 | n/a | .tp_init = (initproc)_asyncio_Future___init__, |
---|
914 | n/a | .tp_new = PyType_GenericNew, |
---|
915 | n/a | .tp_finalize = (destructor)FutureObj_finalize, |
---|
916 | n/a | }; |
---|
917 | n/a | |
---|
918 | n/a | #define Future_CheckExact(obj) (Py_TYPE(obj) == &FutureType) |
---|
919 | n/a | |
---|
920 | n/a | static inline int |
---|
921 | n/a | future_call_schedule_callbacks(FutureObj *fut) |
---|
922 | n/a | { |
---|
923 | n/a | if (Future_CheckExact(fut)) { |
---|
924 | n/a | return future_schedule_callbacks(fut); |
---|
925 | n/a | } |
---|
926 | n/a | else { |
---|
927 | n/a | /* `fut` is a subclass of Future */ |
---|
928 | n/a | PyObject *ret = _PyObject_CallMethodId( |
---|
929 | n/a | (PyObject*)fut, &PyId__schedule_callbacks, NULL); |
---|
930 | n/a | if (ret == NULL) { |
---|
931 | n/a | return -1; |
---|
932 | n/a | } |
---|
933 | n/a | |
---|
934 | n/a | Py_DECREF(ret); |
---|
935 | n/a | return 0; |
---|
936 | n/a | } |
---|
937 | n/a | } |
---|
938 | n/a | |
---|
939 | n/a | static void |
---|
940 | n/a | FutureObj_dealloc(PyObject *self) |
---|
941 | n/a | { |
---|
942 | n/a | FutureObj *fut = (FutureObj *)self; |
---|
943 | n/a | |
---|
944 | n/a | if (Future_CheckExact(fut)) { |
---|
945 | n/a | /* When fut is subclass of Future, finalizer is called from |
---|
946 | n/a | * subtype_dealloc. |
---|
947 | n/a | */ |
---|
948 | n/a | if (PyObject_CallFinalizerFromDealloc(self) < 0) { |
---|
949 | n/a | // resurrected. |
---|
950 | n/a | return; |
---|
951 | n/a | } |
---|
952 | n/a | } |
---|
953 | n/a | |
---|
954 | n/a | if (fut->fut_weakreflist != NULL) { |
---|
955 | n/a | PyObject_ClearWeakRefs(self); |
---|
956 | n/a | } |
---|
957 | n/a | |
---|
958 | n/a | (void)FutureObj_clear(fut); |
---|
959 | n/a | Py_TYPE(fut)->tp_free(fut); |
---|
960 | n/a | } |
---|
961 | n/a | |
---|
962 | n/a | |
---|
963 | n/a | /*********************** Future Iterator **************************/ |
---|
964 | n/a | |
---|
965 | n/a | typedef struct { |
---|
966 | n/a | PyObject_HEAD |
---|
967 | n/a | FutureObj *future; |
---|
968 | n/a | } futureiterobject; |
---|
969 | n/a | |
---|
970 | n/a | static void |
---|
971 | n/a | FutureIter_dealloc(futureiterobject *it) |
---|
972 | n/a | { |
---|
973 | n/a | PyObject_GC_UnTrack(it); |
---|
974 | n/a | Py_XDECREF(it->future); |
---|
975 | n/a | PyObject_GC_Del(it); |
---|
976 | n/a | } |
---|
977 | n/a | |
---|
978 | n/a | static PyObject * |
---|
979 | n/a | FutureIter_iternext(futureiterobject *it) |
---|
980 | n/a | { |
---|
981 | n/a | PyObject *res; |
---|
982 | n/a | FutureObj *fut = it->future; |
---|
983 | n/a | |
---|
984 | n/a | if (fut == NULL) { |
---|
985 | n/a | return NULL; |
---|
986 | n/a | } |
---|
987 | n/a | |
---|
988 | n/a | if (fut->fut_state == STATE_PENDING) { |
---|
989 | n/a | if (!fut->fut_blocking) { |
---|
990 | n/a | fut->fut_blocking = 1; |
---|
991 | n/a | Py_INCREF(fut); |
---|
992 | n/a | return (PyObject *)fut; |
---|
993 | n/a | } |
---|
994 | n/a | PyErr_Format(PyExc_AssertionError, |
---|
995 | n/a | "yield from wasn't used with future"); |
---|
996 | n/a | return NULL; |
---|
997 | n/a | } |
---|
998 | n/a | |
---|
999 | n/a | res = _asyncio_Future_result_impl(fut); |
---|
1000 | n/a | if (res != NULL) { |
---|
1001 | n/a | /* The result of the Future is not an exception. */ |
---|
1002 | n/a | if (_PyGen_SetStopIterationValue(res) < 0) { |
---|
1003 | n/a | Py_DECREF(res); |
---|
1004 | n/a | return NULL; |
---|
1005 | n/a | } |
---|
1006 | n/a | Py_DECREF(res); |
---|
1007 | n/a | } |
---|
1008 | n/a | |
---|
1009 | n/a | it->future = NULL; |
---|
1010 | n/a | Py_DECREF(fut); |
---|
1011 | n/a | return NULL; |
---|
1012 | n/a | } |
---|
1013 | n/a | |
---|
1014 | n/a | static PyObject * |
---|
1015 | n/a | FutureIter_send(futureiterobject *self, PyObject *unused) |
---|
1016 | n/a | { |
---|
1017 | n/a | /* Future.__iter__ doesn't care about values that are pushed to the |
---|
1018 | n/a | * generator, it just returns "self.result(). |
---|
1019 | n/a | */ |
---|
1020 | n/a | return FutureIter_iternext(self); |
---|
1021 | n/a | } |
---|
1022 | n/a | |
---|
1023 | n/a | static PyObject * |
---|
1024 | n/a | FutureIter_throw(futureiterobject *self, PyObject *args) |
---|
1025 | n/a | { |
---|
1026 | n/a | PyObject *type=NULL, *val=NULL, *tb=NULL; |
---|
1027 | n/a | if (!PyArg_ParseTuple(args, "O|OO", &type, &val, &tb)) |
---|
1028 | n/a | return NULL; |
---|
1029 | n/a | |
---|
1030 | n/a | if (val == Py_None) { |
---|
1031 | n/a | val = NULL; |
---|
1032 | n/a | } |
---|
1033 | n/a | if (tb == Py_None) { |
---|
1034 | n/a | tb = NULL; |
---|
1035 | n/a | } else if (tb != NULL && !PyTraceBack_Check(tb)) { |
---|
1036 | n/a | PyErr_SetString(PyExc_TypeError, "throw() third argument must be a traceback"); |
---|
1037 | n/a | return NULL; |
---|
1038 | n/a | } |
---|
1039 | n/a | |
---|
1040 | n/a | Py_INCREF(type); |
---|
1041 | n/a | Py_XINCREF(val); |
---|
1042 | n/a | Py_XINCREF(tb); |
---|
1043 | n/a | |
---|
1044 | n/a | if (PyExceptionClass_Check(type)) { |
---|
1045 | n/a | PyErr_NormalizeException(&type, &val, &tb); |
---|
1046 | n/a | /* No need to call PyException_SetTraceback since we'll be calling |
---|
1047 | n/a | PyErr_Restore for `type`, `val`, and `tb`. */ |
---|
1048 | n/a | } else if (PyExceptionInstance_Check(type)) { |
---|
1049 | n/a | if (val) { |
---|
1050 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1051 | n/a | "instance exception may not have a separate value"); |
---|
1052 | n/a | goto fail; |
---|
1053 | n/a | } |
---|
1054 | n/a | val = type; |
---|
1055 | n/a | type = PyExceptionInstance_Class(type); |
---|
1056 | n/a | Py_INCREF(type); |
---|
1057 | n/a | if (tb == NULL) |
---|
1058 | n/a | tb = PyException_GetTraceback(val); |
---|
1059 | n/a | } else { |
---|
1060 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1061 | n/a | "exceptions must be classes deriving BaseException or " |
---|
1062 | n/a | "instances of such a class"); |
---|
1063 | n/a | goto fail; |
---|
1064 | n/a | } |
---|
1065 | n/a | |
---|
1066 | n/a | Py_CLEAR(self->future); |
---|
1067 | n/a | |
---|
1068 | n/a | PyErr_Restore(type, val, tb); |
---|
1069 | n/a | |
---|
1070 | n/a | return FutureIter_iternext(self); |
---|
1071 | n/a | |
---|
1072 | n/a | fail: |
---|
1073 | n/a | Py_DECREF(type); |
---|
1074 | n/a | Py_XDECREF(val); |
---|
1075 | n/a | Py_XDECREF(tb); |
---|
1076 | n/a | return NULL; |
---|
1077 | n/a | } |
---|
1078 | n/a | |
---|
1079 | n/a | static PyObject * |
---|
1080 | n/a | FutureIter_close(futureiterobject *self, PyObject *arg) |
---|
1081 | n/a | { |
---|
1082 | n/a | Py_CLEAR(self->future); |
---|
1083 | n/a | Py_RETURN_NONE; |
---|
1084 | n/a | } |
---|
1085 | n/a | |
---|
1086 | n/a | static int |
---|
1087 | n/a | FutureIter_traverse(futureiterobject *it, visitproc visit, void *arg) |
---|
1088 | n/a | { |
---|
1089 | n/a | Py_VISIT(it->future); |
---|
1090 | n/a | return 0; |
---|
1091 | n/a | } |
---|
1092 | n/a | |
---|
1093 | n/a | static PyMethodDef FutureIter_methods[] = { |
---|
1094 | n/a | {"send", (PyCFunction)FutureIter_send, METH_O, NULL}, |
---|
1095 | n/a | {"throw", (PyCFunction)FutureIter_throw, METH_VARARGS, NULL}, |
---|
1096 | n/a | {"close", (PyCFunction)FutureIter_close, METH_NOARGS, NULL}, |
---|
1097 | n/a | {NULL, NULL} /* Sentinel */ |
---|
1098 | n/a | }; |
---|
1099 | n/a | |
---|
1100 | n/a | static PyTypeObject FutureIterType = { |
---|
1101 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1102 | n/a | "_asyncio.FutureIter", |
---|
1103 | n/a | .tp_basicsize = sizeof(futureiterobject), |
---|
1104 | n/a | .tp_itemsize = 0, |
---|
1105 | n/a | .tp_dealloc = (destructor)FutureIter_dealloc, |
---|
1106 | n/a | .tp_getattro = PyObject_GenericGetAttr, |
---|
1107 | n/a | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
---|
1108 | n/a | .tp_traverse = (traverseproc)FutureIter_traverse, |
---|
1109 | n/a | .tp_iter = PyObject_SelfIter, |
---|
1110 | n/a | .tp_iternext = (iternextfunc)FutureIter_iternext, |
---|
1111 | n/a | .tp_methods = FutureIter_methods, |
---|
1112 | n/a | }; |
---|
1113 | n/a | |
---|
1114 | n/a | static PyObject * |
---|
1115 | n/a | future_new_iter(PyObject *fut) |
---|
1116 | n/a | { |
---|
1117 | n/a | futureiterobject *it; |
---|
1118 | n/a | |
---|
1119 | n/a | if (!PyObject_TypeCheck(fut, &FutureType)) { |
---|
1120 | n/a | PyErr_BadInternalCall(); |
---|
1121 | n/a | return NULL; |
---|
1122 | n/a | } |
---|
1123 | n/a | it = PyObject_GC_New(futureiterobject, &FutureIterType); |
---|
1124 | n/a | if (it == NULL) { |
---|
1125 | n/a | return NULL; |
---|
1126 | n/a | } |
---|
1127 | n/a | Py_INCREF(fut); |
---|
1128 | n/a | it->future = (FutureObj*)fut; |
---|
1129 | n/a | PyObject_GC_Track(it); |
---|
1130 | n/a | return (PyObject*)it; |
---|
1131 | n/a | } |
---|
1132 | n/a | |
---|
1133 | n/a | |
---|
1134 | n/a | /*********************** Task **************************/ |
---|
1135 | n/a | |
---|
1136 | n/a | |
---|
1137 | n/a | /*[clinic input] |
---|
1138 | n/a | class _asyncio.Task "TaskObj *" "&Task_Type" |
---|
1139 | n/a | [clinic start generated code]*/ |
---|
1140 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=719dcef0fcc03b37]*/ |
---|
1141 | n/a | |
---|
1142 | n/a | static int task_call_step_soon(TaskObj *, PyObject *); |
---|
1143 | n/a | static inline PyObject * task_call_wakeup(TaskObj *, PyObject *); |
---|
1144 | n/a | static inline PyObject * task_call_step(TaskObj *, PyObject *); |
---|
1145 | n/a | static PyObject * task_wakeup(TaskObj *, PyObject *); |
---|
1146 | n/a | static PyObject * task_step(TaskObj *, PyObject *); |
---|
1147 | n/a | |
---|
1148 | n/a | /* ----- Task._step wrapper */ |
---|
1149 | n/a | |
---|
1150 | n/a | static int |
---|
1151 | n/a | TaskSendMethWrapper_clear(TaskSendMethWrapper *o) |
---|
1152 | n/a | { |
---|
1153 | n/a | Py_CLEAR(o->sw_task); |
---|
1154 | n/a | Py_CLEAR(o->sw_arg); |
---|
1155 | n/a | return 0; |
---|
1156 | n/a | } |
---|
1157 | n/a | |
---|
1158 | n/a | static void |
---|
1159 | n/a | TaskSendMethWrapper_dealloc(TaskSendMethWrapper *o) |
---|
1160 | n/a | { |
---|
1161 | n/a | PyObject_GC_UnTrack(o); |
---|
1162 | n/a | (void)TaskSendMethWrapper_clear(o); |
---|
1163 | n/a | Py_TYPE(o)->tp_free(o); |
---|
1164 | n/a | } |
---|
1165 | n/a | |
---|
1166 | n/a | static PyObject * |
---|
1167 | n/a | TaskSendMethWrapper_call(TaskSendMethWrapper *o, |
---|
1168 | n/a | PyObject *args, PyObject *kwds) |
---|
1169 | n/a | { |
---|
1170 | n/a | return task_call_step(o->sw_task, o->sw_arg); |
---|
1171 | n/a | } |
---|
1172 | n/a | |
---|
1173 | n/a | static int |
---|
1174 | n/a | TaskSendMethWrapper_traverse(TaskSendMethWrapper *o, |
---|
1175 | n/a | visitproc visit, void *arg) |
---|
1176 | n/a | { |
---|
1177 | n/a | Py_VISIT(o->sw_task); |
---|
1178 | n/a | Py_VISIT(o->sw_arg); |
---|
1179 | n/a | return 0; |
---|
1180 | n/a | } |
---|
1181 | n/a | |
---|
1182 | n/a | static PyObject * |
---|
1183 | n/a | TaskSendMethWrapper_get___self__(TaskSendMethWrapper *o) |
---|
1184 | n/a | { |
---|
1185 | n/a | if (o->sw_task) { |
---|
1186 | n/a | Py_INCREF(o->sw_task); |
---|
1187 | n/a | return (PyObject*)o->sw_task; |
---|
1188 | n/a | } |
---|
1189 | n/a | Py_RETURN_NONE; |
---|
1190 | n/a | } |
---|
1191 | n/a | |
---|
1192 | n/a | static PyGetSetDef TaskSendMethWrapper_getsetlist[] = { |
---|
1193 | n/a | {"__self__", (getter)TaskSendMethWrapper_get___self__, NULL, NULL}, |
---|
1194 | n/a | {NULL} /* Sentinel */ |
---|
1195 | n/a | }; |
---|
1196 | n/a | |
---|
1197 | n/a | PyTypeObject TaskSendMethWrapper_Type = { |
---|
1198 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1199 | n/a | "TaskSendMethWrapper", |
---|
1200 | n/a | .tp_basicsize = sizeof(TaskSendMethWrapper), |
---|
1201 | n/a | .tp_itemsize = 0, |
---|
1202 | n/a | .tp_getset = TaskSendMethWrapper_getsetlist, |
---|
1203 | n/a | .tp_dealloc = (destructor)TaskSendMethWrapper_dealloc, |
---|
1204 | n/a | .tp_call = (ternaryfunc)TaskSendMethWrapper_call, |
---|
1205 | n/a | .tp_getattro = PyObject_GenericGetAttr, |
---|
1206 | n/a | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
---|
1207 | n/a | .tp_traverse = (traverseproc)TaskSendMethWrapper_traverse, |
---|
1208 | n/a | .tp_clear = (inquiry)TaskSendMethWrapper_clear, |
---|
1209 | n/a | }; |
---|
1210 | n/a | |
---|
1211 | n/a | static PyObject * |
---|
1212 | n/a | TaskSendMethWrapper_new(TaskObj *task, PyObject *arg) |
---|
1213 | n/a | { |
---|
1214 | n/a | TaskSendMethWrapper *o; |
---|
1215 | n/a | o = PyObject_GC_New(TaskSendMethWrapper, &TaskSendMethWrapper_Type); |
---|
1216 | n/a | if (o == NULL) { |
---|
1217 | n/a | return NULL; |
---|
1218 | n/a | } |
---|
1219 | n/a | |
---|
1220 | n/a | Py_INCREF(task); |
---|
1221 | n/a | o->sw_task = task; |
---|
1222 | n/a | |
---|
1223 | n/a | Py_XINCREF(arg); |
---|
1224 | n/a | o->sw_arg = arg; |
---|
1225 | n/a | |
---|
1226 | n/a | PyObject_GC_Track(o); |
---|
1227 | n/a | return (PyObject*) o; |
---|
1228 | n/a | } |
---|
1229 | n/a | |
---|
1230 | n/a | /* ----- Task._wakeup wrapper */ |
---|
1231 | n/a | |
---|
1232 | n/a | static PyObject * |
---|
1233 | n/a | TaskWakeupMethWrapper_call(TaskWakeupMethWrapper *o, |
---|
1234 | n/a | PyObject *args, PyObject *kwds) |
---|
1235 | n/a | { |
---|
1236 | n/a | PyObject *fut; |
---|
1237 | n/a | |
---|
1238 | n/a | if (!PyArg_ParseTuple(args, "O|", &fut)) { |
---|
1239 | n/a | return NULL; |
---|
1240 | n/a | } |
---|
1241 | n/a | |
---|
1242 | n/a | return task_call_wakeup(o->ww_task, fut); |
---|
1243 | n/a | } |
---|
1244 | n/a | |
---|
1245 | n/a | static int |
---|
1246 | n/a | TaskWakeupMethWrapper_clear(TaskWakeupMethWrapper *o) |
---|
1247 | n/a | { |
---|
1248 | n/a | Py_CLEAR(o->ww_task); |
---|
1249 | n/a | return 0; |
---|
1250 | n/a | } |
---|
1251 | n/a | |
---|
1252 | n/a | static int |
---|
1253 | n/a | TaskWakeupMethWrapper_traverse(TaskWakeupMethWrapper *o, |
---|
1254 | n/a | visitproc visit, void *arg) |
---|
1255 | n/a | { |
---|
1256 | n/a | Py_VISIT(o->ww_task); |
---|
1257 | n/a | return 0; |
---|
1258 | n/a | } |
---|
1259 | n/a | |
---|
1260 | n/a | static void |
---|
1261 | n/a | TaskWakeupMethWrapper_dealloc(TaskWakeupMethWrapper *o) |
---|
1262 | n/a | { |
---|
1263 | n/a | PyObject_GC_UnTrack(o); |
---|
1264 | n/a | (void)TaskWakeupMethWrapper_clear(o); |
---|
1265 | n/a | Py_TYPE(o)->tp_free(o); |
---|
1266 | n/a | } |
---|
1267 | n/a | |
---|
1268 | n/a | PyTypeObject TaskWakeupMethWrapper_Type = { |
---|
1269 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1270 | n/a | "TaskWakeupMethWrapper", |
---|
1271 | n/a | .tp_basicsize = sizeof(TaskWakeupMethWrapper), |
---|
1272 | n/a | .tp_itemsize = 0, |
---|
1273 | n/a | .tp_dealloc = (destructor)TaskWakeupMethWrapper_dealloc, |
---|
1274 | n/a | .tp_call = (ternaryfunc)TaskWakeupMethWrapper_call, |
---|
1275 | n/a | .tp_getattro = PyObject_GenericGetAttr, |
---|
1276 | n/a | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
---|
1277 | n/a | .tp_traverse = (traverseproc)TaskWakeupMethWrapper_traverse, |
---|
1278 | n/a | .tp_clear = (inquiry)TaskWakeupMethWrapper_clear, |
---|
1279 | n/a | }; |
---|
1280 | n/a | |
---|
1281 | n/a | static PyObject * |
---|
1282 | n/a | TaskWakeupMethWrapper_new(TaskObj *task) |
---|
1283 | n/a | { |
---|
1284 | n/a | TaskWakeupMethWrapper *o; |
---|
1285 | n/a | o = PyObject_GC_New(TaskWakeupMethWrapper, &TaskWakeupMethWrapper_Type); |
---|
1286 | n/a | if (o == NULL) { |
---|
1287 | n/a | return NULL; |
---|
1288 | n/a | } |
---|
1289 | n/a | |
---|
1290 | n/a | Py_INCREF(task); |
---|
1291 | n/a | o->ww_task = task; |
---|
1292 | n/a | |
---|
1293 | n/a | PyObject_GC_Track(o); |
---|
1294 | n/a | return (PyObject*) o; |
---|
1295 | n/a | } |
---|
1296 | n/a | |
---|
1297 | n/a | /* ----- Task */ |
---|
1298 | n/a | |
---|
1299 | n/a | /*[clinic input] |
---|
1300 | n/a | _asyncio.Task.__init__ |
---|
1301 | n/a | |
---|
1302 | n/a | coro: 'O' |
---|
1303 | n/a | * |
---|
1304 | n/a | loop: 'O' = NULL |
---|
1305 | n/a | |
---|
1306 | n/a | A coroutine wrapped in a Future. |
---|
1307 | n/a | [clinic start generated code]*/ |
---|
1308 | n/a | |
---|
1309 | n/a | static int |
---|
1310 | n/a | _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop) |
---|
1311 | n/a | /*[clinic end generated code: output=9f24774c2287fc2f input=71d8d28c201a18cd]*/ |
---|
1312 | n/a | { |
---|
1313 | n/a | PyObject *res; |
---|
1314 | n/a | _Py_IDENTIFIER(add); |
---|
1315 | n/a | |
---|
1316 | n/a | if (future_init((FutureObj*)self, loop)) { |
---|
1317 | n/a | return -1; |
---|
1318 | n/a | } |
---|
1319 | n/a | |
---|
1320 | n/a | self->task_fut_waiter = NULL; |
---|
1321 | n/a | self->task_must_cancel = 0; |
---|
1322 | n/a | self->task_log_destroy_pending = 1; |
---|
1323 | n/a | |
---|
1324 | n/a | Py_INCREF(coro); |
---|
1325 | n/a | self->task_coro = coro; |
---|
1326 | n/a | |
---|
1327 | n/a | if (task_call_step_soon(self, NULL)) { |
---|
1328 | n/a | return -1; |
---|
1329 | n/a | } |
---|
1330 | n/a | |
---|
1331 | n/a | res = _PyObject_CallMethodIdObjArgs(all_tasks, &PyId_add, self, NULL); |
---|
1332 | n/a | if (res == NULL) { |
---|
1333 | n/a | return -1; |
---|
1334 | n/a | } |
---|
1335 | n/a | Py_DECREF(res); |
---|
1336 | n/a | |
---|
1337 | n/a | return 0; |
---|
1338 | n/a | } |
---|
1339 | n/a | |
---|
1340 | n/a | static int |
---|
1341 | n/a | TaskObj_clear(TaskObj *task) |
---|
1342 | n/a | { |
---|
1343 | n/a | (void)FutureObj_clear((FutureObj*) task); |
---|
1344 | n/a | Py_CLEAR(task->task_coro); |
---|
1345 | n/a | Py_CLEAR(task->task_fut_waiter); |
---|
1346 | n/a | return 0; |
---|
1347 | n/a | } |
---|
1348 | n/a | |
---|
1349 | n/a | static int |
---|
1350 | n/a | TaskObj_traverse(TaskObj *task, visitproc visit, void *arg) |
---|
1351 | n/a | { |
---|
1352 | n/a | Py_VISIT(task->task_coro); |
---|
1353 | n/a | Py_VISIT(task->task_fut_waiter); |
---|
1354 | n/a | (void)FutureObj_traverse((FutureObj*) task, visit, arg); |
---|
1355 | n/a | return 0; |
---|
1356 | n/a | } |
---|
1357 | n/a | |
---|
1358 | n/a | static PyObject * |
---|
1359 | n/a | TaskObj_get_log_destroy_pending(TaskObj *task) |
---|
1360 | n/a | { |
---|
1361 | n/a | if (task->task_log_destroy_pending) { |
---|
1362 | n/a | Py_RETURN_TRUE; |
---|
1363 | n/a | } |
---|
1364 | n/a | else { |
---|
1365 | n/a | Py_RETURN_FALSE; |
---|
1366 | n/a | } |
---|
1367 | n/a | } |
---|
1368 | n/a | |
---|
1369 | n/a | static int |
---|
1370 | n/a | TaskObj_set_log_destroy_pending(TaskObj *task, PyObject *val) |
---|
1371 | n/a | { |
---|
1372 | n/a | int is_true = PyObject_IsTrue(val); |
---|
1373 | n/a | if (is_true < 0) { |
---|
1374 | n/a | return -1; |
---|
1375 | n/a | } |
---|
1376 | n/a | task->task_log_destroy_pending = is_true; |
---|
1377 | n/a | return 0; |
---|
1378 | n/a | } |
---|
1379 | n/a | |
---|
1380 | n/a | static PyObject * |
---|
1381 | n/a | TaskObj_get_must_cancel(TaskObj *task) |
---|
1382 | n/a | { |
---|
1383 | n/a | if (task->task_must_cancel) { |
---|
1384 | n/a | Py_RETURN_TRUE; |
---|
1385 | n/a | } |
---|
1386 | n/a | else { |
---|
1387 | n/a | Py_RETURN_FALSE; |
---|
1388 | n/a | } |
---|
1389 | n/a | } |
---|
1390 | n/a | |
---|
1391 | n/a | static PyObject * |
---|
1392 | n/a | TaskObj_get_coro(TaskObj *task) |
---|
1393 | n/a | { |
---|
1394 | n/a | if (task->task_coro) { |
---|
1395 | n/a | Py_INCREF(task->task_coro); |
---|
1396 | n/a | return task->task_coro; |
---|
1397 | n/a | } |
---|
1398 | n/a | |
---|
1399 | n/a | Py_RETURN_NONE; |
---|
1400 | n/a | } |
---|
1401 | n/a | |
---|
1402 | n/a | static PyObject * |
---|
1403 | n/a | TaskObj_get_fut_waiter(TaskObj *task) |
---|
1404 | n/a | { |
---|
1405 | n/a | if (task->task_fut_waiter) { |
---|
1406 | n/a | Py_INCREF(task->task_fut_waiter); |
---|
1407 | n/a | return task->task_fut_waiter; |
---|
1408 | n/a | } |
---|
1409 | n/a | |
---|
1410 | n/a | Py_RETURN_NONE; |
---|
1411 | n/a | } |
---|
1412 | n/a | |
---|
1413 | n/a | /*[clinic input] |
---|
1414 | n/a | @classmethod |
---|
1415 | n/a | _asyncio.Task.current_task |
---|
1416 | n/a | |
---|
1417 | n/a | loop: 'O' = NULL |
---|
1418 | n/a | |
---|
1419 | n/a | Return the currently running task in an event loop or None. |
---|
1420 | n/a | |
---|
1421 | n/a | By default the current task for the current event loop is returned. |
---|
1422 | n/a | |
---|
1423 | n/a | None is returned when called not in the context of a Task. |
---|
1424 | n/a | [clinic start generated code]*/ |
---|
1425 | n/a | |
---|
1426 | n/a | static PyObject * |
---|
1427 | n/a | _asyncio_Task_current_task_impl(PyTypeObject *type, PyObject *loop) |
---|
1428 | n/a | /*[clinic end generated code: output=99fbe7332c516e03 input=cd784537f02cf833]*/ |
---|
1429 | n/a | { |
---|
1430 | n/a | PyObject *res; |
---|
1431 | n/a | |
---|
1432 | n/a | if (loop == NULL) { |
---|
1433 | n/a | loop = _PyObject_CallNoArg(asyncio_get_event_loop); |
---|
1434 | n/a | if (loop == NULL) { |
---|
1435 | n/a | return NULL; |
---|
1436 | n/a | } |
---|
1437 | n/a | |
---|
1438 | n/a | res = PyDict_GetItem(current_tasks, loop); |
---|
1439 | n/a | Py_DECREF(loop); |
---|
1440 | n/a | } |
---|
1441 | n/a | else { |
---|
1442 | n/a | res = PyDict_GetItem(current_tasks, loop); |
---|
1443 | n/a | } |
---|
1444 | n/a | |
---|
1445 | n/a | if (res == NULL) { |
---|
1446 | n/a | Py_RETURN_NONE; |
---|
1447 | n/a | } |
---|
1448 | n/a | else { |
---|
1449 | n/a | Py_INCREF(res); |
---|
1450 | n/a | return res; |
---|
1451 | n/a | } |
---|
1452 | n/a | } |
---|
1453 | n/a | |
---|
1454 | n/a | static PyObject * |
---|
1455 | n/a | task_all_tasks(PyObject *loop) |
---|
1456 | n/a | { |
---|
1457 | n/a | PyObject *task; |
---|
1458 | n/a | PyObject *task_loop; |
---|
1459 | n/a | PyObject *set; |
---|
1460 | n/a | PyObject *iter; |
---|
1461 | n/a | |
---|
1462 | n/a | assert(loop != NULL); |
---|
1463 | n/a | |
---|
1464 | n/a | set = PySet_New(NULL); |
---|
1465 | n/a | if (set == NULL) { |
---|
1466 | n/a | return NULL; |
---|
1467 | n/a | } |
---|
1468 | n/a | |
---|
1469 | n/a | iter = PyObject_GetIter(all_tasks); |
---|
1470 | n/a | if (iter == NULL) { |
---|
1471 | n/a | goto fail; |
---|
1472 | n/a | } |
---|
1473 | n/a | |
---|
1474 | n/a | while ((task = PyIter_Next(iter))) { |
---|
1475 | n/a | task_loop = PyObject_GetAttrString(task, "_loop"); |
---|
1476 | n/a | if (task_loop == NULL) { |
---|
1477 | n/a | Py_DECREF(task); |
---|
1478 | n/a | goto fail; |
---|
1479 | n/a | } |
---|
1480 | n/a | if (task_loop == loop) { |
---|
1481 | n/a | if (PySet_Add(set, task) == -1) { |
---|
1482 | n/a | Py_DECREF(task_loop); |
---|
1483 | n/a | Py_DECREF(task); |
---|
1484 | n/a | goto fail; |
---|
1485 | n/a | } |
---|
1486 | n/a | } |
---|
1487 | n/a | Py_DECREF(task_loop); |
---|
1488 | n/a | Py_DECREF(task); |
---|
1489 | n/a | } |
---|
1490 | n/a | |
---|
1491 | n/a | Py_DECREF(iter); |
---|
1492 | n/a | return set; |
---|
1493 | n/a | |
---|
1494 | n/a | fail: |
---|
1495 | n/a | Py_XDECREF(set); |
---|
1496 | n/a | Py_XDECREF(iter); |
---|
1497 | n/a | return NULL; |
---|
1498 | n/a | } |
---|
1499 | n/a | |
---|
1500 | n/a | /*[clinic input] |
---|
1501 | n/a | @classmethod |
---|
1502 | n/a | _asyncio.Task.all_tasks |
---|
1503 | n/a | |
---|
1504 | n/a | loop: 'O' = NULL |
---|
1505 | n/a | |
---|
1506 | n/a | Return a set of all tasks for an event loop. |
---|
1507 | n/a | |
---|
1508 | n/a | By default all tasks for the current event loop are returned. |
---|
1509 | n/a | [clinic start generated code]*/ |
---|
1510 | n/a | |
---|
1511 | n/a | static PyObject * |
---|
1512 | n/a | _asyncio_Task_all_tasks_impl(PyTypeObject *type, PyObject *loop) |
---|
1513 | n/a | /*[clinic end generated code: output=11f9b20749ccca5d input=cd64aa5f88bd5c49]*/ |
---|
1514 | n/a | { |
---|
1515 | n/a | PyObject *res; |
---|
1516 | n/a | |
---|
1517 | n/a | if (loop == NULL) { |
---|
1518 | n/a | loop = _PyObject_CallNoArg(asyncio_get_event_loop); |
---|
1519 | n/a | if (loop == NULL) { |
---|
1520 | n/a | return NULL; |
---|
1521 | n/a | } |
---|
1522 | n/a | |
---|
1523 | n/a | res = task_all_tasks(loop); |
---|
1524 | n/a | Py_DECREF(loop); |
---|
1525 | n/a | } |
---|
1526 | n/a | else { |
---|
1527 | n/a | res = task_all_tasks(loop); |
---|
1528 | n/a | } |
---|
1529 | n/a | |
---|
1530 | n/a | return res; |
---|
1531 | n/a | } |
---|
1532 | n/a | |
---|
1533 | n/a | /*[clinic input] |
---|
1534 | n/a | _asyncio.Task._repr_info |
---|
1535 | n/a | [clinic start generated code]*/ |
---|
1536 | n/a | |
---|
1537 | n/a | static PyObject * |
---|
1538 | n/a | _asyncio_Task__repr_info_impl(TaskObj *self) |
---|
1539 | n/a | /*[clinic end generated code: output=6a490eb66d5ba34b input=3c6d051ed3ddec8b]*/ |
---|
1540 | n/a | { |
---|
1541 | n/a | return PyObject_CallFunctionObjArgs( |
---|
1542 | n/a | asyncio_task_repr_info_func, self, NULL); |
---|
1543 | n/a | } |
---|
1544 | n/a | |
---|
1545 | n/a | /*[clinic input] |
---|
1546 | n/a | _asyncio.Task.cancel |
---|
1547 | n/a | |
---|
1548 | n/a | Request that this task cancel itself. |
---|
1549 | n/a | |
---|
1550 | n/a | This arranges for a CancelledError to be thrown into the |
---|
1551 | n/a | wrapped coroutine on the next cycle through the event loop. |
---|
1552 | n/a | The coroutine then has a chance to clean up or even deny |
---|
1553 | n/a | the request using try/except/finally. |
---|
1554 | n/a | |
---|
1555 | n/a | Unlike Future.cancel, this does not guarantee that the |
---|
1556 | n/a | task will be cancelled: the exception might be caught and |
---|
1557 | n/a | acted upon, delaying cancellation of the task or preventing |
---|
1558 | n/a | cancellation completely. The task may also return a value or |
---|
1559 | n/a | raise a different exception. |
---|
1560 | n/a | |
---|
1561 | n/a | Immediately after this method is called, Task.cancelled() will |
---|
1562 | n/a | not return True (unless the task was already cancelled). A |
---|
1563 | n/a | task will be marked as cancelled when the wrapped coroutine |
---|
1564 | n/a | terminates with a CancelledError exception (even if cancel() |
---|
1565 | n/a | was not called). |
---|
1566 | n/a | [clinic start generated code]*/ |
---|
1567 | n/a | |
---|
1568 | n/a | static PyObject * |
---|
1569 | n/a | _asyncio_Task_cancel_impl(TaskObj *self) |
---|
1570 | n/a | /*[clinic end generated code: output=6bfc0479da9d5757 input=13f9bf496695cb52]*/ |
---|
1571 | n/a | { |
---|
1572 | n/a | if (self->task_state != STATE_PENDING) { |
---|
1573 | n/a | Py_RETURN_FALSE; |
---|
1574 | n/a | } |
---|
1575 | n/a | |
---|
1576 | n/a | if (self->task_fut_waiter) { |
---|
1577 | n/a | PyObject *res; |
---|
1578 | n/a | int is_true; |
---|
1579 | n/a | |
---|
1580 | n/a | res = _PyObject_CallMethodId( |
---|
1581 | n/a | self->task_fut_waiter, &PyId_cancel, NULL); |
---|
1582 | n/a | if (res == NULL) { |
---|
1583 | n/a | return NULL; |
---|
1584 | n/a | } |
---|
1585 | n/a | |
---|
1586 | n/a | is_true = PyObject_IsTrue(res); |
---|
1587 | n/a | Py_DECREF(res); |
---|
1588 | n/a | if (is_true < 0) { |
---|
1589 | n/a | return NULL; |
---|
1590 | n/a | } |
---|
1591 | n/a | |
---|
1592 | n/a | if (is_true) { |
---|
1593 | n/a | Py_RETURN_TRUE; |
---|
1594 | n/a | } |
---|
1595 | n/a | } |
---|
1596 | n/a | |
---|
1597 | n/a | self->task_must_cancel = 1; |
---|
1598 | n/a | Py_RETURN_TRUE; |
---|
1599 | n/a | } |
---|
1600 | n/a | |
---|
1601 | n/a | /*[clinic input] |
---|
1602 | n/a | _asyncio.Task.get_stack |
---|
1603 | n/a | |
---|
1604 | n/a | * |
---|
1605 | n/a | limit: 'O' = None |
---|
1606 | n/a | |
---|
1607 | n/a | Return the list of stack frames for this task's coroutine. |
---|
1608 | n/a | |
---|
1609 | n/a | If the coroutine is not done, this returns the stack where it is |
---|
1610 | n/a | suspended. If the coroutine has completed successfully or was |
---|
1611 | n/a | cancelled, this returns an empty list. If the coroutine was |
---|
1612 | n/a | terminated by an exception, this returns the list of traceback |
---|
1613 | n/a | frames. |
---|
1614 | n/a | |
---|
1615 | n/a | The frames are always ordered from oldest to newest. |
---|
1616 | n/a | |
---|
1617 | n/a | The optional limit gives the maximum number of frames to |
---|
1618 | n/a | return; by default all available frames are returned. Its |
---|
1619 | n/a | meaning differs depending on whether a stack or a traceback is |
---|
1620 | n/a | returned: the newest frames of a stack are returned, but the |
---|
1621 | n/a | oldest frames of a traceback are returned. (This matches the |
---|
1622 | n/a | behavior of the traceback module.) |
---|
1623 | n/a | |
---|
1624 | n/a | For reasons beyond our control, only one stack frame is |
---|
1625 | n/a | returned for a suspended coroutine. |
---|
1626 | n/a | [clinic start generated code]*/ |
---|
1627 | n/a | |
---|
1628 | n/a | static PyObject * |
---|
1629 | n/a | _asyncio_Task_get_stack_impl(TaskObj *self, PyObject *limit) |
---|
1630 | n/a | /*[clinic end generated code: output=c9aeeeebd1e18118 input=b1920230a766d17a]*/ |
---|
1631 | n/a | { |
---|
1632 | n/a | return PyObject_CallFunctionObjArgs( |
---|
1633 | n/a | asyncio_task_get_stack_func, self, limit, NULL); |
---|
1634 | n/a | } |
---|
1635 | n/a | |
---|
1636 | n/a | /*[clinic input] |
---|
1637 | n/a | _asyncio.Task.print_stack |
---|
1638 | n/a | |
---|
1639 | n/a | * |
---|
1640 | n/a | limit: 'O' = None |
---|
1641 | n/a | file: 'O' = None |
---|
1642 | n/a | |
---|
1643 | n/a | Print the stack or traceback for this task's coroutine. |
---|
1644 | n/a | |
---|
1645 | n/a | This produces output similar to that of the traceback module, |
---|
1646 | n/a | for the frames retrieved by get_stack(). The limit argument |
---|
1647 | n/a | is passed to get_stack(). The file argument is an I/O stream |
---|
1648 | n/a | to which the output is written; by default output is written |
---|
1649 | n/a | to sys.stderr. |
---|
1650 | n/a | [clinic start generated code]*/ |
---|
1651 | n/a | |
---|
1652 | n/a | static PyObject * |
---|
1653 | n/a | _asyncio_Task_print_stack_impl(TaskObj *self, PyObject *limit, |
---|
1654 | n/a | PyObject *file) |
---|
1655 | n/a | /*[clinic end generated code: output=7339e10314cd3f4d input=19f1e99ab5400bc3]*/ |
---|
1656 | n/a | { |
---|
1657 | n/a | return PyObject_CallFunctionObjArgs( |
---|
1658 | n/a | asyncio_task_print_stack_func, self, limit, file, NULL); |
---|
1659 | n/a | } |
---|
1660 | n/a | |
---|
1661 | n/a | /*[clinic input] |
---|
1662 | n/a | _asyncio.Task._step |
---|
1663 | n/a | |
---|
1664 | n/a | exc: 'O' = NULL |
---|
1665 | n/a | [clinic start generated code]*/ |
---|
1666 | n/a | |
---|
1667 | n/a | static PyObject * |
---|
1668 | n/a | _asyncio_Task__step_impl(TaskObj *self, PyObject *exc) |
---|
1669 | n/a | /*[clinic end generated code: output=7ed23f0cefd5ae42 input=ada4b2324e5370af]*/ |
---|
1670 | n/a | { |
---|
1671 | n/a | return task_step(self, exc == Py_None ? NULL : exc); |
---|
1672 | n/a | } |
---|
1673 | n/a | |
---|
1674 | n/a | /*[clinic input] |
---|
1675 | n/a | _asyncio.Task._wakeup |
---|
1676 | n/a | |
---|
1677 | n/a | fut: 'O' |
---|
1678 | n/a | [clinic start generated code]*/ |
---|
1679 | n/a | |
---|
1680 | n/a | static PyObject * |
---|
1681 | n/a | _asyncio_Task__wakeup_impl(TaskObj *self, PyObject *fut) |
---|
1682 | n/a | /*[clinic end generated code: output=75cb341c760fd071 input=11ee4918a5bdbf21]*/ |
---|
1683 | n/a | { |
---|
1684 | n/a | return task_wakeup(self, fut); |
---|
1685 | n/a | } |
---|
1686 | n/a | |
---|
1687 | n/a | static void |
---|
1688 | n/a | TaskObj_finalize(TaskObj *task) |
---|
1689 | n/a | { |
---|
1690 | n/a | _Py_IDENTIFIER(call_exception_handler); |
---|
1691 | n/a | _Py_IDENTIFIER(task); |
---|
1692 | n/a | _Py_IDENTIFIER(message); |
---|
1693 | n/a | _Py_IDENTIFIER(source_traceback); |
---|
1694 | n/a | |
---|
1695 | n/a | PyObject *message = NULL; |
---|
1696 | n/a | PyObject *context = NULL; |
---|
1697 | n/a | PyObject *func = NULL; |
---|
1698 | n/a | PyObject *res = NULL; |
---|
1699 | n/a | |
---|
1700 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
1701 | n/a | |
---|
1702 | n/a | if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) { |
---|
1703 | n/a | goto done; |
---|
1704 | n/a | } |
---|
1705 | n/a | |
---|
1706 | n/a | /* Save the current exception, if any. */ |
---|
1707 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
1708 | n/a | |
---|
1709 | n/a | context = PyDict_New(); |
---|
1710 | n/a | if (context == NULL) { |
---|
1711 | n/a | goto finally; |
---|
1712 | n/a | } |
---|
1713 | n/a | |
---|
1714 | n/a | message = PyUnicode_FromString("Task was destroyed but it is pending!"); |
---|
1715 | n/a | if (message == NULL) { |
---|
1716 | n/a | goto finally; |
---|
1717 | n/a | } |
---|
1718 | n/a | |
---|
1719 | n/a | if (_PyDict_SetItemId(context, &PyId_message, message) < 0 || |
---|
1720 | n/a | _PyDict_SetItemId(context, &PyId_task, (PyObject*)task) < 0) |
---|
1721 | n/a | { |
---|
1722 | n/a | goto finally; |
---|
1723 | n/a | } |
---|
1724 | n/a | |
---|
1725 | n/a | if (task->task_source_tb != NULL) { |
---|
1726 | n/a | if (_PyDict_SetItemId(context, &PyId_source_traceback, |
---|
1727 | n/a | task->task_source_tb) < 0) |
---|
1728 | n/a | { |
---|
1729 | n/a | goto finally; |
---|
1730 | n/a | } |
---|
1731 | n/a | } |
---|
1732 | n/a | |
---|
1733 | n/a | func = _PyObject_GetAttrId(task->task_loop, &PyId_call_exception_handler); |
---|
1734 | n/a | if (func != NULL) { |
---|
1735 | n/a | res = PyObject_CallFunctionObjArgs(func, context, NULL); |
---|
1736 | n/a | if (res == NULL) { |
---|
1737 | n/a | PyErr_WriteUnraisable(func); |
---|
1738 | n/a | } |
---|
1739 | n/a | } |
---|
1740 | n/a | |
---|
1741 | n/a | finally: |
---|
1742 | n/a | Py_CLEAR(context); |
---|
1743 | n/a | Py_CLEAR(message); |
---|
1744 | n/a | Py_CLEAR(func); |
---|
1745 | n/a | Py_CLEAR(res); |
---|
1746 | n/a | |
---|
1747 | n/a | /* Restore the saved exception. */ |
---|
1748 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
1749 | n/a | |
---|
1750 | n/a | done: |
---|
1751 | n/a | FutureObj_finalize((FutureObj*)task); |
---|
1752 | n/a | } |
---|
1753 | n/a | |
---|
1754 | n/a | static void TaskObj_dealloc(PyObject *); /* Needs Task_CheckExact */ |
---|
1755 | n/a | |
---|
1756 | n/a | static PyMethodDef TaskType_methods[] = { |
---|
1757 | n/a | _ASYNCIO_FUTURE_RESULT_METHODDEF |
---|
1758 | n/a | _ASYNCIO_FUTURE_EXCEPTION_METHODDEF |
---|
1759 | n/a | _ASYNCIO_FUTURE_SET_RESULT_METHODDEF |
---|
1760 | n/a | _ASYNCIO_FUTURE_SET_EXCEPTION_METHODDEF |
---|
1761 | n/a | _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF |
---|
1762 | n/a | _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF |
---|
1763 | n/a | _ASYNCIO_FUTURE_CANCELLED_METHODDEF |
---|
1764 | n/a | _ASYNCIO_FUTURE_DONE_METHODDEF |
---|
1765 | n/a | _ASYNCIO_TASK_CURRENT_TASK_METHODDEF |
---|
1766 | n/a | _ASYNCIO_TASK_ALL_TASKS_METHODDEF |
---|
1767 | n/a | _ASYNCIO_TASK_CANCEL_METHODDEF |
---|
1768 | n/a | _ASYNCIO_TASK_GET_STACK_METHODDEF |
---|
1769 | n/a | _ASYNCIO_TASK_PRINT_STACK_METHODDEF |
---|
1770 | n/a | _ASYNCIO_TASK__WAKEUP_METHODDEF |
---|
1771 | n/a | _ASYNCIO_TASK__STEP_METHODDEF |
---|
1772 | n/a | _ASYNCIO_TASK__REPR_INFO_METHODDEF |
---|
1773 | n/a | {NULL, NULL} /* Sentinel */ |
---|
1774 | n/a | }; |
---|
1775 | n/a | |
---|
1776 | n/a | static PyGetSetDef TaskType_getsetlist[] = { |
---|
1777 | n/a | FUTURE_COMMON_GETSETLIST |
---|
1778 | n/a | {"_log_destroy_pending", (getter)TaskObj_get_log_destroy_pending, |
---|
1779 | n/a | (setter)TaskObj_set_log_destroy_pending, NULL}, |
---|
1780 | n/a | {"_must_cancel", (getter)TaskObj_get_must_cancel, NULL, NULL}, |
---|
1781 | n/a | {"_coro", (getter)TaskObj_get_coro, NULL, NULL}, |
---|
1782 | n/a | {"_fut_waiter", (getter)TaskObj_get_fut_waiter, NULL, NULL}, |
---|
1783 | n/a | {NULL} /* Sentinel */ |
---|
1784 | n/a | }; |
---|
1785 | n/a | |
---|
1786 | n/a | static PyTypeObject TaskType = { |
---|
1787 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1788 | n/a | "_asyncio.Task", |
---|
1789 | n/a | sizeof(TaskObj), /* tp_basicsize */ |
---|
1790 | n/a | .tp_base = &FutureType, |
---|
1791 | n/a | .tp_dealloc = TaskObj_dealloc, |
---|
1792 | n/a | .tp_as_async = &FutureType_as_async, |
---|
1793 | n/a | .tp_repr = (reprfunc)FutureObj_repr, |
---|
1794 | n/a | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
---|
1795 | n/a | | Py_TPFLAGS_HAVE_FINALIZE, |
---|
1796 | n/a | .tp_doc = _asyncio_Task___init____doc__, |
---|
1797 | n/a | .tp_traverse = (traverseproc)TaskObj_traverse, |
---|
1798 | n/a | .tp_clear = (inquiry)TaskObj_clear, |
---|
1799 | n/a | .tp_weaklistoffset = offsetof(TaskObj, task_weakreflist), |
---|
1800 | n/a | .tp_iter = (getiterfunc)future_new_iter, |
---|
1801 | n/a | .tp_methods = TaskType_methods, |
---|
1802 | n/a | .tp_getset = TaskType_getsetlist, |
---|
1803 | n/a | .tp_dictoffset = offsetof(TaskObj, dict), |
---|
1804 | n/a | .tp_init = (initproc)_asyncio_Task___init__, |
---|
1805 | n/a | .tp_new = PyType_GenericNew, |
---|
1806 | n/a | .tp_finalize = (destructor)TaskObj_finalize, |
---|
1807 | n/a | }; |
---|
1808 | n/a | |
---|
1809 | n/a | #define Task_CheckExact(obj) (Py_TYPE(obj) == &TaskType) |
---|
1810 | n/a | |
---|
1811 | n/a | static void |
---|
1812 | n/a | TaskObj_dealloc(PyObject *self) |
---|
1813 | n/a | { |
---|
1814 | n/a | TaskObj *task = (TaskObj *)self; |
---|
1815 | n/a | |
---|
1816 | n/a | if (Task_CheckExact(self)) { |
---|
1817 | n/a | /* When fut is subclass of Task, finalizer is called from |
---|
1818 | n/a | * subtype_dealloc. |
---|
1819 | n/a | */ |
---|
1820 | n/a | if (PyObject_CallFinalizerFromDealloc(self) < 0) { |
---|
1821 | n/a | // resurrected. |
---|
1822 | n/a | return; |
---|
1823 | n/a | } |
---|
1824 | n/a | } |
---|
1825 | n/a | |
---|
1826 | n/a | if (task->task_weakreflist != NULL) { |
---|
1827 | n/a | PyObject_ClearWeakRefs(self); |
---|
1828 | n/a | } |
---|
1829 | n/a | |
---|
1830 | n/a | (void)TaskObj_clear(task); |
---|
1831 | n/a | Py_TYPE(task)->tp_free(task); |
---|
1832 | n/a | } |
---|
1833 | n/a | |
---|
1834 | n/a | static inline PyObject * |
---|
1835 | n/a | task_call_wakeup(TaskObj *task, PyObject *fut) |
---|
1836 | n/a | { |
---|
1837 | n/a | if (Task_CheckExact(task)) { |
---|
1838 | n/a | return task_wakeup(task, fut); |
---|
1839 | n/a | } |
---|
1840 | n/a | else { |
---|
1841 | n/a | /* `task` is a subclass of Task */ |
---|
1842 | n/a | return _PyObject_CallMethodIdObjArgs((PyObject*)task, &PyId__wakeup, |
---|
1843 | n/a | fut, NULL); |
---|
1844 | n/a | } |
---|
1845 | n/a | } |
---|
1846 | n/a | |
---|
1847 | n/a | static inline PyObject * |
---|
1848 | n/a | task_call_step(TaskObj *task, PyObject *arg) |
---|
1849 | n/a | { |
---|
1850 | n/a | if (Task_CheckExact(task)) { |
---|
1851 | n/a | return task_step(task, arg); |
---|
1852 | n/a | } |
---|
1853 | n/a | else { |
---|
1854 | n/a | /* `task` is a subclass of Task */ |
---|
1855 | n/a | if (arg == NULL) { |
---|
1856 | n/a | arg = Py_None; |
---|
1857 | n/a | } |
---|
1858 | n/a | return _PyObject_CallMethodIdObjArgs((PyObject*)task, &PyId__step, |
---|
1859 | n/a | arg, NULL); |
---|
1860 | n/a | } |
---|
1861 | n/a | } |
---|
1862 | n/a | |
---|
1863 | n/a | static int |
---|
1864 | n/a | task_call_step_soon(TaskObj *task, PyObject *arg) |
---|
1865 | n/a | { |
---|
1866 | n/a | PyObject *handle; |
---|
1867 | n/a | |
---|
1868 | n/a | PyObject *cb = TaskSendMethWrapper_new(task, arg); |
---|
1869 | n/a | if (cb == NULL) { |
---|
1870 | n/a | return -1; |
---|
1871 | n/a | } |
---|
1872 | n/a | |
---|
1873 | n/a | handle = _PyObject_CallMethodIdObjArgs(task->task_loop, &PyId_call_soon, |
---|
1874 | n/a | cb, NULL); |
---|
1875 | n/a | Py_DECREF(cb); |
---|
1876 | n/a | if (handle == NULL) { |
---|
1877 | n/a | return -1; |
---|
1878 | n/a | } |
---|
1879 | n/a | |
---|
1880 | n/a | Py_DECREF(handle); |
---|
1881 | n/a | return 0; |
---|
1882 | n/a | } |
---|
1883 | n/a | |
---|
1884 | n/a | static PyObject * |
---|
1885 | n/a | task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...) |
---|
1886 | n/a | { |
---|
1887 | n/a | PyObject* msg; |
---|
1888 | n/a | |
---|
1889 | n/a | va_list vargs; |
---|
1890 | n/a | #ifdef HAVE_STDARG_PROTOTYPES |
---|
1891 | n/a | va_start(vargs, format); |
---|
1892 | n/a | #else |
---|
1893 | n/a | va_start(vargs); |
---|
1894 | n/a | #endif |
---|
1895 | n/a | msg = PyUnicode_FromFormatV(format, vargs); |
---|
1896 | n/a | va_end(vargs); |
---|
1897 | n/a | |
---|
1898 | n/a | if (msg == NULL) { |
---|
1899 | n/a | return NULL; |
---|
1900 | n/a | } |
---|
1901 | n/a | |
---|
1902 | n/a | PyObject *e = PyObject_CallFunctionObjArgs(et, msg, NULL); |
---|
1903 | n/a | Py_DECREF(msg); |
---|
1904 | n/a | if (e == NULL) { |
---|
1905 | n/a | return NULL; |
---|
1906 | n/a | } |
---|
1907 | n/a | |
---|
1908 | n/a | if (task_call_step_soon(task, e) == -1) { |
---|
1909 | n/a | Py_DECREF(e); |
---|
1910 | n/a | return NULL; |
---|
1911 | n/a | } |
---|
1912 | n/a | |
---|
1913 | n/a | Py_DECREF(e); |
---|
1914 | n/a | Py_RETURN_NONE; |
---|
1915 | n/a | } |
---|
1916 | n/a | |
---|
1917 | n/a | static PyObject * |
---|
1918 | n/a | task_step_impl(TaskObj *task, PyObject *exc) |
---|
1919 | n/a | { |
---|
1920 | n/a | int res; |
---|
1921 | n/a | int clear_exc = 0; |
---|
1922 | n/a | PyObject *result = NULL; |
---|
1923 | n/a | PyObject *coro = task->task_coro; |
---|
1924 | n/a | PyObject *o; |
---|
1925 | n/a | |
---|
1926 | n/a | if (task->task_state != STATE_PENDING) { |
---|
1927 | n/a | PyErr_Format(PyExc_AssertionError, |
---|
1928 | n/a | "_step(): already done: %R %R", |
---|
1929 | n/a | task, |
---|
1930 | n/a | exc ? exc : Py_None); |
---|
1931 | n/a | goto fail; |
---|
1932 | n/a | } |
---|
1933 | n/a | |
---|
1934 | n/a | if (task->task_must_cancel) { |
---|
1935 | n/a | assert(exc != Py_None); |
---|
1936 | n/a | |
---|
1937 | n/a | if (exc) { |
---|
1938 | n/a | /* Check if exc is a CancelledError */ |
---|
1939 | n/a | res = PyObject_IsInstance(exc, asyncio_CancelledError); |
---|
1940 | n/a | if (res == -1) { |
---|
1941 | n/a | /* An error occurred, abort */ |
---|
1942 | n/a | goto fail; |
---|
1943 | n/a | } |
---|
1944 | n/a | if (res == 0) { |
---|
1945 | n/a | /* exc is not CancelledError; reset it to NULL */ |
---|
1946 | n/a | exc = NULL; |
---|
1947 | n/a | } |
---|
1948 | n/a | } |
---|
1949 | n/a | |
---|
1950 | n/a | if (!exc) { |
---|
1951 | n/a | /* exc was not a CancelledError */ |
---|
1952 | n/a | exc = _PyObject_CallNoArg(asyncio_CancelledError); |
---|
1953 | n/a | if (!exc) { |
---|
1954 | n/a | goto fail; |
---|
1955 | n/a | } |
---|
1956 | n/a | clear_exc = 1; |
---|
1957 | n/a | } |
---|
1958 | n/a | |
---|
1959 | n/a | task->task_must_cancel = 0; |
---|
1960 | n/a | } |
---|
1961 | n/a | |
---|
1962 | n/a | Py_CLEAR(task->task_fut_waiter); |
---|
1963 | n/a | |
---|
1964 | n/a | if (exc == NULL) { |
---|
1965 | n/a | if (PyGen_CheckExact(coro) || PyCoro_CheckExact(coro)) { |
---|
1966 | n/a | result = _PyGen_Send((PyGenObject*)coro, Py_None); |
---|
1967 | n/a | } |
---|
1968 | n/a | else { |
---|
1969 | n/a | result = _PyObject_CallMethodIdObjArgs(coro, &PyId_send, |
---|
1970 | n/a | Py_None, NULL); |
---|
1971 | n/a | } |
---|
1972 | n/a | } |
---|
1973 | n/a | else { |
---|
1974 | n/a | result = _PyObject_CallMethodIdObjArgs(coro, &PyId_throw, |
---|
1975 | n/a | exc, NULL); |
---|
1976 | n/a | if (clear_exc) { |
---|
1977 | n/a | /* We created 'exc' during this call */ |
---|
1978 | n/a | Py_CLEAR(exc); |
---|
1979 | n/a | } |
---|
1980 | n/a | } |
---|
1981 | n/a | |
---|
1982 | n/a | if (result == NULL) { |
---|
1983 | n/a | PyObject *et, *ev, *tb; |
---|
1984 | n/a | |
---|
1985 | n/a | if (_PyGen_FetchStopIterationValue(&o) == 0) { |
---|
1986 | n/a | /* The error is StopIteration and that means that |
---|
1987 | n/a | the underlying coroutine has resolved */ |
---|
1988 | n/a | PyObject *res = future_set_result((FutureObj*)task, o); |
---|
1989 | n/a | Py_DECREF(o); |
---|
1990 | n/a | if (res == NULL) { |
---|
1991 | n/a | return NULL; |
---|
1992 | n/a | } |
---|
1993 | n/a | Py_DECREF(res); |
---|
1994 | n/a | Py_RETURN_NONE; |
---|
1995 | n/a | } |
---|
1996 | n/a | |
---|
1997 | n/a | if (PyErr_ExceptionMatches(asyncio_CancelledError)) { |
---|
1998 | n/a | /* CancelledError */ |
---|
1999 | n/a | PyErr_Clear(); |
---|
2000 | n/a | return future_cancel((FutureObj*)task); |
---|
2001 | n/a | } |
---|
2002 | n/a | |
---|
2003 | n/a | /* Some other exception; pop it and call Task.set_exception() */ |
---|
2004 | n/a | PyErr_Fetch(&et, &ev, &tb); |
---|
2005 | n/a | assert(et); |
---|
2006 | n/a | if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) { |
---|
2007 | n/a | PyErr_NormalizeException(&et, &ev, &tb); |
---|
2008 | n/a | } |
---|
2009 | n/a | if (tb != NULL) { |
---|
2010 | n/a | PyException_SetTraceback(ev, tb); |
---|
2011 | n/a | } |
---|
2012 | n/a | o = future_set_exception((FutureObj*)task, ev); |
---|
2013 | n/a | if (!o) { |
---|
2014 | n/a | /* An exception in Task.set_exception() */ |
---|
2015 | n/a | Py_XDECREF(et); |
---|
2016 | n/a | Py_XDECREF(tb); |
---|
2017 | n/a | Py_XDECREF(ev); |
---|
2018 | n/a | goto fail; |
---|
2019 | n/a | } |
---|
2020 | n/a | assert(o == Py_None); |
---|
2021 | n/a | Py_CLEAR(o); |
---|
2022 | n/a | |
---|
2023 | n/a | if (!PyErr_GivenExceptionMatches(et, PyExc_Exception)) { |
---|
2024 | n/a | /* We've got a BaseException; re-raise it */ |
---|
2025 | n/a | PyErr_Restore(et, ev, tb); |
---|
2026 | n/a | goto fail; |
---|
2027 | n/a | } |
---|
2028 | n/a | |
---|
2029 | n/a | Py_XDECREF(et); |
---|
2030 | n/a | Py_XDECREF(tb); |
---|
2031 | n/a | Py_XDECREF(ev); |
---|
2032 | n/a | |
---|
2033 | n/a | Py_RETURN_NONE; |
---|
2034 | n/a | } |
---|
2035 | n/a | |
---|
2036 | n/a | if (result == (PyObject*)task) { |
---|
2037 | n/a | /* We have a task that wants to await on itself */ |
---|
2038 | n/a | goto self_await; |
---|
2039 | n/a | } |
---|
2040 | n/a | |
---|
2041 | n/a | /* Check if `result` is FutureObj or TaskObj (and not a subclass) */ |
---|
2042 | n/a | if (Future_CheckExact(result) || Task_CheckExact(result)) { |
---|
2043 | n/a | PyObject *wrapper; |
---|
2044 | n/a | PyObject *res; |
---|
2045 | n/a | FutureObj *fut = (FutureObj*)result; |
---|
2046 | n/a | |
---|
2047 | n/a | /* Check if `result` future is attached to a different loop */ |
---|
2048 | n/a | if (fut->fut_loop != task->task_loop) { |
---|
2049 | n/a | goto different_loop; |
---|
2050 | n/a | } |
---|
2051 | n/a | |
---|
2052 | n/a | if (fut->fut_blocking) { |
---|
2053 | n/a | fut->fut_blocking = 0; |
---|
2054 | n/a | |
---|
2055 | n/a | /* result.add_done_callback(task._wakeup) */ |
---|
2056 | n/a | wrapper = TaskWakeupMethWrapper_new(task); |
---|
2057 | n/a | if (wrapper == NULL) { |
---|
2058 | n/a | goto fail; |
---|
2059 | n/a | } |
---|
2060 | n/a | res = future_add_done_callback((FutureObj*)result, wrapper); |
---|
2061 | n/a | Py_DECREF(wrapper); |
---|
2062 | n/a | if (res == NULL) { |
---|
2063 | n/a | goto fail; |
---|
2064 | n/a | } |
---|
2065 | n/a | Py_DECREF(res); |
---|
2066 | n/a | |
---|
2067 | n/a | /* task._fut_waiter = result */ |
---|
2068 | n/a | task->task_fut_waiter = result; /* no incref is necessary */ |
---|
2069 | n/a | |
---|
2070 | n/a | if (task->task_must_cancel) { |
---|
2071 | n/a | PyObject *r; |
---|
2072 | n/a | r = future_cancel(fut); |
---|
2073 | n/a | if (r == NULL) { |
---|
2074 | n/a | return NULL; |
---|
2075 | n/a | } |
---|
2076 | n/a | if (r == Py_True) { |
---|
2077 | n/a | task->task_must_cancel = 0; |
---|
2078 | n/a | } |
---|
2079 | n/a | Py_DECREF(r); |
---|
2080 | n/a | } |
---|
2081 | n/a | |
---|
2082 | n/a | Py_RETURN_NONE; |
---|
2083 | n/a | } |
---|
2084 | n/a | else { |
---|
2085 | n/a | goto yield_insteadof_yf; |
---|
2086 | n/a | } |
---|
2087 | n/a | } |
---|
2088 | n/a | |
---|
2089 | n/a | /* Check if `result` is a Future-compatible object */ |
---|
2090 | n/a | o = PyObject_GetAttrString(result, "_asyncio_future_blocking"); |
---|
2091 | n/a | if (o == NULL) { |
---|
2092 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
---|
2093 | n/a | PyErr_Clear(); |
---|
2094 | n/a | } |
---|
2095 | n/a | else { |
---|
2096 | n/a | goto fail; |
---|
2097 | n/a | } |
---|
2098 | n/a | } |
---|
2099 | n/a | else { |
---|
2100 | n/a | if (o == Py_None) { |
---|
2101 | n/a | Py_CLEAR(o); |
---|
2102 | n/a | } |
---|
2103 | n/a | else { |
---|
2104 | n/a | /* `result` is a Future-compatible object */ |
---|
2105 | n/a | PyObject *wrapper; |
---|
2106 | n/a | PyObject *res; |
---|
2107 | n/a | |
---|
2108 | n/a | int blocking = PyObject_IsTrue(o); |
---|
2109 | n/a | Py_CLEAR(o); |
---|
2110 | n/a | if (blocking < 0) { |
---|
2111 | n/a | goto fail; |
---|
2112 | n/a | } |
---|
2113 | n/a | |
---|
2114 | n/a | /* Check if `result` future is attached to a different loop */ |
---|
2115 | n/a | PyObject *oloop = PyObject_GetAttrString(result, "_loop"); |
---|
2116 | n/a | if (oloop == NULL) { |
---|
2117 | n/a | goto fail; |
---|
2118 | n/a | } |
---|
2119 | n/a | if (oloop != task->task_loop) { |
---|
2120 | n/a | Py_DECREF(oloop); |
---|
2121 | n/a | goto different_loop; |
---|
2122 | n/a | } |
---|
2123 | n/a | else { |
---|
2124 | n/a | Py_DECREF(oloop); |
---|
2125 | n/a | } |
---|
2126 | n/a | |
---|
2127 | n/a | if (blocking) { |
---|
2128 | n/a | /* result._asyncio_future_blocking = False */ |
---|
2129 | n/a | if (PyObject_SetAttrString( |
---|
2130 | n/a | result, "_asyncio_future_blocking", Py_False) == -1) { |
---|
2131 | n/a | goto fail; |
---|
2132 | n/a | } |
---|
2133 | n/a | |
---|
2134 | n/a | /* result.add_done_callback(task._wakeup) */ |
---|
2135 | n/a | wrapper = TaskWakeupMethWrapper_new(task); |
---|
2136 | n/a | if (wrapper == NULL) { |
---|
2137 | n/a | goto fail; |
---|
2138 | n/a | } |
---|
2139 | n/a | res = _PyObject_CallMethodIdObjArgs(result, |
---|
2140 | n/a | &PyId_add_done_callback, |
---|
2141 | n/a | wrapper, NULL); |
---|
2142 | n/a | Py_DECREF(wrapper); |
---|
2143 | n/a | if (res == NULL) { |
---|
2144 | n/a | goto fail; |
---|
2145 | n/a | } |
---|
2146 | n/a | Py_DECREF(res); |
---|
2147 | n/a | |
---|
2148 | n/a | /* task._fut_waiter = result */ |
---|
2149 | n/a | task->task_fut_waiter = result; /* no incref is necessary */ |
---|
2150 | n/a | |
---|
2151 | n/a | if (task->task_must_cancel) { |
---|
2152 | n/a | PyObject *r; |
---|
2153 | n/a | int is_true; |
---|
2154 | n/a | r = _PyObject_CallMethodId(result, &PyId_cancel, NULL); |
---|
2155 | n/a | if (r == NULL) { |
---|
2156 | n/a | return NULL; |
---|
2157 | n/a | } |
---|
2158 | n/a | is_true = PyObject_IsTrue(r); |
---|
2159 | n/a | Py_DECREF(r); |
---|
2160 | n/a | if (is_true < 0) { |
---|
2161 | n/a | return NULL; |
---|
2162 | n/a | } |
---|
2163 | n/a | else if (is_true) { |
---|
2164 | n/a | task->task_must_cancel = 0; |
---|
2165 | n/a | } |
---|
2166 | n/a | } |
---|
2167 | n/a | |
---|
2168 | n/a | Py_RETURN_NONE; |
---|
2169 | n/a | } |
---|
2170 | n/a | else { |
---|
2171 | n/a | goto yield_insteadof_yf; |
---|
2172 | n/a | } |
---|
2173 | n/a | } |
---|
2174 | n/a | } |
---|
2175 | n/a | |
---|
2176 | n/a | /* Check if `result` is None */ |
---|
2177 | n/a | if (result == Py_None) { |
---|
2178 | n/a | /* Bare yield relinquishes control for one event loop iteration. */ |
---|
2179 | n/a | if (task_call_step_soon(task, NULL)) { |
---|
2180 | n/a | goto fail; |
---|
2181 | n/a | } |
---|
2182 | n/a | return result; |
---|
2183 | n/a | } |
---|
2184 | n/a | |
---|
2185 | n/a | /* Check if `result` is a generator */ |
---|
2186 | n/a | o = PyObject_CallFunctionObjArgs(inspect_isgenerator, result, NULL); |
---|
2187 | n/a | if (o == NULL) { |
---|
2188 | n/a | /* An exception in inspect.isgenerator */ |
---|
2189 | n/a | goto fail; |
---|
2190 | n/a | } |
---|
2191 | n/a | res = PyObject_IsTrue(o); |
---|
2192 | n/a | Py_CLEAR(o); |
---|
2193 | n/a | if (res == -1) { |
---|
2194 | n/a | /* An exception while checking if 'val' is True */ |
---|
2195 | n/a | goto fail; |
---|
2196 | n/a | } |
---|
2197 | n/a | if (res == 1) { |
---|
2198 | n/a | /* `result` is a generator */ |
---|
2199 | n/a | PyObject *ret; |
---|
2200 | n/a | ret = task_set_error_soon( |
---|
2201 | n/a | task, PyExc_RuntimeError, |
---|
2202 | n/a | "yield was used instead of yield from for " |
---|
2203 | n/a | "generator in task %R with %S", task, result); |
---|
2204 | n/a | Py_DECREF(result); |
---|
2205 | n/a | return ret; |
---|
2206 | n/a | } |
---|
2207 | n/a | |
---|
2208 | n/a | /* The `result` is none of the above */ |
---|
2209 | n/a | Py_DECREF(result); |
---|
2210 | n/a | return task_set_error_soon( |
---|
2211 | n/a | task, PyExc_RuntimeError, "Task got bad yield: %R", result); |
---|
2212 | n/a | |
---|
2213 | n/a | self_await: |
---|
2214 | n/a | o = task_set_error_soon( |
---|
2215 | n/a | task, PyExc_RuntimeError, |
---|
2216 | n/a | "Task cannot await on itself: %R", task); |
---|
2217 | n/a | Py_DECREF(result); |
---|
2218 | n/a | return o; |
---|
2219 | n/a | |
---|
2220 | n/a | yield_insteadof_yf: |
---|
2221 | n/a | o = task_set_error_soon( |
---|
2222 | n/a | task, PyExc_RuntimeError, |
---|
2223 | n/a | "yield was used instead of yield from " |
---|
2224 | n/a | "in task %R with %R", |
---|
2225 | n/a | task, result); |
---|
2226 | n/a | Py_DECREF(result); |
---|
2227 | n/a | return o; |
---|
2228 | n/a | |
---|
2229 | n/a | different_loop: |
---|
2230 | n/a | o = task_set_error_soon( |
---|
2231 | n/a | task, PyExc_RuntimeError, |
---|
2232 | n/a | "Task %R got Future %R attached to a different loop", |
---|
2233 | n/a | task, result); |
---|
2234 | n/a | Py_DECREF(result); |
---|
2235 | n/a | return o; |
---|
2236 | n/a | |
---|
2237 | n/a | fail: |
---|
2238 | n/a | Py_XDECREF(result); |
---|
2239 | n/a | return NULL; |
---|
2240 | n/a | } |
---|
2241 | n/a | |
---|
2242 | n/a | static PyObject * |
---|
2243 | n/a | task_step(TaskObj *task, PyObject *exc) |
---|
2244 | n/a | { |
---|
2245 | n/a | PyObject *res; |
---|
2246 | n/a | PyObject *ot; |
---|
2247 | n/a | |
---|
2248 | n/a | if (PyDict_SetItem(current_tasks, |
---|
2249 | n/a | task->task_loop, (PyObject*)task) == -1) |
---|
2250 | n/a | { |
---|
2251 | n/a | return NULL; |
---|
2252 | n/a | } |
---|
2253 | n/a | |
---|
2254 | n/a | res = task_step_impl(task, exc); |
---|
2255 | n/a | |
---|
2256 | n/a | if (res == NULL) { |
---|
2257 | n/a | PyObject *et, *ev, *tb; |
---|
2258 | n/a | PyErr_Fetch(&et, &ev, &tb); |
---|
2259 | n/a | ot = _PyDict_Pop(current_tasks, task->task_loop, NULL); |
---|
2260 | n/a | if (ot == NULL) { |
---|
2261 | n/a | Py_XDECREF(et); |
---|
2262 | n/a | Py_XDECREF(tb); |
---|
2263 | n/a | Py_XDECREF(ev); |
---|
2264 | n/a | return NULL; |
---|
2265 | n/a | } |
---|
2266 | n/a | Py_DECREF(ot); |
---|
2267 | n/a | PyErr_Restore(et, ev, tb); |
---|
2268 | n/a | return NULL; |
---|
2269 | n/a | } |
---|
2270 | n/a | else { |
---|
2271 | n/a | ot = _PyDict_Pop(current_tasks, task->task_loop, NULL); |
---|
2272 | n/a | if (ot == NULL) { |
---|
2273 | n/a | Py_DECREF(res); |
---|
2274 | n/a | return NULL; |
---|
2275 | n/a | } |
---|
2276 | n/a | else { |
---|
2277 | n/a | Py_DECREF(ot); |
---|
2278 | n/a | return res; |
---|
2279 | n/a | } |
---|
2280 | n/a | } |
---|
2281 | n/a | } |
---|
2282 | n/a | |
---|
2283 | n/a | static PyObject * |
---|
2284 | n/a | task_wakeup(TaskObj *task, PyObject *o) |
---|
2285 | n/a | { |
---|
2286 | n/a | assert(o); |
---|
2287 | n/a | |
---|
2288 | n/a | if (Future_CheckExact(o) || Task_CheckExact(o)) { |
---|
2289 | n/a | PyObject *fut_result = NULL; |
---|
2290 | n/a | int res = future_get_result((FutureObj*)o, &fut_result); |
---|
2291 | n/a | PyObject *result; |
---|
2292 | n/a | |
---|
2293 | n/a | switch(res) { |
---|
2294 | n/a | case -1: |
---|
2295 | n/a | assert(fut_result == NULL); |
---|
2296 | n/a | return NULL; |
---|
2297 | n/a | case 0: |
---|
2298 | n/a | Py_DECREF(fut_result); |
---|
2299 | n/a | return task_call_step(task, NULL); |
---|
2300 | n/a | default: |
---|
2301 | n/a | assert(res == 1); |
---|
2302 | n/a | result = task_call_step(task, fut_result); |
---|
2303 | n/a | Py_DECREF(fut_result); |
---|
2304 | n/a | return result; |
---|
2305 | n/a | } |
---|
2306 | n/a | } |
---|
2307 | n/a | |
---|
2308 | n/a | PyObject *fut_result = PyObject_CallMethod(o, "result", NULL); |
---|
2309 | n/a | if (fut_result == NULL) { |
---|
2310 | n/a | PyObject *et, *ev, *tb; |
---|
2311 | n/a | PyObject *res; |
---|
2312 | n/a | |
---|
2313 | n/a | PyErr_Fetch(&et, &ev, &tb); |
---|
2314 | n/a | if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) { |
---|
2315 | n/a | PyErr_NormalizeException(&et, &ev, &tb); |
---|
2316 | n/a | } |
---|
2317 | n/a | |
---|
2318 | n/a | res = task_call_step(task, ev); |
---|
2319 | n/a | |
---|
2320 | n/a | Py_XDECREF(et); |
---|
2321 | n/a | Py_XDECREF(tb); |
---|
2322 | n/a | Py_XDECREF(ev); |
---|
2323 | n/a | |
---|
2324 | n/a | return res; |
---|
2325 | n/a | } |
---|
2326 | n/a | else { |
---|
2327 | n/a | Py_DECREF(fut_result); |
---|
2328 | n/a | return task_call_step(task, NULL); |
---|
2329 | n/a | } |
---|
2330 | n/a | } |
---|
2331 | n/a | |
---|
2332 | n/a | |
---|
2333 | n/a | /*********************** Module **************************/ |
---|
2334 | n/a | |
---|
2335 | n/a | |
---|
2336 | n/a | static void |
---|
2337 | n/a | module_free(void *m) |
---|
2338 | n/a | { |
---|
2339 | n/a | Py_CLEAR(current_tasks); |
---|
2340 | n/a | Py_CLEAR(all_tasks); |
---|
2341 | n/a | Py_CLEAR(traceback_extract_stack); |
---|
2342 | n/a | Py_CLEAR(asyncio_get_event_loop); |
---|
2343 | n/a | Py_CLEAR(asyncio_future_repr_info_func); |
---|
2344 | n/a | Py_CLEAR(asyncio_task_repr_info_func); |
---|
2345 | n/a | Py_CLEAR(asyncio_task_get_stack_func); |
---|
2346 | n/a | Py_CLEAR(asyncio_task_print_stack_func); |
---|
2347 | n/a | Py_CLEAR(asyncio_InvalidStateError); |
---|
2348 | n/a | Py_CLEAR(asyncio_CancelledError); |
---|
2349 | n/a | Py_CLEAR(inspect_isgenerator); |
---|
2350 | n/a | } |
---|
2351 | n/a | |
---|
2352 | n/a | static int |
---|
2353 | n/a | module_init(void) |
---|
2354 | n/a | { |
---|
2355 | n/a | PyObject *module = NULL; |
---|
2356 | n/a | PyObject *cls; |
---|
2357 | n/a | |
---|
2358 | n/a | #define WITH_MOD(NAME) \ |
---|
2359 | n/a | Py_CLEAR(module); \ |
---|
2360 | n/a | module = PyImport_ImportModule(NAME); \ |
---|
2361 | n/a | if (module == NULL) { \ |
---|
2362 | n/a | return -1; \ |
---|
2363 | n/a | } |
---|
2364 | n/a | |
---|
2365 | n/a | #define GET_MOD_ATTR(VAR, NAME) \ |
---|
2366 | n/a | VAR = PyObject_GetAttrString(module, NAME); \ |
---|
2367 | n/a | if (VAR == NULL) { \ |
---|
2368 | n/a | goto fail; \ |
---|
2369 | n/a | } |
---|
2370 | n/a | |
---|
2371 | n/a | WITH_MOD("asyncio.events") |
---|
2372 | n/a | GET_MOD_ATTR(asyncio_get_event_loop, "get_event_loop") |
---|
2373 | n/a | |
---|
2374 | n/a | WITH_MOD("asyncio.base_futures") |
---|
2375 | n/a | GET_MOD_ATTR(asyncio_future_repr_info_func, "_future_repr_info") |
---|
2376 | n/a | GET_MOD_ATTR(asyncio_InvalidStateError, "InvalidStateError") |
---|
2377 | n/a | GET_MOD_ATTR(asyncio_CancelledError, "CancelledError") |
---|
2378 | n/a | |
---|
2379 | n/a | WITH_MOD("asyncio.base_tasks") |
---|
2380 | n/a | GET_MOD_ATTR(asyncio_task_repr_info_func, "_task_repr_info") |
---|
2381 | n/a | GET_MOD_ATTR(asyncio_task_get_stack_func, "_task_get_stack") |
---|
2382 | n/a | GET_MOD_ATTR(asyncio_task_print_stack_func, "_task_print_stack") |
---|
2383 | n/a | |
---|
2384 | n/a | WITH_MOD("inspect") |
---|
2385 | n/a | GET_MOD_ATTR(inspect_isgenerator, "isgenerator") |
---|
2386 | n/a | |
---|
2387 | n/a | WITH_MOD("traceback") |
---|
2388 | n/a | GET_MOD_ATTR(traceback_extract_stack, "extract_stack") |
---|
2389 | n/a | |
---|
2390 | n/a | WITH_MOD("weakref") |
---|
2391 | n/a | GET_MOD_ATTR(cls, "WeakSet") |
---|
2392 | n/a | all_tasks = _PyObject_CallNoArg(cls); |
---|
2393 | n/a | Py_CLEAR(cls); |
---|
2394 | n/a | if (all_tasks == NULL) { |
---|
2395 | n/a | goto fail; |
---|
2396 | n/a | } |
---|
2397 | n/a | |
---|
2398 | n/a | current_tasks = PyDict_New(); |
---|
2399 | n/a | if (current_tasks == NULL) { |
---|
2400 | n/a | goto fail; |
---|
2401 | n/a | } |
---|
2402 | n/a | |
---|
2403 | n/a | Py_CLEAR(module); |
---|
2404 | n/a | return 0; |
---|
2405 | n/a | |
---|
2406 | n/a | fail: |
---|
2407 | n/a | Py_CLEAR(module); |
---|
2408 | n/a | module_free(NULL); |
---|
2409 | n/a | return -1; |
---|
2410 | n/a | |
---|
2411 | n/a | #undef WITH_MOD |
---|
2412 | n/a | #undef GET_MOD_ATTR |
---|
2413 | n/a | } |
---|
2414 | n/a | |
---|
2415 | n/a | PyDoc_STRVAR(module_doc, "Accelerator module for asyncio"); |
---|
2416 | n/a | |
---|
2417 | n/a | static struct PyModuleDef _asynciomodule = { |
---|
2418 | n/a | PyModuleDef_HEAD_INIT, /* m_base */ |
---|
2419 | n/a | "_asyncio", /* m_name */ |
---|
2420 | n/a | module_doc, /* m_doc */ |
---|
2421 | n/a | -1, /* m_size */ |
---|
2422 | n/a | NULL, /* m_methods */ |
---|
2423 | n/a | NULL, /* m_slots */ |
---|
2424 | n/a | NULL, /* m_traverse */ |
---|
2425 | n/a | NULL, /* m_clear */ |
---|
2426 | n/a | (freefunc)module_free /* m_free */ |
---|
2427 | n/a | }; |
---|
2428 | n/a | |
---|
2429 | n/a | |
---|
2430 | n/a | PyMODINIT_FUNC |
---|
2431 | n/a | PyInit__asyncio(void) |
---|
2432 | n/a | { |
---|
2433 | n/a | if (module_init() < 0) { |
---|
2434 | n/a | return NULL; |
---|
2435 | n/a | } |
---|
2436 | n/a | if (PyType_Ready(&FutureType) < 0) { |
---|
2437 | n/a | return NULL; |
---|
2438 | n/a | } |
---|
2439 | n/a | if (PyType_Ready(&FutureIterType) < 0) { |
---|
2440 | n/a | return NULL; |
---|
2441 | n/a | } |
---|
2442 | n/a | if (PyType_Ready(&TaskSendMethWrapper_Type) < 0) { |
---|
2443 | n/a | return NULL; |
---|
2444 | n/a | } |
---|
2445 | n/a | if(PyType_Ready(&TaskWakeupMethWrapper_Type) < 0) { |
---|
2446 | n/a | return NULL; |
---|
2447 | n/a | } |
---|
2448 | n/a | if (PyType_Ready(&TaskType) < 0) { |
---|
2449 | n/a | return NULL; |
---|
2450 | n/a | } |
---|
2451 | n/a | |
---|
2452 | n/a | PyObject *m = PyModule_Create(&_asynciomodule); |
---|
2453 | n/a | if (m == NULL) { |
---|
2454 | n/a | return NULL; |
---|
2455 | n/a | } |
---|
2456 | n/a | |
---|
2457 | n/a | Py_INCREF(&FutureType); |
---|
2458 | n/a | if (PyModule_AddObject(m, "Future", (PyObject *)&FutureType) < 0) { |
---|
2459 | n/a | Py_DECREF(&FutureType); |
---|
2460 | n/a | return NULL; |
---|
2461 | n/a | } |
---|
2462 | n/a | |
---|
2463 | n/a | Py_INCREF(&TaskType); |
---|
2464 | n/a | if (PyModule_AddObject(m, "Task", (PyObject *)&TaskType) < 0) { |
---|
2465 | n/a | Py_DECREF(&TaskType); |
---|
2466 | n/a | return NULL; |
---|
2467 | n/a | } |
---|
2468 | n/a | |
---|
2469 | n/a | return m; |
---|
2470 | n/a | } |
---|