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