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