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