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