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