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