| 
									
										
										
										
											2021-10-22 16:36:28 +03:00
										 |  |  | #ifndef Py_BUILD_CORE_BUILTIN
 | 
					
						
							|  |  |  | #  define Py_BUILD_CORE_MODULE 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2024-08-11 20:49:31 +05:30
										 |  |  | #include "pycore_critical_section.h"  // Py_BEGIN_CRITICAL_SECTION_MUT()
 | 
					
						
							| 
									
										
										
										
											2023-08-24 22:01:50 +02:00
										 |  |  | #include "pycore_dict.h"          // _PyDict_GetItem_KnownHash()
 | 
					
						
							| 
									
										
										
										
											2024-07-23 12:24:24 -04:00
										 |  |  | #include "pycore_freelist.h"      // _Py_FREELIST_POP()
 | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  | #include "pycore_llist.h"         // struct llist_node
 | 
					
						
							| 
									
										
										
										
											2023-10-17 14:30:31 +02:00
										 |  |  | #include "pycore_modsupport.h"    // _PyArg_CheckPositional()
 | 
					
						
							| 
									
										
										
										
											2023-07-04 11:41:43 +02:00
										 |  |  | #include "pycore_moduleobject.h"  // _PyModule_GetState()
 | 
					
						
							| 
									
										
										
										
											2025-01-24 21:12:56 +05:30
										 |  |  | #include "pycore_object.h"        // _PyObject_SetMaybeWeakref
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | #include "pycore_pyerrors.h"      // _PyErr_ClearExcState()
 | 
					
						
							| 
									
										
										
										
											2023-07-04 11:41:43 +02:00
										 |  |  | #include "pycore_pylifecycle.h"   // _Py_IsInterpreterFinalizing()
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | #include "pycore_pystate.h"       // _PyThreadState_GET()
 | 
					
						
							|  |  |  | #include "pycore_runtime_init.h"  // _Py_ID()
 | 
					
						
							| 
									
										
										
										
											2023-07-25 15:28:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | #include <stddef.h>               // offsetof()
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | module _asyncio | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | typedef enum { | 
					
						
							|  |  |  |     STATE_PENDING, | 
					
						
							|  |  |  |     STATE_CANCELLED, | 
					
						
							|  |  |  |     STATE_FINISHED | 
					
						
							|  |  |  | } fut_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define FutureObj_HEAD(prefix)                                              \
 | 
					
						
							|  |  |  |     PyObject_HEAD                                                           \ | 
					
						
							|  |  |  |     PyObject *prefix##_loop;                                                \ | 
					
						
							|  |  |  |     PyObject *prefix##_callback0;                                           \ | 
					
						
							|  |  |  |     PyObject *prefix##_context0;                                            \ | 
					
						
							|  |  |  |     PyObject *prefix##_callbacks;                                           \ | 
					
						
							|  |  |  |     PyObject *prefix##_exception;                                           \ | 
					
						
							|  |  |  |     PyObject *prefix##_exception_tb;                                        \ | 
					
						
							|  |  |  |     PyObject *prefix##_result;                                              \ | 
					
						
							|  |  |  |     PyObject *prefix##_source_tb;                                           \ | 
					
						
							|  |  |  |     PyObject *prefix##_cancel_msg;                                          \ | 
					
						
							|  |  |  |     PyObject *prefix##_cancelled_exc;                                       \ | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     PyObject *prefix##_awaited_by;                                          \ | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     fut_state prefix##_state;                                               \ | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     /* Used by profilers to make traversing the stack from an external      \
 | 
					
						
							|  |  |  |        process faster. */                                                   \ | 
					
						
							|  |  |  |     char prefix##_is_task;                                                  \ | 
					
						
							|  |  |  |     char prefix##_awaited_by_is_set;                                        \ | 
					
						
							|  |  |  |     /* These bitfields need to be at the end of the struct                  \
 | 
					
						
							|  |  |  |        so that these and bitfields from TaskObj are contiguous.             \ | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     */                                                                      \ | 
					
						
							|  |  |  |     unsigned prefix##_log_tb: 1;                                            \ | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     unsigned prefix##_blocking: 1;                                          \ | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     FutureObj_HEAD(fut) | 
					
						
							|  |  |  | } FutureObj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct TaskObj { | 
					
						
							|  |  |  |     FutureObj_HEAD(task) | 
					
						
							|  |  |  |     unsigned task_must_cancel: 1; | 
					
						
							|  |  |  |     unsigned task_log_destroy_pending: 1; | 
					
						
							|  |  |  |     int task_num_cancels_requested; | 
					
						
							|  |  |  |     PyObject *task_fut_waiter; | 
					
						
							|  |  |  |     PyObject *task_coro; | 
					
						
							|  |  |  |     PyObject *task_name; | 
					
						
							|  |  |  |     PyObject *task_context; | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  |     struct llist_node task_node; | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     // thread id of the thread where this task was created
 | 
					
						
							|  |  |  |     uintptr_t task_tid; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | } TaskObj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     TaskObj *sw_task; | 
					
						
							|  |  |  |     PyObject *sw_arg; | 
					
						
							|  |  |  | } TaskStepMethWrapper; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define Future_CheckExact(state, obj) Py_IS_TYPE(obj, state->FutureType)
 | 
					
						
							|  |  |  | #define Task_CheckExact(state, obj) Py_IS_TYPE(obj, state->TaskType)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | #define Future_Check(state, obj)                        \
 | 
					
						
							|  |  |  |     (Future_CheckExact(state, obj)                      \ | 
					
						
							|  |  |  |      || PyObject_TypeCheck(obj, state->FutureType)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define Task_Check(state, obj)                          \
 | 
					
						
							|  |  |  |     (Task_CheckExact(state, obj)                        \ | 
					
						
							|  |  |  |      || PyObject_TypeCheck(obj, state->TaskType)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This macro is optimized to quickly return for native Future *or* Task
 | 
					
						
							|  |  |  | // objects by inlining fast "exact" checks to be called first.
 | 
					
						
							|  |  |  | #define TaskOrFuture_Check(state, obj)                  \
 | 
					
						
							|  |  |  |     (Task_CheckExact(state, obj)                        \ | 
					
						
							|  |  |  |      || Future_CheckExact(state, obj)                   \ | 
					
						
							|  |  |  |      || PyObject_TypeCheck(obj, state->FutureType)      \ | 
					
						
							|  |  |  |      || PyObject_TypeCheck(obj, state->TaskType)) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | typedef struct _Py_AsyncioModuleDebugOffsets { | 
					
						
							|  |  |  |     struct _asyncio_task_object { | 
					
						
							|  |  |  |         uint64_t size; | 
					
						
							|  |  |  |         uint64_t task_name; | 
					
						
							|  |  |  |         uint64_t task_awaited_by; | 
					
						
							|  |  |  |         uint64_t task_is_task; | 
					
						
							|  |  |  |         uint64_t task_awaited_by_is_set; | 
					
						
							|  |  |  |         uint64_t task_coro; | 
					
						
							|  |  |  |     } asyncio_task_object; | 
					
						
							|  |  |  |     struct _asyncio_thread_state { | 
					
						
							|  |  |  |         uint64_t size; | 
					
						
							|  |  |  |         uint64_t asyncio_running_loop; | 
					
						
							|  |  |  |         uint64_t asyncio_running_task; | 
					
						
							|  |  |  |     } asyncio_thread_state; | 
					
						
							|  |  |  | } Py_AsyncioModuleDebugOffsets; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERATE_DEBUG_SECTION(AsyncioDebug, Py_AsyncioModuleDebugOffsets AsyncioDebug) | 
					
						
							|  |  |  |     = {.asyncio_task_object = { | 
					
						
							|  |  |  |            .size = sizeof(TaskObj), | 
					
						
							|  |  |  |            .task_name = offsetof(TaskObj, task_name), | 
					
						
							|  |  |  |            .task_awaited_by = offsetof(TaskObj, task_awaited_by), | 
					
						
							|  |  |  |            .task_is_task = offsetof(TaskObj, task_is_task), | 
					
						
							|  |  |  |            .task_awaited_by_is_set = offsetof(TaskObj, task_awaited_by_is_set), | 
					
						
							|  |  |  |            .task_coro = offsetof(TaskObj, task_coro), | 
					
						
							|  |  |  |        }, | 
					
						
							|  |  |  |        .asyncio_thread_state = { | 
					
						
							|  |  |  |            .size = sizeof(_PyThreadStateImpl), | 
					
						
							|  |  |  |            .asyncio_running_loop = offsetof(_PyThreadStateImpl, asyncio_running_loop), | 
					
						
							|  |  |  |            .asyncio_running_task = offsetof(_PyThreadStateImpl, asyncio_running_task), | 
					
						
							|  |  |  |        }}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-15 15:39:19 +09:00
										 |  |  | /* State of the _asyncio module */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     PyTypeObject *FutureIterType; | 
					
						
							|  |  |  |     PyTypeObject *TaskStepMethWrapper_Type; | 
					
						
							|  |  |  |     PyTypeObject *FutureType; | 
					
						
							|  |  |  |     PyTypeObject *TaskType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *asyncio_mod; | 
					
						
							|  |  |  |     PyObject *context_kwname; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     /* WeakSet containing scheduled 3rd party tasks which don't
 | 
					
						
							|  |  |  |        inherit from native asyncio.Task */ | 
					
						
							|  |  |  |     PyObject *non_asyncio_tasks; | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     /* Set containing all 3rd party eagerly executing tasks which don't
 | 
					
						
							|  |  |  |        inherit from native asyncio.Task */ | 
					
						
							|  |  |  |     PyObject *non_asyncio_eager_tasks; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     /* An isinstance type cache for the 'is_coroutine()' function. */ | 
					
						
							|  |  |  |     PyObject *iscoroutine_typecache; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     /* Imports from asyncio.events. */ | 
					
						
							|  |  |  |     PyObject *asyncio_get_event_loop_policy; | 
					
						
							| 
									
										
										
										
											2018-01-23 15:10:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     /* Imports from asyncio.base_futures. */ | 
					
						
							|  |  |  |     PyObject *asyncio_future_repr_func; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     /* Imports from asyncio.exceptions. */ | 
					
						
							|  |  |  |     PyObject *asyncio_CancelledError; | 
					
						
							|  |  |  |     PyObject *asyncio_InvalidStateError; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     /* Imports from asyncio.base_tasks. */ | 
					
						
							|  |  |  |     PyObject *asyncio_task_get_stack_func; | 
					
						
							|  |  |  |     PyObject *asyncio_task_print_stack_func; | 
					
						
							|  |  |  |     PyObject *asyncio_task_repr_func; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     /* Imports from asyncio.coroutines. */ | 
					
						
							|  |  |  |     PyObject *asyncio_iscoroutine_func; | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     /* Imports from traceback. */ | 
					
						
							|  |  |  |     PyObject *traceback_extract_stack; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Counter for autogenerated Task names */ | 
					
						
							|  |  |  |     uint64_t task_name_counter; | 
					
						
							| 
									
										
										
										
											2023-05-05 16:42:58 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | } asyncio_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline asyncio_state * | 
					
						
							|  |  |  | get_asyncio_state(PyObject *mod) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = _PyModule_GetState(mod); | 
					
						
							|  |  |  |     assert(state != NULL); | 
					
						
							|  |  |  |     return state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline asyncio_state * | 
					
						
							|  |  |  | get_asyncio_state_by_cls(PyTypeObject *cls) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-24 21:16:29 +01:00
										 |  |  |     asyncio_state *state = (asyncio_state *)_PyType_GetModuleState(cls); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     assert(state != NULL); | 
					
						
							|  |  |  |     return state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef _asynciomodule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline asyncio_state * | 
					
						
							|  |  |  | get_asyncio_state_by_def(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							|  |  |  |     PyObject *mod = PyType_GetModuleByDef(tp, &_asynciomodule); | 
					
						
							|  |  |  |     assert(mod != NULL); | 
					
						
							|  |  |  |     return get_asyncio_state(mod); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | #include "clinic/_asynciomodule.c.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | class _asyncio.Future "FutureObj *" "&Future_Type" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=00d3e4abca711e0f]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /* Get FutureIter from Future */ | 
					
						
							| 
									
										
										
										
											2018-01-23 15:10:03 -05:00
										 |  |  | static PyObject * future_new_iter(PyObject *); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *result); | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  | static void unregister_task(TaskObj *task); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | static void | 
					
						
							|  |  |  | clear_task_coro(TaskObj *task) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_CLEAR(task->task_coro); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | set_task_coro(TaskObj *task, PyObject *coro) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(coro != NULL); | 
					
						
							|  |  |  |     Py_INCREF(coro); | 
					
						
							|  |  |  |     Py_XSETREF(task->task_coro, coro); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _is_coroutine(asyncio_state *state, PyObject *coro) | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     /* 'coro' is not a native coroutine, call asyncio.iscoroutine()
 | 
					
						
							|  |  |  |        to check if it's another coroutine flavour. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        Do this check after 'future_init()'; in case we need to raise | 
					
						
							|  |  |  |        an error, __del__ needs a properly initialized object. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     PyObject *res = PyObject_CallOneArg(state->asyncio_iscoroutine_func, coro); | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |     if (res == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int is_res_true = PyObject_IsTrue(res); | 
					
						
							|  |  |  |     Py_DECREF(res); | 
					
						
							|  |  |  |     if (is_res_true <= 0) { | 
					
						
							|  |  |  |         return is_res_true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (PySet_GET_SIZE(state->iscoroutine_typecache) < 100) { | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |         /* Just in case we don't want to cache more than 100
 | 
					
						
							|  |  |  |            positive types.  That shouldn't ever happen, unless | 
					
						
							|  |  |  |            someone stressing the system on purpose. | 
					
						
							|  |  |  |         */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         if (PySet_Add(state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) { | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | is_coroutine(asyncio_state *state, PyObject *coro) | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     if (PyCoro_CheckExact(coro)) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check if `type(coro)` is in the cache.
 | 
					
						
							|  |  |  |        Caching makes is_coroutine() function almost as fast as | 
					
						
							|  |  |  |        PyCoro_CheckExact() for non-native coroutine-like objects | 
					
						
							|  |  |  |        (like coroutines compiled with Cython). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        asyncio.iscoroutine() has its own type caching mechanism. | 
					
						
							|  |  |  |        This cache allows us to avoid the cost of even calling | 
					
						
							|  |  |  |        a pure-Python function in 99.9% cases. | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     int has_it = PySet_Contains( | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro)); | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |     if (has_it == 0) { | 
					
						
							|  |  |  |         /* type(coro) is not in iscoroutine_typecache */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         return _is_coroutine(state, coro); | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 18:36:10 -06:00
										 |  |  |     /* either an error has occurred or
 | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |        type(coro) is in iscoroutine_typecache | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     return has_it; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | get_future_loop(asyncio_state *state, PyObject *fut) | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     /* Implementation of `asyncio.futures._get_loop` */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |     PyObject *getloop; | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (Future_CheckExact(state, fut) || Task_CheckExact(state, fut)) { | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |         PyObject *loop = ((FutureObj *)fut)->fut_loop; | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |         return Py_NewRef(loop); | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 08:57:10 +03:00
										 |  |  |     if (PyObject_GetOptionalAttr(fut, &_Py_ID(get_loop), &getloop) < 0) { | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     if (getloop != NULL) { | 
					
						
							| 
									
										
										
										
											2019-06-17 14:27:23 +02:00
										 |  |  |         PyObject *res = PyObject_CallNoArgs(getloop); | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |         Py_DECREF(getloop); | 
					
						
							|  |  |  |         return res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     return PyObject_GetAttr(fut, &_Py_ID(_loop)); | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  | get_event_loop(asyncio_state *state) | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *loop; | 
					
						
							|  |  |  |     PyObject *policy; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-17 18:21:24 -04:00
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2024-07-17 01:09:58 +08:00
										 |  |  |     loop = Py_XNewRef(ts->asyncio_running_loop); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     if (loop != NULL) { | 
					
						
							|  |  |  |         return loop; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     policy = PyObject_CallNoArgs(state->asyncio_get_event_loop_policy); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     if (policy == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     loop = PyObject_CallMethodNoArgs(policy, &_Py_ID(get_event_loop)); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     Py_DECREF(policy); | 
					
						
							|  |  |  |     return loop; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | call_soon(asyncio_state *state, PyObject *loop, PyObject *func, PyObject *arg, | 
					
						
							|  |  |  |           PyObject *ctx) | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *handle; | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ctx == NULL) { | 
					
						
							| 
									
										
										
										
											2023-04-03 18:14:32 +05:30
										 |  |  |         PyObject *stack[] = {loop, func, arg}; | 
					
						
							|  |  |  |         size_t nargsf = 3 | PY_VECTORCALL_ARGUMENTS_OFFSET; | 
					
						
							|  |  |  |         handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf, NULL); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         /* All refs in 'stack' are borrowed. */ | 
					
						
							| 
									
										
										
										
											2023-04-03 18:14:32 +05:30
										 |  |  |         PyObject *stack[4]; | 
					
						
							|  |  |  |         size_t nargs = 2; | 
					
						
							|  |  |  |         stack[0] = loop; | 
					
						
							|  |  |  |         stack[1] = func; | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         if (arg != NULL) { | 
					
						
							| 
									
										
										
										
											2023-04-03 18:14:32 +05:30
										 |  |  |             stack[2] = arg; | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |             nargs++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         stack[nargs] = (PyObject *)ctx; | 
					
						
							| 
									
										
										
										
											2023-04-03 18:14:32 +05:30
										 |  |  |         size_t nargsf = nargs | PY_VECTORCALL_ARGUMENTS_OFFSET; | 
					
						
							|  |  |  |         handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf, | 
					
						
							|  |  |  |                                            state->context_kwname); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (handle == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(handle); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | future_is_alive(FutureObj *fut) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return fut->fut_loop != NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | future_ensure_alive(FutureObj *fut) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!future_is_alive(fut)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_RuntimeError, | 
					
						
							|  |  |  |                         "Future object is not initialized."); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | #define ENSURE_FUTURE_ALIVE(state, fut)                             \
 | 
					
						
							|  |  |  |     do {                                                            \ | 
					
						
							|  |  |  |         assert(Future_Check(state, fut) || Task_Check(state, fut)); \ | 
					
						
							|  |  |  |         (void)state;                                                \ | 
					
						
							|  |  |  |         if (future_ensure_alive((FutureObj*)fut)) {                 \ | 
					
						
							|  |  |  |             return NULL;                                            \ | 
					
						
							|  |  |  |         }                                                           \ | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     } while(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_schedule_callbacks(asyncio_state *state, FutureObj *fut) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-13 13:19:53 +05:30
										 |  |  |     assert(fut->fut_state != STATE_PENDING); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (Task_Check(state, fut)) { | 
					
						
							|  |  |  |         // remove task from linked-list of tasks
 | 
					
						
							|  |  |  |         // as it is finished now
 | 
					
						
							|  |  |  |         TaskObj *task = (TaskObj *)fut; | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  |         unregister_task(task); | 
					
						
							| 
									
										
										
										
											2025-02-13 13:19:53 +05:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (fut->fut_callback0 != NULL) { | 
					
						
							|  |  |  |         /* There's a 1st callback */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-27 16:04:43 +01:00
										 |  |  |         // Beware: An evil call_soon could alter fut_callback0 or fut_context0.
 | 
					
						
							|  |  |  |         // Since we are anyway clearing them after the call, whether call_soon
 | 
					
						
							|  |  |  |         // succeeds or not, the idea is to transfer ownership so that external
 | 
					
						
							|  |  |  |         // code is not able to alter them during the call.
 | 
					
						
							|  |  |  |         PyObject *fut_callback0 = fut->fut_callback0; | 
					
						
							|  |  |  |         fut->fut_callback0 = NULL; | 
					
						
							|  |  |  |         PyObject *fut_context0 = fut->fut_context0; | 
					
						
							|  |  |  |         fut->fut_context0 = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         int ret = call_soon(state, fut->fut_loop, fut_callback0, | 
					
						
							|  |  |  |                             (PyObject *)fut, fut_context0); | 
					
						
							|  |  |  |         Py_CLEAR(fut_callback0); | 
					
						
							|  |  |  |         Py_CLEAR(fut_context0); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         if (ret) { | 
					
						
							|  |  |  |             /* If an error occurs in pure-Python implementation,
 | 
					
						
							|  |  |  |                all callbacks are cleared. */ | 
					
						
							|  |  |  |             Py_CLEAR(fut->fut_callbacks); | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* we called the first callback, now try calling
 | 
					
						
							|  |  |  |            callbacks from the 'fut_callbacks' list. */ | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (fut->fut_callbacks == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         /* No more callbacks, return. */ | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 20:15:09 +02:00
										 |  |  |     // Beware: An evil call_soon could change fut->fut_callbacks.
 | 
					
						
							|  |  |  |     // The idea is to transfer the ownership of the callbacks list
 | 
					
						
							|  |  |  |     // so that external code is not able to mutate the list during
 | 
					
						
							|  |  |  |     // the iteration.
 | 
					
						
							|  |  |  |     PyObject *callbacks = fut->fut_callbacks; | 
					
						
							|  |  |  |     fut->fut_callbacks = NULL; | 
					
						
							|  |  |  |     Py_ssize_t n = PyList_GET_SIZE(callbacks); | 
					
						
							|  |  |  |     for (Py_ssize_t i = 0; i < n; i++) { | 
					
						
							|  |  |  |         assert(PyList_GET_SIZE(callbacks) == n); | 
					
						
							|  |  |  |         PyObject *cb_tup = PyList_GET_ITEM(callbacks, i); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         PyObject *cb = PyTuple_GET_ITEM(cb_tup, 0); | 
					
						
							|  |  |  |         PyObject *ctx = PyTuple_GET_ITEM(cb_tup, 1); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         if (call_soon(state, fut->fut_loop, cb, (PyObject *)fut, ctx)) { | 
					
						
							| 
									
										
										
										
											2024-10-25 20:15:09 +02:00
										 |  |  |             Py_DECREF(callbacks); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-25 20:15:09 +02:00
										 |  |  |     Py_DECREF(callbacks); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | future_init(FutureObj *fut, PyObject *loop) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     int is_true; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  |     Py_CLEAR(fut->fut_loop); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_callback0); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_context0); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_callbacks); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_result); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_exception); | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |     Py_CLEAR(fut->fut_exception_tb); | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  |     Py_CLEAR(fut->fut_source_tb); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     Py_CLEAR(fut->fut_cancel_msg); | 
					
						
							| 
									
										
										
										
											2022-02-21 22:59:04 +02:00
										 |  |  |     Py_CLEAR(fut->fut_cancelled_exc); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     Py_CLEAR(fut->fut_awaited_by); | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fut->fut_state = STATE_PENDING; | 
					
						
							|  |  |  |     fut->fut_log_tb = 0; | 
					
						
							|  |  |  |     fut->fut_blocking = 0; | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     fut->fut_awaited_by_is_set = 0; | 
					
						
							|  |  |  |     fut->fut_is_task = 0; | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     if (loop == Py_None) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         loop = get_event_loop(state); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         if (loop == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_INCREF(loop); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  |     fut->fut_loop = loop; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     res = PyObject_CallMethodNoArgs(fut->fut_loop, &_Py_ID(get_debug)); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (res == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     is_true = PyObject_IsTrue(res); | 
					
						
							|  |  |  |     Py_DECREF(res); | 
					
						
							|  |  |  |     if (is_true < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-02 18:37:37 +02:00
										 |  |  |     if (is_true && !_Py_IsInterpreterFinalizing(_PyInterpreterState_GET())) { | 
					
						
							| 
									
										
										
										
											2018-05-28 11:11:31 -04:00
										 |  |  |         /* Only try to capture the traceback if the interpreter is not being
 | 
					
						
							| 
									
										
										
										
											2023-08-18 12:34:41 +02:00
										 |  |  |            finalized.  The original motivation to add a `Py_IsFinalizing()` | 
					
						
							| 
									
										
										
										
											2018-05-28 11:11:31 -04:00
										 |  |  |            call was to prevent SIGSEGV when a Future is created in a __del__ | 
					
						
							|  |  |  |            method, which is called during the interpreter shutdown and the | 
					
						
							|  |  |  |            traceback module is already unloaded. | 
					
						
							|  |  |  |         */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); | 
					
						
							|  |  |  |         fut->fut_source_tb = PyObject_CallNoArgs(state->traceback_extract_stack); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         if (fut->fut_source_tb == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  | future_awaited_by_add(asyncio_state *state, FutureObj *fut, PyObject *thing) | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  |     // We only want to support native asyncio Futures.
 | 
					
						
							|  |  |  |     // For further insight see the comment in the Python
 | 
					
						
							|  |  |  |     // implementation of "future_add_to_awaited_by()".
 | 
					
						
							|  |  |  |     assert(TaskOrFuture_Check(state, fut)); | 
					
						
							|  |  |  |     assert(TaskOrFuture_Check(state, thing)); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Most futures/task are only awaited by one entity, so we want
 | 
					
						
							|  |  |  |        to avoid always creating a set for `fut_awaited_by`. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (fut->fut_awaited_by == NULL) { | 
					
						
							|  |  |  |         assert(!fut->fut_awaited_by_is_set); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         Py_INCREF(thing); | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |         fut->fut_awaited_by = thing; | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (fut->fut_awaited_by_is_set) { | 
					
						
							|  |  |  |         assert(PySet_CheckExact(fut->fut_awaited_by)); | 
					
						
							|  |  |  |         return PySet_Add(fut->fut_awaited_by, thing); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *set = PySet_New(NULL); | 
					
						
							|  |  |  |     if (set == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PySet_Add(set, thing)) { | 
					
						
							|  |  |  |         Py_DECREF(set); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (PySet_Add(set, fut->fut_awaited_by)) { | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         Py_DECREF(set); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     Py_SETREF(fut->fut_awaited_by, set); | 
					
						
							|  |  |  |     fut->fut_awaited_by_is_set = 1; | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  | future_awaited_by_discard(asyncio_state *state, FutureObj *fut, PyObject *thing) | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  |     // We only want to support native asyncio Futures.
 | 
					
						
							|  |  |  |     // For further insight see the comment in the Python
 | 
					
						
							|  |  |  |     // implementation of "future_add_to_awaited_by()".
 | 
					
						
							|  |  |  |     assert(TaskOrFuture_Check(state, fut)); | 
					
						
							|  |  |  |     assert(TaskOrFuture_Check(state, thing)); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Following the semantics of 'set.discard()' here in not
 | 
					
						
							|  |  |  |        raising an error if `thing` isn't in the `awaited_by` "set". | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (fut->fut_awaited_by == NULL) { | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (fut->fut_awaited_by == thing) { | 
					
						
							|  |  |  |         Py_CLEAR(fut->fut_awaited_by); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (fut->fut_awaited_by_is_set) { | 
					
						
							|  |  |  |         assert(PySet_CheckExact(fut->fut_awaited_by)); | 
					
						
							|  |  |  |         int err = PySet_Discard(fut->fut_awaited_by, thing); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         if (err < 0) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_set_result(asyncio_state *state, FutureObj *fut, PyObject *res) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (future_ensure_alive(fut)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (fut->fut_state != STATE_PENDING) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     assert(!fut->fut_result); | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |     fut->fut_result = Py_NewRef(res); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     fut->fut_state = STATE_FINISHED; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (future_schedule_callbacks(state, fut) == -1) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_set_exception(asyncio_state *state, FutureObj *fut, PyObject *exc) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     PyObject *exc_val = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fut->fut_state != STATE_PENDING) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyExceptionClass_Check(exc)) { | 
					
						
							| 
									
										
										
										
											2019-06-17 14:27:23 +02:00
										 |  |  |         exc_val = PyObject_CallNoArgs(exc); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         if (exc_val == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         if (fut->fut_state != STATE_PENDING) { | 
					
						
							|  |  |  |             Py_DECREF(exc_val); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |         exc_val = Py_NewRef(exc); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (!PyExceptionInstance_Check(exc_val)) { | 
					
						
							|  |  |  |         Py_DECREF(exc_val); | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "invalid exception object"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-01-10 16:21:00 +11:00
										 |  |  |     if (PyErr_GivenExceptionMatches(exc_val, PyExc_StopIteration)) { | 
					
						
							|  |  |  |         const char *msg = "StopIteration interacts badly with " | 
					
						
							|  |  |  |                           "generators and cannot be raised into a " | 
					
						
							|  |  |  |                           "Future"; | 
					
						
							|  |  |  |         PyObject *message = PyUnicode_FromString(msg); | 
					
						
							|  |  |  |         if (message == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(exc_val); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyObject *err = PyObject_CallOneArg(PyExc_RuntimeError, message); | 
					
						
							|  |  |  |         Py_DECREF(message); | 
					
						
							|  |  |  |         if (err == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(exc_val); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         assert(PyExceptionInstance_Check(err)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         PyException_SetCause(err, Py_NewRef(exc_val)); | 
					
						
							|  |  |  |         PyException_SetContext(err, Py_NewRef(exc_val)); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_DECREF(exc_val); | 
					
						
							| 
									
										
										
										
											2024-01-10 16:21:00 +11:00
										 |  |  |         exc_val = err; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     assert(!fut->fut_exception); | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |     assert(!fut->fut_exception_tb); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     fut->fut_exception = exc_val; | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |     fut->fut_exception_tb = PyException_GetTraceback(exc_val); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     fut->fut_state = STATE_FINISHED; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (future_schedule_callbacks(state, fut) == -1) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fut->fut_log_tb = 1; | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | create_cancelled_error(asyncio_state *state, FutureObj *fut) | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     PyObject *exc; | 
					
						
							| 
									
										
										
										
											2022-02-21 22:59:04 +02:00
										 |  |  |     if (fut->fut_cancelled_exc != NULL) { | 
					
						
							|  |  |  |         /* transfer ownership */ | 
					
						
							|  |  |  |         exc = fut->fut_cancelled_exc; | 
					
						
							|  |  |  |         fut->fut_cancelled_exc = NULL; | 
					
						
							|  |  |  |         return exc; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *msg = fut->fut_cancel_msg; | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     if (msg == NULL || msg == Py_None) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         exc = PyObject_CallNoArgs(state->asyncio_CancelledError); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         exc = PyObject_CallOneArg(state->asyncio_CancelledError, msg); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     return exc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_set_cancelled_error(asyncio_state *state, FutureObj *fut) | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     PyObject *exc = create_cancelled_error(state, fut); | 
					
						
							| 
									
										
										
										
											2022-08-15 16:32:40 -07:00
										 |  |  |     if (exc == NULL) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     PyErr_SetObject(state->asyncio_CancelledError, exc); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     Py_DECREF(exc); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_get_result(asyncio_state *state, FutureObj *fut, PyObject **result) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (fut->fut_state == STATE_CANCELLED) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         future_set_cancelled_error(state, fut); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fut->fut_state != STATE_FINISHED) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyErr_SetString(state->asyncio_InvalidStateError, | 
					
						
							|  |  |  |                         "Result is not set."); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fut->fut_log_tb = 0; | 
					
						
							|  |  |  |     if (fut->fut_exception != NULL) { | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |         PyObject *tb = fut->fut_exception_tb; | 
					
						
							|  |  |  |         if (tb == NULL) { | 
					
						
							|  |  |  |             tb = Py_None; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (PyException_SetTraceback(fut->fut_exception, tb) < 0) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |         *result = Py_NewRef(fut->fut_exception); | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |         Py_CLEAR(fut->fut_exception_tb); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |     *result = Py_NewRef(fut->fut_result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg, | 
					
						
							|  |  |  |                          PyObject *ctx) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (!future_is_alive(fut)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_RuntimeError, "uninitialized Future object"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (fut->fut_state != STATE_PENDING) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         /* The future is done/cancelled, so schedule the callback
 | 
					
						
							|  |  |  |            right away. */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         if (call_soon(state, fut->fut_loop, arg, (PyObject*) fut, ctx)) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         /* The future is pending, add a callback.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            Callbacks in the future object are stored as follows: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               callback0 -- a pointer to the first callback | 
					
						
							|  |  |  |               callbacks -- a list of 2nd, 3rd, ... callbacks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            Invariants: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             * callbacks != NULL: | 
					
						
							|  |  |  |                 There are some callbacks in in the list.  Just | 
					
						
							|  |  |  |                 add the new callback to it. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             * callbacks == NULL and callback0 == NULL: | 
					
						
							|  |  |  |                 This is the first callback.  Set it to callback0. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             * callbacks == NULL and callback0 != NULL: | 
					
						
							|  |  |  |                 This is a second callback.  Initialize callbacks | 
					
						
							|  |  |  |                 with a new list and add the new callback to it. | 
					
						
							|  |  |  |         */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         if (fut->fut_callbacks == NULL && fut->fut_callback0 == NULL) { | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |             fut->fut_callback0 = Py_NewRef(arg); | 
					
						
							|  |  |  |             fut->fut_context0 = Py_NewRef(ctx); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |             PyObject *tup = PyTuple_New(2); | 
					
						
							|  |  |  |             if (tup == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Py_INCREF(arg); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |             PyTuple_SET_ITEM(tup, 0, arg); | 
					
						
							|  |  |  |             Py_INCREF(ctx); | 
					
						
							|  |  |  |             PyTuple_SET_ITEM(tup, 1, (PyObject *)ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (fut->fut_callbacks != NULL) { | 
					
						
							|  |  |  |                 int err = PyList_Append(fut->fut_callbacks, tup); | 
					
						
							|  |  |  |                 if (err) { | 
					
						
							|  |  |  |                     Py_DECREF(tup); | 
					
						
							|  |  |  |                     return NULL; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 Py_DECREF(tup); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 fut->fut_callbacks = PyList_New(1); | 
					
						
							|  |  |  |                 if (fut->fut_callbacks == NULL) { | 
					
						
							| 
									
										
										
										
											2020-05-30 02:22:02 -06:00
										 |  |  |                     Py_DECREF(tup); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |                     return NULL; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 PyList_SET_ITEM(fut->fut_callbacks, 0, tup);  /* borrow */ | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | future_cancel(asyncio_state *state, FutureObj *fut, PyObject *msg) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  |     fut->fut_log_tb = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (fut->fut_state != STATE_PENDING) { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     fut->fut_state = STATE_CANCELLED; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     Py_XINCREF(msg); | 
					
						
							|  |  |  |     Py_XSETREF(fut->fut_cancel_msg, msg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (future_schedule_callbacks(state, fut) == -1) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Future.__init__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     * | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     loop: object = None | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | This class is *almost* compatible with concurrent.futures.Future. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Differences: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - result() and exception() do not take a timeout argument and | 
					
						
							|  |  |  |       raise an exception when the future isn't done yet. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - Callbacks registered with add_done_callback() are always called | 
					
						
							|  |  |  |       via the event loop's call_soon_threadsafe(). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - This class is not compatible with the wait() and as_completed() | 
					
						
							|  |  |  |       methods in the concurrent.futures package. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _asyncio_Future___init___impl(FutureObj *self, PyObject *loop) | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  | /*[clinic end generated code: output=9ed75799eaccb5d6 input=89af317082bc0bf8]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return future_init(self, loop); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureObj_clear(PyObject *op) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     FutureObj *fut = (FutureObj*)op; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_CLEAR(fut->fut_loop); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     Py_CLEAR(fut->fut_callback0); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     Py_CLEAR(fut->fut_context0); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_CLEAR(fut->fut_callbacks); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_result); | 
					
						
							|  |  |  |     Py_CLEAR(fut->fut_exception); | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |     Py_CLEAR(fut->fut_exception_tb); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_CLEAR(fut->fut_source_tb); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     Py_CLEAR(fut->fut_cancel_msg); | 
					
						
							| 
									
										
										
										
											2022-02-21 22:59:04 +02:00
										 |  |  |     Py_CLEAR(fut->fut_cancelled_exc); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     Py_CLEAR(fut->fut_awaited_by); | 
					
						
							|  |  |  |     fut->fut_awaited_by_is_set = 0; | 
					
						
							| 
									
										
										
										
											2023-10-02 19:24:08 +02:00
										 |  |  |     PyObject_ClearManagedDict((PyObject *)fut); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureObj_traverse(PyObject *op, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     FutureObj *fut = (FutureObj*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_VISIT(Py_TYPE(fut)); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_VISIT(fut->fut_loop); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     Py_VISIT(fut->fut_callback0); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     Py_VISIT(fut->fut_context0); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_VISIT(fut->fut_callbacks); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_result); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_exception); | 
					
						
							| 
									
										
										
										
											2022-07-11 18:02:11 +05:30
										 |  |  |     Py_VISIT(fut->fut_exception_tb); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_VISIT(fut->fut_source_tb); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     Py_VISIT(fut->fut_cancel_msg); | 
					
						
							| 
									
										
										
										
											2022-02-21 22:59:04 +02:00
										 |  |  |     Py_VISIT(fut->fut_cancelled_exc); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     Py_VISIT(fut->fut_awaited_by); | 
					
						
							| 
									
										
										
										
											2023-10-02 19:24:08 +02:00
										 |  |  |     PyObject_VisitManagedDict((PyObject *)fut, visit, arg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return the result this future represents. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the future has been cancelled, raises CancelledError.  If the | 
					
						
							|  |  |  | future's result isn't yet available, raises InvalidStateError.  If | 
					
						
							|  |  |  | the future is done and has an exception set, this exception is raised. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Future_result_impl(FutureObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=f35f940936a4b1e5 input=61d89f48e4c8b670]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     PyObject *result; | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!future_is_alive(self)) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyErr_SetString(state->asyncio_InvalidStateError, | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |                         "Future object is not initialized."); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     int res = future_get_result(state, self, &result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (res == -1) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (res == 0) { | 
					
						
							|  |  |  |         return result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(res == 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyErr_SetObject(PyExceptionInstance_Class(result), result); | 
					
						
							|  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.exception | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | Return the exception that was set on this future. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The exception (or None if no exception was set) is returned only if | 
					
						
							|  |  |  | the future is done.  If the future has been cancelled, raises | 
					
						
							|  |  |  | CancelledError.  If the future isn't done yet, raises | 
					
						
							|  |  |  | InvalidStateError. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_exception_impl(FutureObj *self, PyTypeObject *cls) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=ce75576b187c905b input=647d1fd1fc403301]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (!future_is_alive(self)) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |         PyErr_SetString(state->asyncio_InvalidStateError, | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |                         "Future object is not initialized."); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (self->fut_state == STATE_CANCELLED) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |         future_set_cancelled_error(state, self); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (self->fut_state != STATE_FINISHED) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |         PyErr_SetString(state->asyncio_InvalidStateError, | 
					
						
							|  |  |  |                         "Exception is not set."); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (self->fut_exception != NULL) { | 
					
						
							|  |  |  |         self->fut_log_tb = 0; | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |         return Py_NewRef(self->fut_exception); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.set_result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  |     result: object | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Mark the future done and set its result. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the future is already done when this method is called, raises | 
					
						
							|  |  |  | InvalidStateError. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_set_result_impl(FutureObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                                 PyObject *result) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=99afbbe78f99c32d input=4069306f03a3b6ee]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							|  |  |  |     return future_set_result(state, self, result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.set_exception | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     exception: object | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Mark the future done and set an exception. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the future is already done when this method is called, raises | 
					
						
							|  |  |  | InvalidStateError. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_set_exception_impl(FutureObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                                    PyObject *exception) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=0a5e8b5a52f058d6 input=b6eab43a389bc966]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							|  |  |  |     return future_set_exception(state, self, exception); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.add_done_callback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     fn: object | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     / | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     * | 
					
						
							|  |  |  |     context: object = NULL | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Add a callback to be run when the future becomes done. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The callback is called with a single argument - the future object. If | 
					
						
							|  |  |  | the future is already done when this is called, the callback is | 
					
						
							|  |  |  | scheduled with call_soon. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_add_done_callback_impl(FutureObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                                        PyObject *fn, PyObject *context) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=922e9a4cbd601167 input=37d97f941beb7b3e]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     if (context == NULL) { | 
					
						
							| 
									
										
										
										
											2018-09-27 14:55:55 -04:00
										 |  |  |         context = PyContext_CopyCurrent(); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         if (context == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyObject *res = future_add_done_callback(state, self, fn, context); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         Py_DECREF(context); | 
					
						
							|  |  |  |         return res; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     return future_add_done_callback(state, self, fn, context); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.remove_done_callback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     fn: object | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Remove all instances of a callback from the "call when done" list. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns the number of callbacks removed. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                                           PyObject *fn) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=2da35ccabfe41b98 input=3afbc9f6a673091b]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *newlist; | 
					
						
							|  |  |  |     Py_ssize_t len, i, j=0; | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     Py_ssize_t cleared_callback0 = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (self->fut_callback0 != NULL) { | 
					
						
							| 
									
										
										
										
											2024-10-27 18:10:10 +01:00
										 |  |  |         // Beware: An evil PyObject_RichCompareBool could free fut_callback0
 | 
					
						
							|  |  |  |         // before a recursive call is made with that same arg. For details, see
 | 
					
						
							|  |  |  |         // https://github.com/python/cpython/pull/125967#discussion_r1816593340.
 | 
					
						
							|  |  |  |         PyObject *fut_callback0 = Py_NewRef(self->fut_callback0); | 
					
						
							|  |  |  |         int cmp = PyObject_RichCompareBool(fut_callback0, fn, Py_EQ); | 
					
						
							|  |  |  |         Py_DECREF(fut_callback0); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         if (cmp == -1) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (cmp == 1) { | 
					
						
							|  |  |  |             /* callback0 == fn */ | 
					
						
							|  |  |  |             Py_CLEAR(self->fut_callback0); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |             Py_CLEAR(self->fut_context0); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |             cleared_callback0 = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     if (self->fut_callbacks == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         return PyLong_FromSsize_t(cleared_callback0); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     len = PyList_GET_SIZE(self->fut_callbacks); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (len == 0) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         Py_CLEAR(self->fut_callbacks); | 
					
						
							|  |  |  |         return PyLong_FromSsize_t(cleared_callback0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (len == 1) { | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         PyObject *cb_tup = PyList_GET_ITEM(self->fut_callbacks, 0); | 
					
						
							| 
									
										
										
										
											2024-11-12 18:01:34 +05:30
										 |  |  |         Py_INCREF(cb_tup); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         int cmp = PyObject_RichCompareBool( | 
					
						
							| 
									
										
										
										
											2019-08-04 14:12:48 +03:00
										 |  |  |             PyTuple_GET_ITEM(cb_tup, 0), fn, Py_EQ); | 
					
						
							| 
									
										
										
										
											2024-11-12 18:01:34 +05:30
										 |  |  |         Py_DECREF(cb_tup); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         if (cmp == -1) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (cmp == 1) { | 
					
						
							|  |  |  |             /* callbacks[0] == fn */ | 
					
						
							|  |  |  |             Py_CLEAR(self->fut_callbacks); | 
					
						
							|  |  |  |             return PyLong_FromSsize_t(1 + cleared_callback0); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /* callbacks[0] != fn and len(callbacks) == 1 */ | 
					
						
							|  |  |  |         return PyLong_FromSsize_t(cleared_callback0); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     newlist = PyList_New(len); | 
					
						
							|  |  |  |     if (newlist == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 12:57:09 -07:00
										 |  |  |     // Beware: PyObject_RichCompareBool below may change fut_callbacks.
 | 
					
						
							|  |  |  |     // See GH-97592.
 | 
					
						
							|  |  |  |     for (i = 0; | 
					
						
							|  |  |  |          self->fut_callbacks != NULL && i < PyList_GET_SIZE(self->fut_callbacks); | 
					
						
							|  |  |  |          i++) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         int ret; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         Py_INCREF(item); | 
					
						
							| 
									
										
										
										
											2019-08-04 14:12:48 +03:00
										 |  |  |         ret = PyObject_RichCompareBool(PyTuple_GET_ITEM(item, 0), fn, Py_EQ); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         if (ret == 0) { | 
					
						
							| 
									
										
										
										
											2017-07-05 13:32:03 -04:00
										 |  |  |             if (j < len) { | 
					
						
							|  |  |  |                 PyList_SET_ITEM(newlist, j, item); | 
					
						
							|  |  |  |                 j++; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2017-07-05 13:32:03 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |             ret = PyList_Append(newlist, item); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(item); | 
					
						
							|  |  |  |         if (ret < 0) { | 
					
						
							|  |  |  |             goto fail; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 12:57:09 -07:00
										 |  |  |     // Note: fut_callbacks may have been cleared.
 | 
					
						
							|  |  |  |     if (j == 0 || self->fut_callbacks == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         Py_CLEAR(self->fut_callbacks); | 
					
						
							|  |  |  |         Py_DECREF(newlist); | 
					
						
							|  |  |  |         return PyLong_FromSsize_t(len + cleared_callback0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     if (j < len) { | 
					
						
							| 
									
										
										
										
											2020-02-07 23:18:08 +01:00
										 |  |  |         Py_SET_SIZE(newlist, j); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     j = PyList_GET_SIZE(newlist); | 
					
						
							|  |  |  |     len = PyList_GET_SIZE(self->fut_callbacks); | 
					
						
							|  |  |  |     if (j != len) { | 
					
						
							|  |  |  |         if (PyList_SetSlice(self->fut_callbacks, 0, len, newlist) < 0) { | 
					
						
							|  |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(newlist); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     return PyLong_FromSsize_t(len - j + cleared_callback0); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     Py_DECREF(newlist); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.cancel | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							|  |  |  |     / | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     msg: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | Cancel the future and schedule callbacks. | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | If the future is already done or cancelled, return False.  Otherwise, | 
					
						
							|  |  |  | change the future's state to cancelled, schedule the callbacks and | 
					
						
							|  |  |  | return True. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_cancel_impl(FutureObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                             PyObject *msg) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=074956f35904b034 input=44ab4003da839970]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							|  |  |  |     return future_cancel(state, self, msg); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.cancelled | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return True if the future was cancelled. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio_Future_cancelled_impl(FutureObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=145197ced586357d input=9b8644819a675416]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (future_is_alive(self) && self->fut_state == STATE_CANCELLED) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Future.done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return True if the future is done. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Done means either that a result / exception are available, or that the | 
					
						
							|  |  |  | future was cancelled. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio_Future_done_impl(FutureObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=244c5ac351145096 input=7204d3cc63bef7f3]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     if (!future_is_alive(self) || self->fut_state == STATE_PENDING) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | _asyncio.Future.get_loop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | Return the event loop the Future is bound to. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Future_get_loop_impl(FutureObj *self, PyTypeObject *cls) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=f50ea6c374d9ee97 input=f3ce629bfd9f45c1]*/ | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |     return Py_NewRef(self->fut_loop); | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._asyncio_awaited_by | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Future__asyncio_awaited_by_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=932af76d385d2e2a input=64c1783df2d44d2b]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Implementation of a Python getter. */ | 
					
						
							|  |  |  |     if (self->fut_awaited_by == NULL) { | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (self->fut_awaited_by_is_set) { | 
					
						
							|  |  |  |         /* Already a set, just wrap it into a frozen set and return. */ | 
					
						
							|  |  |  |         assert(PySet_CheckExact(self->fut_awaited_by)); | 
					
						
							|  |  |  |         return PyFrozenSet_New(self->fut_awaited_by); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *set = PyFrozenSet_New(NULL); | 
					
						
							|  |  |  |     if (set == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PySet_Add(set, self->fut_awaited_by)) { | 
					
						
							|  |  |  |         Py_DECREF(set); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return set; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._asyncio_future_blocking | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__asyncio_future_blocking_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=a558a2c51e38823b input=58da92efc03b617d]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (future_is_alive(self) && self->fut_blocking) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @setter | 
					
						
							|  |  |  | _asyncio.Future._asyncio_future_blocking | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__asyncio_future_blocking_set_impl(FutureObj *self, | 
					
						
							|  |  |  |                                                   PyObject *value) | 
					
						
							|  |  |  | /*[clinic end generated code: output=0686d1cb024a7453 input=3fd4a5f95df788b7]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (future_ensure_alive(self)) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (value == NULL) { | 
					
						
							| 
									
										
										
										
											2018-12-17 07:52:45 -07:00
										 |  |  |         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     int is_true = PyObject_IsTrue(value); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (is_true < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     self->fut_blocking = is_true; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._log_traceback | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__log_traceback_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=2724433b238593c7 input=91e5144ea4117d8e]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							|  |  |  |     if (self->fut_log_tb) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @setter | 
					
						
							|  |  |  | _asyncio.Future._log_traceback | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__log_traceback_set_impl(FutureObj *self, PyObject *value) | 
					
						
							|  |  |  | /*[clinic end generated code: output=9ce8e19504f42f54 input=30ac8217754b08c2]*/ | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (value == NULL) { | 
					
						
							| 
									
										
										
										
											2018-12-17 07:52:45 -07:00
										 |  |  |         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     int is_true = PyObject_IsTrue(value); | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  |     if (is_true < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-25 16:16:10 -05:00
										 |  |  |     if (is_true) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                         "_log_traceback can only be set to False"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     self->fut_log_tb = is_true; | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._loop | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__loop_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=5ba31563eecfeedf input=0337130bc5781670]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (!future_is_alive(self)) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     return Py_NewRef(self->fut_loop); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._callbacks | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__callbacks_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=b40d360505fcc583 input=7a466649530c01bb]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |     Py_ssize_t len = 0; | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->fut_callback0 != NULL) { | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |         len++; | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->fut_callbacks != NULL) { | 
					
						
							|  |  |  |         len += PyList_GET_SIZE(self->fut_callbacks); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |     if (len == 0) { | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |     PyObject *callbacks = PyList_New(len); | 
					
						
							|  |  |  |     if (callbacks == NULL) { | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |     Py_ssize_t i = 0; | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->fut_callback0 != NULL) { | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |         PyObject *tup0 = PyTuple_New(2); | 
					
						
							|  |  |  |         if (tup0 == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(callbacks); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |         PyTuple_SET_ITEM(tup0, 0, Py_NewRef(self->fut_callback0)); | 
					
						
							|  |  |  |         assert(self->fut_context0 != NULL); | 
					
						
							|  |  |  |         PyTuple_SET_ITEM(tup0, 1, Py_NewRef(self->fut_context0)); | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |         PyList_SET_ITEM(callbacks, i, tup0); | 
					
						
							|  |  |  |         i++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->fut_callbacks != NULL) { | 
					
						
							|  |  |  |         for (Py_ssize_t j = 0; j < PyList_GET_SIZE(self->fut_callbacks); j++) { | 
					
						
							|  |  |  |             PyObject *cb = PyList_GET_ITEM(self->fut_callbacks, j); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |             Py_INCREF(cb); | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |             PyList_SET_ITEM(callbacks, i, cb); | 
					
						
							|  |  |  |             i++; | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 18:19:30 +05:30
										 |  |  |     return callbacks; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._result | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__result_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=6877e8ce97333873 input=624f8e28e67f2636]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							|  |  |  |     if (self->fut_result == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     return Py_NewRef(self->fut_result); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._exception | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__exception_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=32f2c93b9e021a9b input=1828a1fcac929710]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							|  |  |  |     if (self->fut_exception == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     return Py_NewRef(self->fut_exception); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._source_traceback | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__source_traceback_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=d4f12b09af22f61b input=3c831fbde5da90d0]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (!future_is_alive(self) || self->fut_source_tb == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     return Py_NewRef(self->fut_source_tb); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._cancel_message | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__cancel_message_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=52ef6444f92cedac input=54c12c67082e4eea]*/ | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->fut_cancel_msg == NULL) { | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     return Py_NewRef(self->fut_cancel_msg); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @setter | 
					
						
							|  |  |  | _asyncio.Future._cancel_message | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__cancel_message_set_impl(FutureObj *self, PyObject *value) | 
					
						
							|  |  |  | /*[clinic end generated code: output=0854b2f77bff2209 input=f461d17f2d891fad]*/ | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (value == NULL) { | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     Py_INCREF(value); | 
					
						
							|  |  |  |     Py_XSETREF(self->fut_cancel_msg, value); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Future._state | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Future__state_get_impl(FutureObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=622f560a3fa69c63 input=7c5ad023a93423ff]*/ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     PyObject *ret = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     ENSURE_FUTURE_ALIVE(state, self) | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     switch (self->fut_state) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     case STATE_PENDING: | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         ret = &_Py_ID(PENDING); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case STATE_CANCELLED: | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         ret = &_Py_ID(CANCELLED); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case STATE_FINISHED: | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         ret = &_Py_ID(FINISHED); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         assert (0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-10 18:19:08 +01:00
										 |  |  |     assert(_Py_IsImmortal(ret)); | 
					
						
							| 
									
										
										
										
											2023-08-04 16:24:50 -07:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-17 03:03:09 +02:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureObj_repr(PyObject *op) | 
					
						
							| 
									
										
										
										
											2022-03-17 03:03:09 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     FutureObj *fut = (FutureObj*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, fut) | 
					
						
							|  |  |  |     return PyObject_CallOneArg(state->asyncio_future_repr_func, (PyObject *)fut); | 
					
						
							| 
									
										
										
										
											2022-03-17 03:03:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | _asyncio.Future._make_cancelled_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Create the CancelledError to raise if the Future is cancelled. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This should only be called once when handling a cancellation since | 
					
						
							|  |  |  | it erases the context exception value. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Future__make_cancelled_error_impl(FutureObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=a5df276f6c1213de input=ccb90df8c3c18bcd]*/ | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							|  |  |  |     return create_cancelled_error(state, self); | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureObj_finalize(PyObject *op) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     FutureObj *fut = (FutureObj*)op; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *context; | 
					
						
							|  |  |  |     PyObject *message = NULL; | 
					
						
							|  |  |  |     PyObject *func; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (!fut->fut_log_tb) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     assert(fut->fut_exception != NULL); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     fut->fut_log_tb = 0; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Save the current exception, if any. */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context = PyDict_New(); | 
					
						
							|  |  |  |     if (context == NULL) { | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     message = PyUnicode_FromFormat( | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         "%s exception was never retrieved", _PyType_Name(Py_TYPE(fut))); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (message == NULL) { | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     if (PyDict_SetItem(context, &_Py_ID(message), message) < 0 || | 
					
						
							|  |  |  |         PyDict_SetItem(context, &_Py_ID(exception), fut->fut_exception) < 0 || | 
					
						
							|  |  |  |         PyDict_SetItem(context, &_Py_ID(future), (PyObject*)fut) < 0) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (fut->fut_source_tb != NULL) { | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         if (PyDict_SetItem(context, &_Py_ID(source_traceback), | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |                               fut->fut_source_tb) < 0) { | 
					
						
							|  |  |  |             goto finally; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     func = PyObject_GetAttr(fut->fut_loop, &_Py_ID(call_exception_handler)); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (func != NULL) { | 
					
						
							| 
									
										
										
										
											2020-02-11 17:46:57 +01:00
										 |  |  |         PyObject *res = PyObject_CallOneArg(func, context); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2025-01-31 14:20:35 +01:00
										 |  |  |             PyErr_FormatUnraisable("Exception ignored while calling asyncio " | 
					
						
							|  |  |  |                                    "function %R", func); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         else { | 
					
						
							|  |  |  |             Py_DECREF(res); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(func); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finally: | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     Py_XDECREF(context); | 
					
						
							|  |  |  |     Py_XDECREF(message); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Restore the saved exception. */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef FutureType_methods[] = { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     _ASYNCIO_FUTURE_RESULT_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_EXCEPTION_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_SET_RESULT_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_SET_EXCEPTION_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_CANCEL_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_CANCELLED_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_DONE_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     _ASYNCIO_FUTURE_GET_LOOP_METHODDEF | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  |     _ASYNCIO_FUTURE__MAKE_CANCELLED_ERROR_METHODDEF | 
					
						
							| 
									
										
										
										
											2022-01-22 16:58:53 +05:30
										 |  |  |     {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     {NULL, NULL}        /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyGetSetDef FutureType_getsetlist[] = { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _ASYNCIO_FUTURE__STATE_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__ASYNCIO_FUTURE_BLOCKING_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__LOOP_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__CALLBACKS_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__RESULT_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__EXCEPTION_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__LOG_TRACEBACK_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__SOURCE_TRACEBACK_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE__CANCEL_MESSAGE_GETSETDEF | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     _ASYNCIO_FUTURE__ASYNCIO_AWAITED_BY_GETSETDEF | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     {NULL} /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void FutureObj_dealloc(PyObject *self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static PyType_Slot Future_slots[] = { | 
					
						
							|  |  |  |     {Py_tp_dealloc, FutureObj_dealloc}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_repr, FutureObj_repr}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_doc, (void *)_asyncio_Future___init____doc__}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_traverse, FutureObj_traverse}, | 
					
						
							|  |  |  |     {Py_tp_clear, FutureObj_clear}, | 
					
						
							|  |  |  |     {Py_tp_iter, future_new_iter}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_methods, FutureType_methods}, | 
					
						
							|  |  |  |     {Py_tp_getset, FutureType_getsetlist}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_init, _asyncio_Future___init__}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_new, PyType_GenericNew}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_finalize, FutureObj_finalize}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     // async slots
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_am_await, future_new_iter}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {0, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyType_Spec Future_spec = { | 
					
						
							|  |  |  |     .name = "_asyncio.Future", | 
					
						
							|  |  |  |     .basicsize = sizeof(FutureObj), | 
					
						
							|  |  |  |     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | | 
					
						
							| 
									
										
										
										
											2023-07-10 17:18:03 +05:30
										 |  |  |               Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_DICT | | 
					
						
							|  |  |  |               Py_TPFLAGS_MANAGED_WEAKREF), | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     .slots = Future_slots, | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | FutureObj_dealloc(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (PyObject_CallFinalizerFromDealloc(self) < 0) { | 
					
						
							|  |  |  |         // resurrected.
 | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							| 
									
										
										
										
											2017-08-01 23:31:07 -07:00
										 |  |  |     PyObject_GC_UnTrack(self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-10 17:18:03 +05:30
										 |  |  |     PyObject_ClearWeakRefs(self); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     (void)FutureObj_clear(self); | 
					
						
							|  |  |  |     tp->tp_free(self); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************** Future Iterator **************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 16:42:58 +05:30
										 |  |  | typedef struct futureiterobject { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     FutureObj *future; | 
					
						
							|  |  |  | } futureiterobject; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_dealloc(PyObject *it) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     PyTypeObject *tp = Py_TYPE(it); | 
					
						
							| 
									
										
										
										
											2024-04-19 15:30:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert(_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     PyObject_GC_UnTrack(it); | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     tp->tp_clear(it); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 12:24:24 -04:00
										 |  |  |     if (!_Py_FREELIST_PUSH(futureiters, it, Py_futureiters_MAXFREELIST)) { | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         PyObject_GC_Del(it); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  | static PySendResult | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | FutureIter_am_send_lock_held(futureiterobject *it, PyObject **result) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *res; | 
					
						
							|  |  |  |     FutureObj *fut = it->future; | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  |     *result = NULL; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (fut->fut_state == STATE_PENDING) { | 
					
						
							|  |  |  |         if (!fut->fut_blocking) { | 
					
						
							|  |  |  |             fut->fut_blocking = 1; | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |             *result = Py_NewRef(fut); | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  |             return PYGEN_NEXT; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  |         PyErr_SetString(PyExc_RuntimeError, | 
					
						
							|  |  |  |                         "await wasn't used with future"); | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  |         return PYGEN_ERROR; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     res = _asyncio_Future_result_impl(fut); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     if (res != NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  |         *result = res; | 
					
						
							|  |  |  |         return PYGEN_RETURN; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  |     return PYGEN_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | static PySendResult | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_am_send(PyObject *op, | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |                    PyObject *Py_UNUSED(arg), | 
					
						
							|  |  |  |                    PyObject **result) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     futureiterobject *it = (futureiterobject*)op; | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     /* arg is unused, see the comment on FutureIter_send for clarification */ | 
					
						
							|  |  |  |     PySendResult res; | 
					
						
							|  |  |  |     Py_BEGIN_CRITICAL_SECTION(it->future); | 
					
						
							|  |  |  |     res = FutureIter_am_send_lock_held(it, result); | 
					
						
							|  |  |  |     Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_iternext(PyObject *it) | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *result; | 
					
						
							|  |  |  |     switch (FutureIter_am_send(it, Py_None, &result)) { | 
					
						
							|  |  |  |         case PYGEN_RETURN: | 
					
						
							|  |  |  |             (void)_PyGen_SetStopIterationValue(result); | 
					
						
							|  |  |  |             Py_DECREF(result); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         case PYGEN_NEXT: | 
					
						
							|  |  |  |             return result; | 
					
						
							|  |  |  |         case PYGEN_ERROR: | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             Py_UNREACHABLE(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_send(PyObject *self, PyObject *unused) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-10-25 19:00:45 +09:00
										 |  |  |     /* Future.__iter__ doesn't care about values that are pushed to the
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |      * generator, it just returns self.result(). | 
					
						
							| 
									
										
										
										
											2016-10-25 19:00:45 +09:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     return FutureIter_iternext(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_throw(PyObject *op, PyObject *const *args, Py_ssize_t nargs) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     futureiterobject *self = (futureiterobject*)op; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *type, *val = NULL, *tb = NULL; | 
					
						
							| 
									
										
										
										
											2022-03-19 14:01:46 +02:00
										 |  |  |     if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) { | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2022-03-19 14:01:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-30 16:43:02 +08:00
										 |  |  |     if (nargs > 1) { | 
					
						
							|  |  |  |         if (PyErr_WarnEx(PyExc_DeprecationWarning, | 
					
						
							|  |  |  |                             "the (type, exc, tb) signature of throw() is deprecated, " | 
					
						
							|  |  |  |                             "use the single-arg signature instead.", | 
					
						
							|  |  |  |                             1) < 0) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 14:01:46 +02:00
										 |  |  |     type = args[0]; | 
					
						
							|  |  |  |     if (nargs == 3) { | 
					
						
							|  |  |  |         val = args[1]; | 
					
						
							|  |  |  |         tb = args[2]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (nargs == 2) { | 
					
						
							|  |  |  |         val = args[1]; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-19 14:01:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 12:01:01 -08:00
										 |  |  |     if (val == Py_None) { | 
					
						
							|  |  |  |         val = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (tb == Py_None ) { | 
					
						
							|  |  |  |         tb = NULL; | 
					
						
							|  |  |  |     } else if (tb != NULL && !PyTraceBack_Check(tb)) { | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  |         PyErr_SetString(PyExc_TypeError, "throw() third argument must be a traceback"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  |     Py_INCREF(type); | 
					
						
							|  |  |  |     Py_XINCREF(val); | 
					
						
							|  |  |  |     Py_XINCREF(tb); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  |     if (PyExceptionClass_Check(type)) { | 
					
						
							|  |  |  |         PyErr_NormalizeException(&type, &val, &tb); | 
					
						
							| 
									
										
										
										
											2016-12-01 11:37:47 -05:00
										 |  |  |         /* No need to call PyException_SetTraceback since we'll be calling
 | 
					
						
							|  |  |  |            PyErr_Restore for `type`, `val`, and `tb`. */ | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  |     } else if (PyExceptionInstance_Check(type)) { | 
					
						
							|  |  |  |         if (val) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                             "instance exception may not have a separate value"); | 
					
						
							|  |  |  |             goto fail; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  |         val = type; | 
					
						
							|  |  |  |         type = PyExceptionInstance_Class(type); | 
					
						
							|  |  |  |         Py_INCREF(type); | 
					
						
							|  |  |  |         if (tb == NULL) | 
					
						
							|  |  |  |             tb = PyException_GetTraceback(val); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                         "exceptions must be classes deriving BaseException or " | 
					
						
							|  |  |  |                         "instances of such a class"); | 
					
						
							|  |  |  |         goto fail; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_CLEAR(self->future); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyErr_Restore(type, val, tb); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2016-11-14 00:15:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   fail: | 
					
						
							|  |  |  |     Py_DECREF(type); | 
					
						
							|  |  |  |     Py_XDECREF(val); | 
					
						
							|  |  |  |     Py_XDECREF(tb); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_clear(PyObject *op) | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     futureiterobject *it = (futureiterobject*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_CLEAR(it->future); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_close(PyObject *self, PyObject *arg) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     (void)FutureIter_clear(self); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | FutureIter_traverse(PyObject *op, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     futureiterobject *it = (futureiterobject*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_VISIT(Py_TYPE(it)); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     Py_VISIT(it->future); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef FutureIter_methods[] = { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {"send",  FutureIter_send, METH_O, NULL}, | 
					
						
							| 
									
										
										
										
											2022-05-03 21:42:14 +02:00
										 |  |  |     {"throw", _PyCFunction_CAST(FutureIter_throw), METH_FASTCALL, NULL}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {"close", FutureIter_close, METH_NOARGS, NULL}, | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     {NULL, NULL}        /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static PyType_Slot FutureIter_slots[] = { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_dealloc, FutureIter_dealloc}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_getattro, PyObject_GenericGetAttr}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_traverse, FutureIter_traverse}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_clear, FutureIter_clear}, | 
					
						
							|  |  |  |     {Py_tp_iter, PyObject_SelfIter}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_iternext, FutureIter_iternext}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_methods, FutureIter_methods}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // async methods
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_am_send, FutureIter_am_send}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {0, NULL}, | 
					
						
							| 
									
										
										
										
											2020-11-10 12:09:55 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static PyType_Spec FutureIter_spec = { | 
					
						
							|  |  |  |     .name = "_asyncio.FutureIter", | 
					
						
							|  |  |  |     .basicsize = sizeof(futureiterobject), | 
					
						
							|  |  |  |     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | 
					
						
							|  |  |  |               Py_TPFLAGS_IMMUTABLETYPE), | 
					
						
							|  |  |  |     .slots = FutureIter_slots, | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | future_new_iter(PyObject *fut) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     futureiterobject *it; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); | 
					
						
							|  |  |  |     ENSURE_FUTURE_ALIVE(state, fut) | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 12:24:24 -04:00
										 |  |  |     it = _Py_FREELIST_POP(futureiterobject, futureiters); | 
					
						
							|  |  |  |     if (it == NULL) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         it = PyObject_GC_New(futureiterobject, state->FutureIterType); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |         if (it == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-16 10:39:47 +01:00
										 |  |  |     it->future = (FutureObj*)Py_NewRef(fut); | 
					
						
							| 
									
										
										
										
											2016-10-11 02:12:34 +09:00
										 |  |  |     PyObject_GC_Track(it); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  |     return (PyObject*)it; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*********************** Task **************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | class _asyncio.Task "TaskObj *" "&Task_Type" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=719dcef0fcc03b37]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static int task_call_step_soon(asyncio_state *state, TaskObj *, PyObject *); | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | static PyObject *task_wakeup(PyObject *op, PyObject *arg); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static PyObject * task_step(asyncio_state *, TaskObj *, PyObject *); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | static int task_eager_start(asyncio_state *state, TaskObj *task); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ----- Task._step wrapper */ | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskStepMethWrapper_clear(PyObject *op) | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskStepMethWrapper *o = (TaskStepMethWrapper*)op; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_CLEAR(o->sw_task); | 
					
						
							|  |  |  |     Py_CLEAR(o->sw_arg); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskStepMethWrapper_dealloc(PyObject *op) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskStepMethWrapper *o = (TaskStepMethWrapper*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     PyTypeObject *tp = Py_TYPE(o); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     PyObject_GC_UnTrack(o); | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     (void)TaskStepMethWrapper_clear(op); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_TYPE(o)->tp_free(o); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskStepMethWrapper_call(PyObject *op, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |                          PyObject *args, PyObject *kwds) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskStepMethWrapper *o = (TaskStepMethWrapper*)op; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "function takes no keyword arguments"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (args != NULL && PyTuple_GET_SIZE(args) != 0) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "function takes no positional arguments"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)o); | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     Py_BEGIN_CRITICAL_SECTION(o->sw_task); | 
					
						
							|  |  |  |     res = task_step(state, o->sw_task, o->sw_arg); | 
					
						
							|  |  |  |     Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskStepMethWrapper_traverse(PyObject *op, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |                              visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskStepMethWrapper *o = (TaskStepMethWrapper*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_VISIT(Py_TYPE(o)); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_VISIT(o->sw_task); | 
					
						
							|  |  |  |     Py_VISIT(o->sw_arg); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-11-27 19:34:35 +02:00
										 |  |  | TaskStepMethWrapper_get___self__(TaskStepMethWrapper *o, void *Py_UNUSED(ignored)) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     if (o->sw_task) { | 
					
						
							| 
									
										
										
										
											2022-11-16 10:39:47 +01:00
										 |  |  |         return Py_NewRef(o->sw_task); | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  | static PyGetSetDef TaskStepMethWrapper_getsetlist[] = { | 
					
						
							|  |  |  |     {"__self__", (getter)TaskStepMethWrapper_get___self__, NULL, NULL}, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     {NULL} /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static PyType_Slot TaskStepMethWrapper_slots[] = { | 
					
						
							|  |  |  |     {Py_tp_getset, TaskStepMethWrapper_getsetlist}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_dealloc, TaskStepMethWrapper_dealloc}, | 
					
						
							|  |  |  |     {Py_tp_call, TaskStepMethWrapper_call}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_getattro, PyObject_GenericGetAttr}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_traverse, TaskStepMethWrapper_traverse}, | 
					
						
							|  |  |  |     {Py_tp_clear, TaskStepMethWrapper_clear}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {0, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyType_Spec TaskStepMethWrapper_spec = { | 
					
						
							|  |  |  |     .name = "_asyncio.TaskStepMethWrapper", | 
					
						
							|  |  |  |     .basicsize = sizeof(TaskStepMethWrapper), | 
					
						
							|  |  |  |     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | 
					
						
							|  |  |  |               Py_TPFLAGS_IMMUTABLETYPE), | 
					
						
							|  |  |  |     .slots = TaskStepMethWrapper_slots, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  | TaskStepMethWrapper_new(TaskObj *task, PyObject *arg) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)task); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     TaskStepMethWrapper *o; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     o = PyObject_GC_New(TaskStepMethWrapper, state->TaskStepMethWrapper_Type); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (o == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |     o->sw_task = (TaskObj*)Py_NewRef(task); | 
					
						
							|  |  |  |     o->sw_arg = Py_XNewRef(arg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PyObject_GC_Track(o); | 
					
						
							|  |  |  |     return (PyObject*) o; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 17:49:10 -07:00
										 |  |  | /* ----- Task._wakeup implementation */ | 
					
						
							| 
									
										
										
										
											2019-12-07 13:23:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 17:49:10 -07:00
										 |  |  | static  PyMethodDef TaskWakeupDef = { | 
					
						
							|  |  |  |     "task_wakeup", | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     task_wakeup, | 
					
						
							| 
									
										
										
										
											2020-10-21 17:49:10 -07:00
										 |  |  |     METH_O, | 
					
						
							|  |  |  |     NULL | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | /* ----- Task introspection helpers */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  | register_task(TaskObj *task) | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  |     if (task->task_node.next != NULL) { | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |         // already registered
 | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  |         assert(task->task_node.prev != NULL); | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  |     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *) _PyThreadState_GET(); | 
					
						
							|  |  |  |     struct llist_node *head = &tstate->asyncio_tasks_head; | 
					
						
							|  |  |  |     llist_insert_tail(head, &task->task_node); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  | static inline void | 
					
						
							|  |  |  | unregister_task_safe(TaskObj *task) | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  |     if (task->task_node.next == NULL) { | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |         // not registered
 | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  |         assert(task->task_node.prev == NULL); | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-25 17:51:27 +05:30
										 |  |  |     llist_remove(&task->task_node); | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  | unregister_task(TaskObj *task) | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  | { | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     // check if we are in the same thread
 | 
					
						
							|  |  |  |     // if so, we can avoid locking
 | 
					
						
							|  |  |  |     if (task->task_tid == _Py_ThreadId()) { | 
					
						
							|  |  |  |         unregister_task_safe(task); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         // we are in a different thread
 | 
					
						
							|  |  |  |         // stop the world then check and remove the task
 | 
					
						
							|  |  |  |         PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |         _PyEval_StopTheWorld(tstate->interp); | 
					
						
							|  |  |  |         unregister_task_safe(task); | 
					
						
							|  |  |  |         _PyEval_StartTheWorld(tstate->interp); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     unregister_task_safe(task); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  | enter_task(PyObject *loop, PyObject *task) | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ts->asyncio_running_loop != loop) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_RuntimeError, "loop %R is not the running loop", loop); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ts->asyncio_running_task != NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         PyErr_Format( | 
					
						
							|  |  |  |             PyExc_RuntimeError, | 
					
						
							|  |  |  |             "Cannot enter into task %R while another " \ | 
					
						
							|  |  |  |             "task %R is being executed.", | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |             task, ts->asyncio_running_task, NULL); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     ts->asyncio_running_task = Py_NewRef(task); | 
					
						
							| 
									
										
										
										
											2024-07-23 04:47:14 -04:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 13:06:03 -04:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  | leave_task(PyObject *loop, PyObject *task) | 
					
						
							| 
									
										
										
										
											2024-07-23 13:06:03 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (ts->asyncio_running_loop != loop) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_RuntimeError, "loop %R is not the running loop", loop); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (ts->asyncio_running_task != task) { | 
					
						
							|  |  |  |         PyErr_Format( | 
					
						
							|  |  |  |             PyExc_RuntimeError, | 
					
						
							|  |  |  |             "Invalid attempt to leave task %R while " \ | 
					
						
							|  |  |  |             "task %R is entered.", | 
					
						
							|  |  |  |             task, ts->asyncio_running_task ? ts->asyncio_running_task : Py_None, NULL); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2024-08-02 09:32:08 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     Py_CLEAR(ts->asyncio_running_task); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2024-08-02 09:32:08 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  | swap_current_task(PyObject *loop, PyObject *task) | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2024-03-07 11:21:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (ts->asyncio_running_loop != loop) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_RuntimeError, "loop %R is not the running loop", loop); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     /* transfer ownership to avoid redundant ref counting */ | 
					
						
							|  |  |  |     PyObject *prev_task = ts->asyncio_running_task; | 
					
						
							|  |  |  |     if (task != Py_None) { | 
					
						
							| 
									
										
										
										
											2025-01-23 19:26:36 +01:00
										 |  |  |         ts->asyncio_running_task = Py_NewRef(task); | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     } else { | 
					
						
							|  |  |  |         ts->asyncio_running_task = NULL; | 
					
						
							| 
									
										
										
										
											2025-01-23 19:26:36 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (prev_task == NULL) { | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2025-01-23 19:26:36 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     return prev_task; | 
					
						
							| 
									
										
										
										
											2025-01-23 19:26:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /* ----- Task */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Task.__init__ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     coro: object | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     * | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     loop: object = None | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     name: object = None | 
					
						
							| 
									
										
										
										
											2022-03-14 13:54:13 +02:00
										 |  |  |     context: object = None | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     eager_start: bool = False | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | A coroutine wrapped in a Future. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  | _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |                             PyObject *name, PyObject *context, | 
					
						
							|  |  |  |                             int eager_start) | 
					
						
							|  |  |  | /*[clinic end generated code: output=7aced2d27836f1a1 input=18e3f113a51b829d]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     if (future_init((FutureObj*)self, loop)) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     self->task_is_task = 1; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | 
					
						
							|  |  |  |     int is_coro = is_coroutine(state, coro); | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |     if (is_coro == -1) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (is_coro == 0) { | 
					
						
							|  |  |  |         self->task_log_destroy_pending = 0; | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "a coroutine was expected, got %R", | 
					
						
							|  |  |  |                      coro, NULL); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2017-12-15 07:04:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:54:13 +02:00
										 |  |  |     if (context == Py_None) { | 
					
						
							|  |  |  |         Py_XSETREF(self->task_context, PyContext_CopyCurrent()); | 
					
						
							|  |  |  |         if (self->task_context == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2024-10-31 03:47:57 -04:00
										 |  |  |         Py_XSETREF(self->task_context, Py_NewRef(context)); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 12:28:33 +02:00
										 |  |  |     Py_CLEAR(self->task_fut_waiter); | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     self->task_tid = _Py_ThreadId(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     self->task_must_cancel = 0; | 
					
						
							|  |  |  |     self->task_log_destroy_pending = 1; | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  |     self->task_num_cancels_requested = 0; | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     set_task_coro(self, coro); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     if (name == Py_None) { | 
					
						
							| 
									
										
										
										
											2023-04-29 08:20:09 -07:00
										 |  |  |         // optimization: defer task name formatting
 | 
					
						
							|  |  |  |         // store the task counter as PyLong in the name
 | 
					
						
							|  |  |  |         // for deferred formatting in get_name
 | 
					
						
							| 
									
										
										
										
											2024-07-14 20:22:56 +08:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |         unsigned long long counter = _Py_atomic_add_uint64(&state->task_name_counter, 1) + 1; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |         unsigned long long counter = ++state->task_name_counter; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         name = PyLong_FromUnsignedLongLong(counter); | 
					
						
							| 
									
										
										
										
											2018-08-09 23:49:49 +03:00
										 |  |  |     } else if (!PyUnicode_CheckExact(name)) { | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |         name = PyObject_Str(name); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Py_INCREF(name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_XSETREF(self->task_name, name); | 
					
						
							|  |  |  |     if (self->task_name == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     if (eager_start) { | 
					
						
							| 
									
										
										
										
											2023-05-06 21:25:45 -07:00
										 |  |  |         PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running)); | 
					
						
							|  |  |  |         if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-06 21:25:45 -07:00
										 |  |  |         int is_loop_running = Py_IsTrue(res); | 
					
						
							|  |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |         if (is_loop_running) { | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |             if (task_eager_start(state, self)) { | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (task_call_step_soon(state, self, NULL)) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-24 21:12:56 +05:30
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     // This is required so that _Py_TryIncref(self)
 | 
					
						
							|  |  |  |     // works correctly in non-owning threads.
 | 
					
						
							|  |  |  |     _PyObject_SetMaybeWeakref((PyObject *)self); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  |     register_task(self); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskObj_clear(PyObject *op) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskObj *task = (TaskObj*)op; | 
					
						
							|  |  |  |     (void)FutureObj_clear(op); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     clear_task_coro(task); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     Py_CLEAR(task->task_context); | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     Py_CLEAR(task->task_name); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_CLEAR(task->task_fut_waiter); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskObj_traverse(PyObject *op, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskObj *task = (TaskObj*)op; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_VISIT(Py_TYPE(task)); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |     Py_VISIT(task->task_context); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_VISIT(task->task_coro); | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     Py_VISIT(task->task_name); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_VISIT(task->task_fut_waiter); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     FutureObj *fut = (FutureObj *)task; | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_loop); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_callback0); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_context0); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_callbacks); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_result); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_exception); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_exception_tb); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_source_tb); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_cancel_msg); | 
					
						
							|  |  |  |     Py_VISIT(fut->fut_cancelled_exc); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     Py_VISIT(fut->fut_awaited_by); | 
					
						
							| 
									
										
										
										
											2023-10-02 19:24:08 +02:00
										 |  |  |     PyObject_VisitManagedDict((PyObject *)fut, visit, arg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Task._log_destroy_pending | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Task__log_destroy_pending_get_impl(TaskObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=e6c2a47d029ac93b input=17127298cd4c720b]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->task_log_destroy_pending) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         Py_RETURN_TRUE; | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     else { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @setter | 
					
						
							|  |  |  | _asyncio.Task._log_destroy_pending | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Task__log_destroy_pending_set_impl(TaskObj *self, PyObject *value) | 
					
						
							|  |  |  | /*[clinic end generated code: output=7ebc030bb92ec5ce input=49b759c97d1216a4]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (value == NULL) { | 
					
						
							| 
									
										
										
										
											2018-12-17 07:52:45 -07:00
										 |  |  |         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     int is_true = PyObject_IsTrue(value); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (is_true < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     self->task_log_destroy_pending = is_true; | 
					
						
							| 
									
										
										
										
											2016-10-18 11:48:14 +09:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Task._must_cancel | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Task__must_cancel_get_impl(TaskObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=70e79b900996c363 input=2d04529fb23feedf]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->task_must_cancel) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Task._coro | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Task__coro_get_impl(TaskObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=a2726012ab5fd531 input=323c31a272020624]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->task_coro) { | 
					
						
							|  |  |  |         return Py_NewRef(self->task_coro); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @critical_section | 
					
						
							|  |  |  | @getter | 
					
						
							|  |  |  | _asyncio.Task._fut_waiter | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Task__fut_waiter_get_impl(TaskObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=c4f966b847fefcdf input=4d1005d725e72db7]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     if (self->task_fut_waiter) { | 
					
						
							|  |  |  |         return Py_NewRef(self->task_fut_waiter); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-17 03:03:09 +02:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskObj_repr(PyObject *task) | 
					
						
							| 
									
										
										
										
											2022-03-17 03:03:09 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def(task); | 
					
						
							|  |  |  |     return PyObject_CallOneArg(state->asyncio_task_repr_func, task); | 
					
						
							| 
									
										
										
										
											2022-03-17 03:03:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | _asyncio.Task._make_cancelled_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Create the CancelledError to raise if the Task is cancelled. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This should only be called once when handling a cancellation since | 
					
						
							|  |  |  | it erases the context exception value. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Task__make_cancelled_error_impl(TaskObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=55a819e8b4276fab input=2d3213be0cb02390]*/ | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     FutureObj *fut = (FutureObj*)self; | 
					
						
							|  |  |  |     return _asyncio_Future__make_cancelled_error_impl(fut); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | _asyncio.Task.cancel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     msg: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | Request that this task cancel itself. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This arranges for a CancelledError to be thrown into the | 
					
						
							|  |  |  | wrapped coroutine on the next cycle through the event loop. | 
					
						
							|  |  |  | The coroutine then has a chance to clean up or even deny | 
					
						
							|  |  |  | the request using try/except/finally. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Unlike Future.cancel, this does not guarantee that the | 
					
						
							|  |  |  | task will be cancelled: the exception might be caught and | 
					
						
							|  |  |  | acted upon, delaying cancellation of the task or preventing | 
					
						
							|  |  |  | cancellation completely.  The task may also return a value or | 
					
						
							|  |  |  | raise a different exception. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Immediately after this method is called, Task.cancelled() will | 
					
						
							|  |  |  | not return True (unless the task was already cancelled).  A | 
					
						
							|  |  |  | task will be marked as cancelled when the wrapped coroutine | 
					
						
							|  |  |  | terminates with a CancelledError exception (even if cancel() | 
					
						
							|  |  |  | was not called). | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | This also increases the task's count of cancellation requests. | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=c66b60d41c74f9f1 input=6125d45b9a6a5abd]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-06-11 13:49:18 +00:00
										 |  |  |     self->task_log_tb = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (self->task_state != STATE_PENDING) { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  |     self->task_num_cancels_requested += 1; | 
					
						
							| 
									
										
										
										
											2022-02-28 15:15:56 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // These three lines are controversial.  See discussion starting at
 | 
					
						
							|  |  |  |     // https://github.com/python/cpython/pull/31394#issuecomment-1053545331
 | 
					
						
							|  |  |  |     // and corresponding code in tasks.py.
 | 
					
						
							|  |  |  |     // if (self->task_num_cancels_requested > 1) {
 | 
					
						
							|  |  |  |     //     Py_RETURN_FALSE;
 | 
					
						
							|  |  |  |     // }
 | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (self->task_fut_waiter) { | 
					
						
							|  |  |  |         PyObject *res; | 
					
						
							|  |  |  |         int is_true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         res = PyObject_CallMethodOneArg(self->task_fut_waiter, | 
					
						
							|  |  |  |                                            &_Py_ID(cancel), msg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         if (res == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         is_true = PyObject_IsTrue(res); | 
					
						
							|  |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |         if (is_true < 0) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (is_true) { | 
					
						
							|  |  |  |             Py_RETURN_TRUE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     self->task_must_cancel = 1; | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  |     Py_XINCREF(msg); | 
					
						
							|  |  |  |     Py_XSETREF(self->task_cancel_msg, msg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_RETURN_TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | _asyncio.Task.cancelling | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  | Return the count of the task's cancellation requests. | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  | This count is incremented when .cancel() is called | 
					
						
							|  |  |  | and may be decremented using .uncancel(). | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Task_cancelling_impl(TaskObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=803b3af96f917d7e input=5ef89b1b38f080ee]*/ | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | /*[clinic end generated code]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  |     return PyLong_FromLong(self->task_num_cancels_requested); | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | _asyncio.Task.uncancel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  | Decrement the task's count of cancellation requests. | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | This should be used by tasks that catch CancelledError | 
					
						
							|  |  |  | and wish to continue indefinitely until they are cancelled again. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  | Returns the remaining number of cancellation requests. | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Task_uncancel_impl(TaskObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=58184d236a817d3c input=cb3220b0e5afd61d]*/ | 
					
						
							| 
									
										
										
										
											2022-08-04 15:51:38 +02:00
										 |  |  | /*[clinic end generated code]*/ | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  |     if (self->task_num_cancels_requested > 0) { | 
					
						
							|  |  |  |         self->task_num_cancels_requested -= 1; | 
					
						
							| 
									
										
										
										
											2024-04-09 08:17:28 -07:00
										 |  |  |         if (self->task_num_cancels_requested == 0) { | 
					
						
							|  |  |  |             self->task_must_cancel = 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-24 03:17:00 +01:00
										 |  |  |     return PyLong_FromLong(self->task_num_cancels_requested); | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Task.get_stack | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							|  |  |  |     / | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     * | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     limit: object = None | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Return the list of stack frames for this task's coroutine. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the coroutine is not done, this returns the stack where it is | 
					
						
							|  |  |  | suspended.  If the coroutine has completed successfully or was | 
					
						
							|  |  |  | cancelled, this returns an empty list.  If the coroutine was | 
					
						
							|  |  |  | terminated by an exception, this returns the list of traceback | 
					
						
							|  |  |  | frames. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The frames are always ordered from oldest to newest. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The optional limit gives the maximum number of frames to | 
					
						
							|  |  |  | return; by default all available frames are returned.  Its | 
					
						
							|  |  |  | meaning differs depending on whether a stack or a traceback is | 
					
						
							|  |  |  | returned: the newest frames of a stack are returned, but the | 
					
						
							|  |  |  | oldest frames of a traceback are returned.  (This matches the | 
					
						
							|  |  |  | behavior of the traceback module.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For reasons beyond our control, only one stack frame is | 
					
						
							|  |  |  | returned for a suspended coroutine. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Task_get_stack_impl(TaskObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                              PyObject *limit) | 
					
						
							|  |  |  | /*[clinic end generated code: output=6774dfc10d3857fa input=8e01c9b2618ae953]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							| 
									
										
										
										
											2023-04-03 18:14:32 +05:30
										 |  |  |     PyObject *stack[] = {(PyObject *)self, limit}; | 
					
						
							|  |  |  |     return PyObject_Vectorcall(state->asyncio_task_get_stack_func, | 
					
						
							|  |  |  |                                stack, 2, NULL); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Task.print_stack | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     cls: defining_class | 
					
						
							|  |  |  |     / | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     * | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     limit: object = None | 
					
						
							|  |  |  |     file: object = None | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Print the stack or traceback for this task's coroutine. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This produces output similar to that of the traceback module, | 
					
						
							|  |  |  | for the frames retrieved by get_stack().  The limit argument | 
					
						
							|  |  |  | is passed to get_stack().  The file argument is an I/O stream | 
					
						
							|  |  |  | to which the output is written; by default output is written | 
					
						
							|  |  |  | to sys.stderr. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | _asyncio_Task_print_stack_impl(TaskObj *self, PyTypeObject *cls, | 
					
						
							|  |  |  |                                PyObject *limit, PyObject *file) | 
					
						
							|  |  |  | /*[clinic end generated code: output=b38affe9289ec826 input=150b35ba2d3a7dee]*/ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_cls(cls); | 
					
						
							| 
									
										
										
										
											2023-04-03 18:14:32 +05:30
										 |  |  |     PyObject *stack[] = {(PyObject *)self, limit, file}; | 
					
						
							|  |  |  |     return PyObject_Vectorcall(state->asyncio_task_print_stack_func, | 
					
						
							|  |  |  |                                stack, 3, NULL); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Task.set_result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result: object | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-11 16:33:36 +01:00
										 |  |  | _asyncio_Task_set_result_impl(TaskObj *self, PyObject *result) | 
					
						
							|  |  |  | /*[clinic end generated code: output=e9d8e3cdaf18e258 input=9d1a00c07be41bab]*/ | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     PyErr_SetString(PyExc_RuntimeError, | 
					
						
							|  |  |  |                     "Task does not support set_result operation"); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Task.set_exception | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     exception: object | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-11 16:33:36 +01:00
										 |  |  | _asyncio_Task_set_exception_impl(TaskObj *self, PyObject *exception) | 
					
						
							|  |  |  | /*[clinic end generated code: output=96a91790c192cc7d input=9a8f65c83dcf893a]*/ | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     PyErr_SetString(PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											2017-12-30 15:40:27 +02:00
										 |  |  |                     "Task does not support set_exception operation"); | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 18:30:09 +03:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2019-05-30 18:30:09 +03:00
										 |  |  | _asyncio.Task.get_coro | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Task_get_coro_impl(TaskObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=bcac27c8cc6c8073 input=a47f81427e39fe0c]*/ | 
					
						
							| 
									
										
										
										
											2019-05-30 18:30:09 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-27 02:27:48 -04:00
										 |  |  |     if (self->task_coro) { | 
					
						
							|  |  |  |         return Py_NewRef(self->task_coro); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2019-05-30 18:30:09 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 23:49:10 -07:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.Task.get_context | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Task_get_context_impl(TaskObj *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |     return Py_NewRef(self->task_context); | 
					
						
							| 
									
										
										
										
											2022-10-04 23:49:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  | _asyncio.Task.get_name | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_Task_get_name_impl(TaskObj *self) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | /*[clinic end generated code: output=0ecf1570c3b37a8f input=92a8f30c85034249]*/ | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     if (self->task_name) { | 
					
						
							| 
									
										
										
										
											2023-04-29 08:20:09 -07:00
										 |  |  |         if (PyLong_CheckExact(self->task_name)) { | 
					
						
							|  |  |  |             PyObject *name = PyUnicode_FromFormat("Task-%S", self->task_name); | 
					
						
							|  |  |  |             if (name == NULL) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Py_SETREF(self->task_name, name); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-14 00:31:21 +01:00
										 |  |  |         return Py_NewRef(self->task_name); | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  | _asyncio.Task.set_name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     value: object | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | _asyncio_Task_set_name_impl(TaskObj *self, PyObject *value) | 
					
						
							|  |  |  | /*[clinic end generated code: output=f88ff4c0d64a9a6f input=e8d400ad64bad799]*/ | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-09 23:49:49 +03:00
										 |  |  |     if (!PyUnicode_CheckExact(value)) { | 
					
						
							|  |  |  |         value = PyObject_Str(value); | 
					
						
							|  |  |  |         if (value == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Py_INCREF(value); | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 23:49:49 +03:00
										 |  |  |     Py_XSETREF(self->task_name, value); | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | TaskObj_finalize(PyObject *op) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskObj *task = (TaskObj*)op; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *context; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     PyObject *message = NULL; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *func; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) { | 
					
						
							|  |  |  |         goto done; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Save the current exception, if any. */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context = PyDict_New(); | 
					
						
							|  |  |  |     if (context == NULL) { | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     message = PyUnicode_FromString("Task was destroyed but it is pending!"); | 
					
						
							|  |  |  |     if (message == NULL) { | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     if (PyDict_SetItem(context, &_Py_ID(message), message) < 0 || | 
					
						
							|  |  |  |         PyDict_SetItem(context, &_Py_ID(task), (PyObject*)task) < 0) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (task->task_source_tb != NULL) { | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         if (PyDict_SetItem(context, &_Py_ID(source_traceback), | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |                               task->task_source_tb) < 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             goto finally; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |     func = PyObject_GetAttr(task->task_loop, &_Py_ID(call_exception_handler)); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (func != NULL) { | 
					
						
							| 
									
										
										
										
											2020-02-11 17:46:57 +01:00
										 |  |  |         PyObject *res = PyObject_CallOneArg(func, context); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2025-01-31 14:20:35 +01:00
										 |  |  |             PyErr_FormatUnraisable("Exception ignored while calling asyncio " | 
					
						
							|  |  |  |                                    "function %R", func); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         else { | 
					
						
							|  |  |  |             Py_DECREF(res); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(func); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finally: | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     Py_XDECREF(context); | 
					
						
							|  |  |  |     Py_XDECREF(message); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Restore the saved exception. */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | done: | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     FutureObj_finalize((PyObject*)task); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void TaskObj_dealloc(PyObject *);  /* Needs Task_CheckExact */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef TaskType_methods[] = { | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_RESULT_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_EXCEPTION_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_CANCELLED_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_DONE_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-25 10:48:15 -05:00
										 |  |  |     _ASYNCIO_TASK_SET_RESULT_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK_SET_EXCEPTION_METHODDEF | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     _ASYNCIO_TASK_CANCEL_METHODDEF | 
					
						
							| 
									
										
										
										
											2022-02-15 15:42:04 -08:00
										 |  |  |     _ASYNCIO_TASK_CANCELLING_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK_UNCANCEL_METHODDEF | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     _ASYNCIO_TASK_GET_STACK_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK_PRINT_STACK_METHODDEF | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  |     _ASYNCIO_TASK__MAKE_CANCELLED_ERROR_METHODDEF | 
					
						
							| 
									
										
										
										
											2018-08-09 00:06:47 +03:00
										 |  |  |     _ASYNCIO_TASK_GET_NAME_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK_SET_NAME_METHODDEF | 
					
						
							| 
									
										
										
										
											2019-05-30 18:30:09 +03:00
										 |  |  |     _ASYNCIO_TASK_GET_CORO_METHODDEF | 
					
						
							| 
									
										
										
										
											2022-10-04 23:49:10 -07:00
										 |  |  |     _ASYNCIO_TASK_GET_CONTEXT_METHODDEF | 
					
						
							| 
									
										
										
										
											2022-01-22 16:58:53 +05:30
										 |  |  |     {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     {NULL, NULL}        /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyGetSetDef TaskType_getsetlist[] = { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _ASYNCIO_TASK__LOG_DESTROY_PENDING_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK__MUST_CANCEL_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK__CORO_GETSETDEF | 
					
						
							|  |  |  |     _ASYNCIO_TASK__FUT_WAITER_GETSETDEF | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     {NULL} /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static PyType_Slot Task_slots[] = { | 
					
						
							|  |  |  |     {Py_tp_dealloc, TaskObj_dealloc}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_repr, TaskObj_repr}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_doc, (void *)_asyncio_Task___init____doc__}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_traverse, TaskObj_traverse}, | 
					
						
							|  |  |  |     {Py_tp_clear, TaskObj_clear}, | 
					
						
							|  |  |  |     {Py_tp_iter, future_new_iter}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {Py_tp_methods, TaskType_methods}, | 
					
						
							|  |  |  |     {Py_tp_getset, TaskType_getsetlist}, | 
					
						
							|  |  |  |     {Py_tp_init, (initproc)_asyncio_Task___init__}, | 
					
						
							|  |  |  |     {Py_tp_new, PyType_GenericNew}, | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_tp_finalize, TaskObj_finalize}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     // async slots
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     {Py_am_await, future_new_iter}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {0, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyType_Spec Task_spec = { | 
					
						
							|  |  |  |     .name = "_asyncio.Task", | 
					
						
							|  |  |  |     .basicsize = sizeof(TaskObj), | 
					
						
							|  |  |  |     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | | 
					
						
							| 
									
										
										
										
											2023-07-10 17:18:03 +05:30
										 |  |  |               Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_DICT | | 
					
						
							|  |  |  |               Py_TPFLAGS_MANAGED_WEAKREF), | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     .slots = Task_slots, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | TaskObj_dealloc(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:03:59 +05:30
										 |  |  |     _PyObject_ResurrectStart(self); | 
					
						
							|  |  |  |     // Unregister the task here so that even if any subclass of Task
 | 
					
						
							|  |  |  |     // which doesn't end up calling TaskObj_finalize not crashes.
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     unregister_task((TaskObj *)self); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:03:59 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     PyObject_CallFinalizer(self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyObject_ResurrectEnd(self)) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							| 
									
										
										
										
											2017-08-01 23:31:07 -07:00
										 |  |  |     PyObject_GC_UnTrack(self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-10 17:18:03 +05:30
										 |  |  |     PyObject_ClearWeakRefs(self); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     (void)TaskObj_clear(self); | 
					
						
							|  |  |  |     tp->tp_free(self); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | task_call_step_soon(asyncio_state *state, TaskObj *task, PyObject *arg) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *cb = TaskStepMethWrapper_new(task, arg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (cb == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-31 18:14:47 +01:00
										 |  |  |     // Beware: An evil call_soon could alter task_context.
 | 
					
						
							|  |  |  |     // See: https://github.com/python/cpython/issues/126080.
 | 
					
						
							|  |  |  |     PyObject *task_context = Py_NewRef(task->task_context); | 
					
						
							|  |  |  |     int ret = call_soon(state, task->task_loop, cb, NULL, task_context); | 
					
						
							|  |  |  |     Py_DECREF(task_context); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_DECREF(cb); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | task_set_error_soon(asyncio_state *state, TaskObj *task, PyObject *et, | 
					
						
							|  |  |  |                     const char *format, ...) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject* msg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     va_list vargs; | 
					
						
							|  |  |  |     va_start(vargs, format); | 
					
						
							|  |  |  |     msg = PyUnicode_FromFormatV(format, vargs); | 
					
						
							|  |  |  |     va_end(vargs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (msg == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 17:46:57 +01:00
										 |  |  |     PyObject *e = PyObject_CallOneArg(et, msg); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     Py_DECREF(msg); | 
					
						
							|  |  |  |     if (e == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (task_call_step_soon(state, task, e) == -1) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         Py_DECREF(e); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_DECREF(e); | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 18:38:38 -07:00
										 |  |  | static inline int | 
					
						
							|  |  |  | gen_status_from_result(PyObject **result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (*result != NULL) { | 
					
						
							|  |  |  |         return PYGEN_NEXT; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (_PyGen_FetchStopIterationValue(result) == 0) { | 
					
						
							|  |  |  |         return PYGEN_RETURN; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(PyErr_Occurred()); | 
					
						
							|  |  |  |     return PYGEN_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(task); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     int clear_exc = 0; | 
					
						
							|  |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *coro; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     PyObject *o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (task->task_state != STATE_PENDING) { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyErr_Format(state->asyncio_InvalidStateError, | 
					
						
							| 
									
										
										
										
											2024-07-15 11:29:19 +05:30
										 |  |  |                      "__step(): already done: %R %R", | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |                      task, | 
					
						
							|  |  |  |                      exc ? exc : Py_None); | 
					
						
							|  |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (task->task_must_cancel) { | 
					
						
							|  |  |  |         assert(exc != Py_None); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-09 21:41:02 +02:00
										 |  |  |         if (!exc || !PyErr_GivenExceptionMatches(exc, state->asyncio_CancelledError)) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             /* exc was not a CancelledError */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             exc = create_cancelled_error(state, (FutureObj*)task); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             if (!exc) { | 
					
						
							|  |  |  |                 goto fail; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             clear_exc = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         task->task_must_cancel = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_CLEAR(task->task_fut_waiter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     coro = task->task_coro; | 
					
						
							|  |  |  |     if (coro == NULL) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_RuntimeError, "uninitialized Task object"); | 
					
						
							| 
									
										
										
										
											2020-05-08 03:54:38 -07:00
										 |  |  |         if (clear_exc) { | 
					
						
							|  |  |  |             /* We created 'exc' during this call */ | 
					
						
							|  |  |  |             Py_DECREF(exc); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 18:38:38 -07:00
										 |  |  |     int gen_status = PYGEN_ERROR; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (exc == NULL) { | 
					
						
							| 
									
										
										
										
											2020-10-09 17:15:15 -07:00
										 |  |  |         gen_status = PyIter_Send(coro, Py_None, &result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         result = PyObject_CallMethodOneArg(coro, &_Py_ID(throw), exc); | 
					
						
							| 
									
										
										
										
											2020-09-18 18:38:38 -07:00
										 |  |  |         gen_status = gen_status_from_result(&result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         if (clear_exc) { | 
					
						
							|  |  |  |             /* We created 'exc' during this call */ | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |             Py_DECREF(exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 18:38:38 -07:00
										 |  |  |     if (gen_status == PYGEN_RETURN || gen_status == PYGEN_ERROR) { | 
					
						
							|  |  |  |         if (result != NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             /* The error is StopIteration and that means that
 | 
					
						
							|  |  |  |                the underlying coroutine has resolved */ | 
					
						
							| 
									
										
										
										
											2019-09-25 03:32:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |             PyObject *tmp; | 
					
						
							| 
									
										
										
										
											2017-05-11 21:18:38 +09:00
										 |  |  |             if (task->task_must_cancel) { | 
					
						
							|  |  |  |                 // Task is cancelled right before coro stops.
 | 
					
						
							|  |  |  |                 task->task_must_cancel = 0; | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |                 tmp = future_cancel(state, (FutureObj*)task, | 
					
						
							|  |  |  |                                     task->task_cancel_msg); | 
					
						
							| 
									
										
										
										
											2019-09-25 03:32:08 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |                 tmp = future_set_result(state, (FutureObj*)task, result); | 
					
						
							| 
									
										
										
										
											2017-05-11 21:18:38 +09:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-25 03:32:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 18:38:38 -07:00
										 |  |  |             Py_DECREF(result); | 
					
						
							| 
									
										
										
										
											2019-09-25 03:32:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |             if (tmp == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |             Py_DECREF(tmp); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             Py_RETURN_NONE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             /* CancelledError */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							|  |  |  |             assert(exc); | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-17 22:47:31 -07:00
										 |  |  |             FutureObj *fut = (FutureObj*)task; | 
					
						
							| 
									
										
										
										
											2022-02-21 22:59:04 +02:00
										 |  |  |             /* transfer ownership */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |             fut->fut_cancelled_exc = exc; | 
					
						
							| 
									
										
										
										
											2020-05-15 16:55:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             return future_cancel(state, fut, NULL); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Some other exception; pop it and call Task.set_exception() */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |         PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							|  |  |  |         assert(exc); | 
					
						
							| 
									
										
										
										
											2021-12-03 19:05:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |         o = future_set_exception(state, (FutureObj*)task, exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         if (!o) { | 
					
						
							|  |  |  |             /* An exception in Task.set_exception() */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |             Py_DECREF(exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         assert(o == Py_None); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         Py_DECREF(o); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |         if (PyErr_GivenExceptionMatches(exc, PyExc_KeyboardInterrupt) || | 
					
						
							|  |  |  |             PyErr_GivenExceptionMatches(exc, PyExc_SystemExit)) | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |             /* We've got a KeyboardInterrupt or a SystemError; re-raise it */ | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |             PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |         Py_DECREF(exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     PyObject *ret = task_step_handle_result_impl(state, task, result); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *result) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(task); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     int res; | 
					
						
							|  |  |  |     PyObject *o; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     if (result == (PyObject*)task) { | 
					
						
							|  |  |  |         /* We have a task that wants to await on itself */ | 
					
						
							|  |  |  |         goto self_await; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check if `result` is FutureObj or TaskObj (and not a subclass) */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (Future_CheckExact(state, result) || Task_CheckExact(state, result)) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         PyObject *wrapper; | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |         PyObject *tmp; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         FutureObj *fut = (FutureObj*)result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Check if `result` future is attached to a different loop */ | 
					
						
							| 
									
										
										
										
											2022-08-04 15:51:38 +02:00
										 |  |  |         if (fut->fut_loop != task->task_loop) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             goto different_loop; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (!fut->fut_blocking) { | 
					
						
							|  |  |  |             goto yield_insteadof_yf; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |         int res; | 
					
						
							|  |  |  |         Py_BEGIN_CRITICAL_SECTION(result); | 
					
						
							|  |  |  |         res = future_awaited_by_add(state, (FutureObj *)result, (PyObject *)task); | 
					
						
							|  |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |         if (res) { | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         fut->fut_blocking = 0; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         /* result.add_done_callback(task._wakeup) */ | 
					
						
							| 
									
										
										
										
											2020-10-21 17:49:10 -07:00
										 |  |  |         wrapper = PyCFunction_New(&TaskWakeupDef, (PyObject *)task); | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (wrapper == NULL) { | 
					
						
							|  |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |         Py_BEGIN_CRITICAL_SECTION(result); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         tmp = future_add_done_callback(state, | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             (FutureObj*)result, wrapper, task->task_context); | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         Py_DECREF(wrapper); | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |         if (tmp == NULL) { | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |         Py_DECREF(tmp); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         /* task._fut_waiter = result */ | 
					
						
							|  |  |  |         task->task_fut_waiter = result;  /* no incref is necessary */ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (task->task_must_cancel) { | 
					
						
							|  |  |  |             PyObject *r; | 
					
						
							| 
									
										
										
										
											2018-10-03 10:30:31 -04:00
										 |  |  |             int is_true; | 
					
						
							| 
									
										
										
										
											2024-11-02 03:46:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Beware: An evil `__getattribute__` could
 | 
					
						
							|  |  |  |             // prematurely delete task->task_cancel_msg before the
 | 
					
						
							|  |  |  |             // task is cancelled, thereby causing a UAF crash.
 | 
					
						
							|  |  |  |             //
 | 
					
						
							|  |  |  |             // See https://github.com/python/cpython/issues/126138
 | 
					
						
							|  |  |  |             PyObject *task_cancel_msg = Py_NewRef(task->task_cancel_msg); | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |             r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel), | 
					
						
							| 
									
										
										
										
											2024-11-02 03:46:00 -04:00
										 |  |  |                                           task_cancel_msg); | 
					
						
							|  |  |  |             Py_DECREF(task_cancel_msg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             if (r == NULL) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-10-03 10:30:31 -04:00
										 |  |  |             is_true = PyObject_IsTrue(r); | 
					
						
							|  |  |  |             Py_DECREF(r); | 
					
						
							|  |  |  |             if (is_true < 0) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (is_true) { | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |                 task->task_must_cancel = 0; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check if `result` is None */ | 
					
						
							|  |  |  |     if (result == Py_None) { | 
					
						
							|  |  |  |         /* Bare yield relinquishes control for one event loop iteration. */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         if (task_call_step_soon(state, task, NULL)) { | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             goto fail; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         return result; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check if `result` is a Future-compatible object */ | 
					
						
							| 
									
										
										
										
											2023-07-12 08:57:10 +03:00
										 |  |  |     if (PyObject_GetOptionalAttr(result, &_Py_ID(_asyncio_future_blocking), &o) < 0) { | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (o != NULL && o != Py_None) { | 
					
						
							|  |  |  |         /* `result` is a Future-compatible object */ | 
					
						
							|  |  |  |         PyObject *wrapper; | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |         PyObject *tmp; | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         int blocking = PyObject_IsTrue(o); | 
					
						
							|  |  |  |         Py_DECREF(o); | 
					
						
							|  |  |  |         if (blocking < 0) { | 
					
						
							|  |  |  |             goto fail; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /* Check if `result` future is attached to a different loop */ | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         PyObject *oloop = get_future_loop(state, result); | 
					
						
							| 
									
										
										
										
											2022-08-04 15:51:38 +02:00
										 |  |  |         if (oloop == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-04 15:51:38 +02:00
										 |  |  |         if (oloop != task->task_loop) { | 
					
						
							|  |  |  |             Py_DECREF(oloop); | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             goto different_loop; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-04 15:51:38 +02:00
										 |  |  |         Py_DECREF(oloop); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (!blocking) { | 
					
						
							|  |  |  |             goto yield_insteadof_yf; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |         if (TaskOrFuture_Check(state, result)) { | 
					
						
							|  |  |  |             int res; | 
					
						
							|  |  |  |             Py_BEGIN_CRITICAL_SECTION(result); | 
					
						
							|  |  |  |             res = future_awaited_by_add(state, (FutureObj *)result, (PyObject *)task); | 
					
						
							|  |  |  |             Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |             if (res) { | 
					
						
							|  |  |  |                 goto fail; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         /* result._asyncio_future_blocking = False */ | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         if (PyObject_SetAttr( | 
					
						
							|  |  |  |                 result, &_Py_ID(_asyncio_future_blocking), Py_False) == -1) { | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 17:49:10 -07:00
										 |  |  |         wrapper = PyCFunction_New(&TaskWakeupDef, (PyObject *)task); | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (wrapper == NULL) { | 
					
						
							|  |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         /* result.add_done_callback(task._wakeup) */ | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |         PyObject *add_cb = PyObject_GetAttr( | 
					
						
							|  |  |  |             result, &_Py_ID(add_done_callback)); | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (add_cb == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(wrapper); | 
					
						
							|  |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyObject *stack[2]; | 
					
						
							|  |  |  |         stack[0] = wrapper; | 
					
						
							|  |  |  |         stack[1] = (PyObject *)task->task_context; | 
					
						
							| 
									
										
										
										
											2022-05-27 16:31:41 +01:00
										 |  |  |         EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, add_cb); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         tmp = PyObject_Vectorcall(add_cb, stack, 1, state->context_kwname); | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         Py_DECREF(add_cb); | 
					
						
							|  |  |  |         Py_DECREF(wrapper); | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |         if (tmp == NULL) { | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             goto fail; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-30 00:33:51 +03:00
										 |  |  |         Py_DECREF(tmp); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         /* task._fut_waiter = result */ | 
					
						
							|  |  |  |         task->task_fut_waiter = result;  /* no incref is necessary */ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         if (task->task_must_cancel) { | 
					
						
							|  |  |  |             PyObject *r; | 
					
						
							|  |  |  |             int is_true; | 
					
						
							| 
									
										
										
										
											2024-11-02 03:46:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Beware: An evil `__getattribute__` could
 | 
					
						
							|  |  |  |             // prematurely delete task->task_cancel_msg before the
 | 
					
						
							|  |  |  |             // task is cancelled, thereby causing a UAF crash.
 | 
					
						
							|  |  |  |             //
 | 
					
						
							|  |  |  |             // See https://github.com/python/cpython/issues/126138
 | 
					
						
							|  |  |  |             PyObject *task_cancel_msg = Py_NewRef(task->task_cancel_msg); | 
					
						
							| 
									
										
										
										
											2022-11-02 22:46:06 +05:30
										 |  |  |             r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel), | 
					
						
							| 
									
										
										
										
											2024-11-02 03:46:00 -04:00
										 |  |  |                                           task_cancel_msg); | 
					
						
							|  |  |  |             Py_DECREF(task_cancel_msg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             if (r == NULL) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |             is_true = PyObject_IsTrue(r); | 
					
						
							|  |  |  |             Py_DECREF(r); | 
					
						
							|  |  |  |             if (is_true < 0) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (is_true) { | 
					
						
							|  |  |  |                 task->task_must_cancel = 0; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |     Py_XDECREF(o); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     /* Check if `result` is a generator */ | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |     res = PyObject_IsInstance(result, (PyObject*)&PyGen_Type); | 
					
						
							|  |  |  |     if (res < 0) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-21 09:11:32 +03:00
										 |  |  |     if (res) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         /* `result` is a generator */ | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |         o = task_set_error_soon( | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             state, task, PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             "yield was used instead of yield from for " | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |             "generator in task %R with %R", task, result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         Py_DECREF(result); | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |         return o; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* The `result` is none of the above */ | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |     o = task_set_error_soon( | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         state, task, PyExc_RuntimeError, "Task got bad yield: %R", result); | 
					
						
							| 
									
										
										
										
											2018-05-20 16:30:31 +03:00
										 |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     return o; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | self_await: | 
					
						
							|  |  |  |     o = task_set_error_soon( | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         state, task, PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         "Task cannot await on itself: %R", task); | 
					
						
							|  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     return o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | yield_insteadof_yf: | 
					
						
							|  |  |  |     o = task_set_error_soon( | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         state, task, PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         "yield was used instead of yield from " | 
					
						
							|  |  |  |         "in task %R with %R", | 
					
						
							|  |  |  |         task, result); | 
					
						
							|  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     return o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | different_loop: | 
					
						
							|  |  |  |     o = task_set_error_soon( | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |         state, task, PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         "Task %R got Future %R attached to a different loop", | 
					
						
							|  |  |  |         task, result); | 
					
						
							|  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     return o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     Py_XDECREF(result); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | task_step(asyncio_state *state, TaskObj *task, PyObject *exc) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (enter_task(task->task_loop, (PyObject*)task) < 0) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     res = task_step_impl(state, task, exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |         PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |         leave_task(task->task_loop, (PyObject*)task); | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |         _PyErr_ChainExceptions1(exc); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |         if (leave_task(task->task_loop, (PyObject*)task) < 0) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             Py_DECREF(res); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | task_eager_start(asyncio_state *state, TaskObj *task) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(task != NULL); | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     PyObject *prevtask = swap_current_task(task->task_loop, (PyObject *)task); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     if (prevtask == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     // register the task into the linked list of tasks
 | 
					
						
							|  |  |  |     // if the task completes eagerly (without suspending) then it will unregister itself
 | 
					
						
							|  |  |  |     // in future_schedule_callbacks when done, otherwise
 | 
					
						
							|  |  |  |     // it will continue as a regular (non-eager) asyncio task
 | 
					
						
							|  |  |  |     register_task(task); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (PyContext_Enter(task->task_context) == -1) { | 
					
						
							|  |  |  |         Py_DECREF(prevtask); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int retval = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     PyObject *stepres; | 
					
						
							|  |  |  |     Py_BEGIN_CRITICAL_SECTION(task); | 
					
						
							|  |  |  |     stepres = task_step_impl(state, task, NULL); | 
					
						
							|  |  |  |     Py_END_CRITICAL_SECTION(); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     if (stepres == NULL) { | 
					
						
							|  |  |  |         PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							|  |  |  |         _PyErr_ChainExceptions1(exc); | 
					
						
							|  |  |  |         retval = -1; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Py_DECREF(stepres); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     PyObject *curtask = swap_current_task(task->task_loop, prevtask); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     Py_DECREF(prevtask); | 
					
						
							|  |  |  |     if (curtask == NULL) { | 
					
						
							|  |  |  |         retval = -1; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         assert(curtask == (PyObject *)task); | 
					
						
							|  |  |  |         Py_DECREF(curtask); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyContext_Exit(task->task_context) == -1) { | 
					
						
							|  |  |  |         retval = -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     if (task->task_state != STATE_PENDING) { | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |         // This seems to really help performance on pyperformance benchmarks
 | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |         clear_task_coro(task); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return retval; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | task_wakeup_lock_held(TaskObj *task, PyObject *o) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(task); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     PyObject *result; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     assert(o); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state_by_def((PyObject *)task); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (TaskOrFuture_Check(state, o)) { | 
					
						
							|  |  |  |         int res; | 
					
						
							|  |  |  |         Py_BEGIN_CRITICAL_SECTION(o); | 
					
						
							|  |  |  |         res = future_awaited_by_discard(state, (FutureObj *)o, (PyObject *)task); | 
					
						
							|  |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |         if (res) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     if (Future_CheckExact(state, o) || Task_CheckExact(state, o)) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         PyObject *fut_result = NULL; | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |         int res; | 
					
						
							|  |  |  |         Py_BEGIN_CRITICAL_SECTION(o); | 
					
						
							|  |  |  |         res = future_get_result(state, (FutureObj*)o, &fut_result); | 
					
						
							|  |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         switch(res) { | 
					
						
							|  |  |  |         case -1: | 
					
						
							|  |  |  |             assert(fut_result == NULL); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |             break; /* exception raised */ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         case 0: | 
					
						
							|  |  |  |             Py_DECREF(fut_result); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             return task_step(state, task, NULL); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         default: | 
					
						
							|  |  |  |             assert(res == 1); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             result = task_step(state, task, fut_result); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             Py_DECREF(fut_result); | 
					
						
							|  |  |  |             return result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     else { | 
					
						
							|  |  |  |         PyObject *fut_result = PyObject_CallMethod(o, "result", NULL); | 
					
						
							|  |  |  |         if (fut_result != NULL) { | 
					
						
							|  |  |  |             Py_DECREF(fut_result); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |             return task_step(state, task, NULL); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         /* exception raised */ | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							|  |  |  |     assert(exc); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     result = task_step(state, task, exc); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:43:03 +00:00
										 |  |  |     Py_DECREF(exc); | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  | task_wakeup(PyObject *op, PyObject *arg) | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     TaskObj *task = (TaskObj*)op; | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     Py_BEGIN_CRITICAL_SECTION(task); | 
					
						
							| 
									
										
										
										
											2025-03-14 14:37:39 +01:00
										 |  |  |     res = task_wakeup_lock_held(task, arg); | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | /*********************** Functions **************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._get_running_loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return the running event loop or None. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is a low-level function intended to be used by event loops. | 
					
						
							|  |  |  | This function is thread-specific. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__get_running_loop_impl(PyObject *module) | 
					
						
							|  |  |  | /*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-17 18:21:24 -04:00
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2024-07-17 01:09:58 +08:00
										 |  |  |     PyObject *loop = Py_XNewRef(ts->asyncio_running_loop); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     if (loop == NULL) { | 
					
						
							|  |  |  |         /* There's no currently running event loop */ | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return loop; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._set_running_loop | 
					
						
							|  |  |  |     loop: 'O' | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Set the running event loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is a low-level function intended to be used by event loops. | 
					
						
							|  |  |  | This function is thread-specific. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__set_running_loop(PyObject *module, PyObject *loop) | 
					
						
							|  |  |  | /*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-17 18:21:24 -04:00
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2024-07-17 01:09:58 +08:00
										 |  |  |     if (loop == Py_None) { | 
					
						
							|  |  |  |         loop = NULL; | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-17 01:09:58 +08:00
										 |  |  |     Py_XSETREF(ts->asyncio_running_loop, Py_XNewRef(loop)); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.get_event_loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return an asyncio event loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When called from a coroutine or a callback (e.g. scheduled with | 
					
						
							|  |  |  | call_soon or similar API), this function will always return the | 
					
						
							|  |  |  | running event loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If there is no running event loop set, the function will return | 
					
						
							|  |  |  | the result of `get_event_loop_policy().get_event_loop()` call. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_get_event_loop_impl(PyObject *module) | 
					
						
							|  |  |  | /*[clinic end generated code: output=2a2d8b2f824c648b input=9364bf2916c8655d]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |     return get_event_loop(state); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.get_running_loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return the running event loop.  Raise a RuntimeError if there is none. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This function is thread-specific. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_get_running_loop_impl(PyObject *module) | 
					
						
							|  |  |  | /*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *loop; | 
					
						
							| 
									
										
										
										
											2024-07-17 18:21:24 -04:00
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2024-07-17 01:09:58 +08:00
										 |  |  |     loop = Py_XNewRef(ts->asyncio_running_loop); | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     if (loop == NULL) { | 
					
						
							|  |  |  |         /* There's no currently running event loop */ | 
					
						
							|  |  |  |         PyErr_SetString( | 
					
						
							|  |  |  |             PyExc_RuntimeError, "no running event loop"); | 
					
						
							| 
									
										
										
										
											2024-07-17 01:09:58 +08:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     return loop; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._register_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Register a new task in asyncio as executed by loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns None. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | _asyncio__register_task_impl(PyObject *module, PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=8672dadd69a7d4e2 input=21075aaea14dfbad]*/ | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     if (Task_Check(state, task)) { | 
					
						
							|  |  |  |         // task is an asyncio.Task instance or subclass, use efficient
 | 
					
						
							|  |  |  |         // linked-list implementation.
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  |         register_task((TaskObj *)task); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // As task does not inherit from asyncio.Task, fallback to less efficient
 | 
					
						
							|  |  |  |     // weakset implementation.
 | 
					
						
							|  |  |  |     PyObject *res = PyObject_CallMethodOneArg(state->non_asyncio_tasks, | 
					
						
							|  |  |  |                                               &_Py_ID(add), task); | 
					
						
							|  |  |  |     if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     Py_DECREF(res); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._register_eager_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Register a new task in asyncio as executed by loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns None. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__register_eager_task_impl(PyObject *module, PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=dfe1d45367c73f1a input=237f684683398c51]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     if (Task_Check(state, task)) { | 
					
						
							|  |  |  |         // task is an asyncio.Task instance or subclass, use efficient
 | 
					
						
							|  |  |  |         // linked-list implementation.
 | 
					
						
							|  |  |  |         register_task((TaskObj *)task); | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PySet_Add(state->non_asyncio_eager_tasks, task) < 0) { | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._unregister_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Unregister a task. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns None. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  | _asyncio__unregister_task_impl(PyObject *module, PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=6e5585706d568a46 input=28fb98c3975f7bdc]*/ | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     if (Task_Check(state, task)) { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:07:56 +05:30
										 |  |  |         unregister_task((TaskObj *)task); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *res = PyObject_CallMethodOneArg(state->non_asyncio_tasks, | 
					
						
							|  |  |  |                                               &_Py_ID(discard), task); | 
					
						
							|  |  |  |     if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     Py_DECREF(res); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._unregister_eager_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Unregister a task. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns None. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__unregister_eager_task_impl(PyObject *module, PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=a426922bd07f23d1 input=9d07401ef14ee048]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     if (Task_Check(state, task)) { | 
					
						
							|  |  |  |         // task is an asyncio.Task instance or subclass, use efficient
 | 
					
						
							|  |  |  |         // linked-list implementation.
 | 
					
						
							|  |  |  |         unregister_task((TaskObj *)task); | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PySet_Discard(state->non_asyncio_eager_tasks, task) < 0) { | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._enter_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loop: object | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enter into task execution or resume suspended task. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Task belongs to loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns None. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__enter_task_impl(PyObject *module, PyObject *loop, PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=a22611c858035b73 input=de1b06dca70d8737]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (enter_task(loop, task) < 0) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._leave_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loop: object | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Leave task execution or suspend a task. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Task belongs to loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns None. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     if (leave_task(loop, task) < 0) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio._swap_current_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loop: object | 
					
						
							|  |  |  |     task: object | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Temporarily swap in the supplied task and return the original one (or None). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is intended for use during eager coroutine execution. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio__swap_current_task_impl(PyObject *module, PyObject *loop, | 
					
						
							|  |  |  |                                  PyObject *task) | 
					
						
							|  |  |  | /*[clinic end generated code: output=9f88de958df74c7e input=c9c72208d3d38b6c]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     return swap_current_task(loop, task); | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 15:38:12 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.current_task | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loop: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return a currently executed task. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_current_task_impl(PyObject *module, PyObject *loop) | 
					
						
							|  |  |  | /*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (loop == Py_None) { | 
					
						
							|  |  |  |         loop = _asyncio_get_running_loop_impl(module); | 
					
						
							|  |  |  |         if (loop == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Py_INCREF(loop); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							|  |  |  |     // Fast path for the current running loop of current thread
 | 
					
						
							|  |  |  |     // no locking or stop the world pause is required
 | 
					
						
							|  |  |  |     if (ts->asyncio_running_loop == loop) { | 
					
						
							|  |  |  |         if (ts->asyncio_running_task != NULL) { | 
					
						
							|  |  |  |             Py_DECREF(loop); | 
					
						
							|  |  |  |             return Py_NewRef(ts->asyncio_running_task); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(loop); | 
					
						
							| 
									
										
										
										
											2022-12-22 15:38:12 +02:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-19 22:04:49 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     PyObject *ret = Py_None; | 
					
						
							|  |  |  |     // Stop the world and traverse the per-thread current tasks
 | 
					
						
							|  |  |  |     // and return the task if the loop matches
 | 
					
						
							|  |  |  |     PyInterpreterState *interp = ts->base.interp; | 
					
						
							|  |  |  |     _PyEval_StopTheWorld(interp); | 
					
						
							|  |  |  |     _Py_FOR_EACH_TSTATE_BEGIN(interp, p) { | 
					
						
							|  |  |  |         ts = (_PyThreadStateImpl *)p; | 
					
						
							|  |  |  |         if (ts->asyncio_running_loop == loop) { | 
					
						
							|  |  |  |             if (ts->asyncio_running_task != NULL) { | 
					
						
							|  |  |  |                 ret = Py_NewRef(ts->asyncio_running_task); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             goto exit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | exit: | 
					
						
							|  |  |  |     _Py_FOR_EACH_TSTATE_END(interp); | 
					
						
							|  |  |  |     _PyEval_StartTheWorld(interp); | 
					
						
							|  |  |  |     Py_DECREF(loop); | 
					
						
							| 
									
										
										
										
											2022-12-22 15:38:12 +02:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | static inline int | 
					
						
							|  |  |  | add_one_task(asyncio_state *state, PyObject *tasks, PyObject *task, PyObject *loop) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  |     assert(PySet_CheckExact(tasks)); | 
					
						
							| 
									
										
										
										
											2025-02-10 15:04:33 +05:30
										 |  |  |     if (Task_CheckExact(state, task)) { | 
					
						
							|  |  |  |         int pending = 0; | 
					
						
							|  |  |  |         Py_BEGIN_CRITICAL_SECTION(task); | 
					
						
							|  |  |  |         pending = ((TaskObj *)task)->task_state == STATE_PENDING && ((TaskObj *)task)->task_loop == loop; | 
					
						
							|  |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |         if (pending) { | 
					
						
							|  |  |  |             if (PySet_Add(tasks, task) < 0) { | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     PyObject *done = PyObject_CallMethodNoArgs(task, &_Py_ID(done)); | 
					
						
							|  |  |  |     if (done == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (Py_IsTrue(done)) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(done); | 
					
						
							|  |  |  |     PyObject *task_loop = get_future_loop(state, task); | 
					
						
							|  |  |  |     if (task_loop == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (task_loop == loop) { | 
					
						
							|  |  |  |         if (PySet_Add(tasks, task) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(task_loop); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(task_loop); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  | static inline int | 
					
						
							|  |  |  | add_tasks_llist(struct llist_node *head, PyListObject *tasks) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct llist_node *node; | 
					
						
							|  |  |  |     llist_for_each_safe(node, head) { | 
					
						
							|  |  |  |         TaskObj *task = llist_data(node, TaskObj, task_node); | 
					
						
							| 
									
										
										
										
											2025-02-13 13:19:53 +05:30
										 |  |  |         assert(task->task_state == STATE_PENDING); | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  |         // The linked list holds borrowed references to task
 | 
					
						
							|  |  |  |         // as such it is possible that the task is concurrently
 | 
					
						
							|  |  |  |         // deallocated while added to this list.
 | 
					
						
							|  |  |  |         // To protect against concurrent deallocations,
 | 
					
						
							|  |  |  |         // we first try to incref the task which would fail
 | 
					
						
							|  |  |  |         // if it is concurrently getting deallocated in another thread,
 | 
					
						
							|  |  |  |         // otherwise it gets added to the list.
 | 
					
						
							|  |  |  |         if (_Py_TryIncref((PyObject *)task)) { | 
					
						
							|  |  |  |             if (_PyList_AppendTakeRef(tasks, (PyObject *)task) < 0) { | 
					
						
							|  |  |  |                 // do not call any escaping calls here while the world is stopped.
 | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | add_tasks_interp(PyInterpreterState *interp, PyListObject *tasks) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     assert(interp->stoptheworld.world_stopped); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     // Start traversing from interpreter's linked list
 | 
					
						
							|  |  |  |     struct llist_node *head = &interp->asyncio_tasks_head; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (add_tasks_llist(head, tasks) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  |     // traverse the task lists of thread states
 | 
					
						
							|  |  |  |     _Py_FOR_EACH_TSTATE_BEGIN(interp, p) { | 
					
						
							|  |  |  |         _PyThreadStateImpl *ts = (_PyThreadStateImpl *)p; | 
					
						
							|  |  |  |         head = &ts->asyncio_tasks_head; | 
					
						
							|  |  |  |         if (add_tasks_llist(head, tasks) < 0) { | 
					
						
							|  |  |  |             ret = -1; | 
					
						
							|  |  |  |             goto exit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | exit: | 
					
						
							|  |  |  |     _Py_FOR_EACH_TSTATE_END(interp); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | /*********************** Module **************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.all_tasks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loop: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return a set of all tasks for the loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_all_tasks_impl(PyObject *module, PyObject *loop) | 
					
						
							|  |  |  | /*[clinic end generated code: output=0e107cbb7f72aa7b input=43a1b423c2d95bfa]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							|  |  |  |     if (loop == Py_None) { | 
					
						
							|  |  |  |         loop = _asyncio_get_running_loop_impl(module); | 
					
						
							|  |  |  |         if (loop == NULL) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Py_INCREF(loop); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     // First add eager tasks to the list so that we don't miss
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     // any tasks which graduates from eager to non-eager
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     // We first add all the tasks to `tasks` list and then filter
 | 
					
						
							|  |  |  |     // out the tasks which are done and return it as a set.
 | 
					
						
							|  |  |  |     PyObject *tasks = PyList_New(0); | 
					
						
							|  |  |  |     if (tasks == NULL) { | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |         Py_DECREF(loop); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     if (PyList_Extend(tasks, state->non_asyncio_eager_tasks) < 0) { | 
					
						
							| 
									
										
										
										
											2024-12-25 18:42:04 +01:00
										 |  |  |         Py_DECREF(tasks); | 
					
						
							|  |  |  |         Py_DECREF(loop); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-10 14:26:32 +05:30
										 |  |  |     if (PyList_Extend(tasks, state->non_asyncio_tasks) < 0) { | 
					
						
							|  |  |  |         Py_DECREF(tasks); | 
					
						
							|  |  |  |         Py_DECREF(loop); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-07 00:21:07 +05:30
										 |  |  |     PyInterpreterState *interp = PyInterpreterState_Get(); | 
					
						
							|  |  |  |     // Stop the world and traverse the per-thread linked list
 | 
					
						
							|  |  |  |     // of asyncio tasks for every thread, as well as the
 | 
					
						
							|  |  |  |     // interpreter's linked list, and add them to `tasks`.
 | 
					
						
							|  |  |  |     // The interpreter linked list is used for any lingering tasks
 | 
					
						
							|  |  |  |     // whose thread state has been deallocated while the task was
 | 
					
						
							|  |  |  |     // still alive. This can happen if a task is referenced by
 | 
					
						
							|  |  |  |     // a different thread, in which case the task is moved to
 | 
					
						
							|  |  |  |     // the interpreter's linked list from the thread's linked
 | 
					
						
							|  |  |  |     // list before deallocation. See PyThreadState_Clear.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // The stop-the-world pause is required so that no thread
 | 
					
						
							|  |  |  |     // modifies its linked list while being iterated here
 | 
					
						
							|  |  |  |     // in parallel. This design allows for lock-free
 | 
					
						
							|  |  |  |     // register_task/unregister_task for loops running in parallel
 | 
					
						
							|  |  |  |     // in different threads (the general case).
 | 
					
						
							|  |  |  |     _PyEval_StopTheWorld(interp); | 
					
						
							|  |  |  |     int ret = add_tasks_interp(interp, (PyListObject *)tasks); | 
					
						
							|  |  |  |     _PyEval_StartTheWorld(interp); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         // call any escaping calls after starting the world to avoid any deadlocks.
 | 
					
						
							|  |  |  |         Py_DECREF(tasks); | 
					
						
							|  |  |  |         Py_DECREF(loop); | 
					
						
							| 
									
										
										
										
											2024-08-11 20:49:31 +05:30
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-10 14:26:32 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     // All the tasks are now in the list, now filter the tasks which are done
 | 
					
						
							|  |  |  |     PyObject *res = PySet_New(NULL); | 
					
						
							|  |  |  |     if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2024-12-25 18:42:04 +01:00
										 |  |  |         Py_DECREF(tasks); | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |         Py_DECREF(loop); | 
					
						
							| 
									
										
										
										
											2024-12-25 18:42:04 +01:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 14:18:22 +05:30
										 |  |  |     for (Py_ssize_t i = 0; i < PyList_GET_SIZE(tasks); i++) { | 
					
						
							|  |  |  |         PyObject *task = PyList_GET_ITEM(tasks, i); | 
					
						
							|  |  |  |         if (add_one_task(state, res, task, loop) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(res); | 
					
						
							|  |  |  |             Py_DECREF(tasks); | 
					
						
							|  |  |  |             Py_DECREF(loop); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_DECREF(tasks); | 
					
						
							|  |  |  |     Py_DECREF(loop); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.future_add_to_awaited_by | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fut: object | 
					
						
							|  |  |  |     waiter: object | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Record that `fut` is awaited on by `waiter`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_future_add_to_awaited_by_impl(PyObject *module, PyObject *fut, | 
					
						
							|  |  |  |                                        PyObject *waiter) | 
					
						
							|  |  |  | /*[clinic end generated code: output=0ab9a1a63389e4df input=06e6eaac51f532b9]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (TaskOrFuture_Check(state, fut) && TaskOrFuture_Check(state, waiter)) { | 
					
						
							|  |  |  |         int res; | 
					
						
							|  |  |  |         Py_BEGIN_CRITICAL_SECTION(fut); | 
					
						
							|  |  |  |         res = future_awaited_by_add(state, (FutureObj *)fut, waiter); | 
					
						
							|  |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |         if (res) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _asyncio.future_discard_from_awaited_by | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fut: object | 
					
						
							|  |  |  |     waiter: object | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut, | 
					
						
							|  |  |  |                                              PyObject *waiter) | 
					
						
							|  |  |  | /*[clinic end generated code: output=a03b0b4323b779de input=3833f7639e88e483]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(module); | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |     if (TaskOrFuture_Check(state, fut) && TaskOrFuture_Check(state, waiter)) { | 
					
						
							|  |  |  |         int res; | 
					
						
							|  |  |  |         Py_BEGIN_CRITICAL_SECTION(fut); | 
					
						
							| 
									
										
										
										
											2025-02-06 22:05:33 +05:30
										 |  |  |         res = future_awaited_by_discard(state, (FutureObj *)fut, waiter); | 
					
						
							| 
									
										
										
										
											2025-02-06 21:33:52 +05:30
										 |  |  |         Py_END_CRITICAL_SECTION(); | 
					
						
							|  |  |  |         if (res) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static int | 
					
						
							|  |  |  | module_traverse(PyObject *mod, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(mod); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_VISIT(state->FutureIterType); | 
					
						
							|  |  |  |     Py_VISIT(state->TaskStepMethWrapper_Type); | 
					
						
							|  |  |  |     Py_VISIT(state->FutureType); | 
					
						
							|  |  |  |     Py_VISIT(state->TaskType); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_mod); | 
					
						
							|  |  |  |     Py_VISIT(state->traceback_extract_stack); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_future_repr_func); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_get_event_loop_policy); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_iscoroutine_func); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_task_get_stack_func); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_task_print_stack_func); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_task_repr_func); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_InvalidStateError); | 
					
						
							|  |  |  |     Py_VISIT(state->asyncio_CancelledError); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     Py_VISIT(state->non_asyncio_tasks); | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     Py_VISIT(state->non_asyncio_eager_tasks); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_VISIT(state->iscoroutine_typecache); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_VISIT(state->context_kwname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static int | 
					
						
							|  |  |  | module_clear(PyObject *mod) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     asyncio_state *state = get_asyncio_state(mod); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_CLEAR(state->FutureIterType); | 
					
						
							|  |  |  |     Py_CLEAR(state->TaskStepMethWrapper_Type); | 
					
						
							|  |  |  |     Py_CLEAR(state->FutureType); | 
					
						
							|  |  |  |     Py_CLEAR(state->TaskType); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_CLEAR(state->asyncio_mod); | 
					
						
							|  |  |  |     Py_CLEAR(state->traceback_extract_stack); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_future_repr_func); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_get_event_loop_policy); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_iscoroutine_func); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_task_get_stack_func); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_task_print_stack_func); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_task_repr_func); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_InvalidStateError); | 
					
						
							|  |  |  |     Py_CLEAR(state->asyncio_CancelledError); | 
					
						
							| 
									
										
										
										
											2017-12-17 20:19:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     Py_CLEAR(state->non_asyncio_tasks); | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     Py_CLEAR(state->non_asyncio_eager_tasks); | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     Py_CLEAR(state->iscoroutine_typecache); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_CLEAR(state->context_kwname); | 
					
						
							| 
									
										
										
										
											2024-12-18 19:55:03 +05:30
										 |  |  |     // Clear the ref to running loop so that finalizers can run early.
 | 
					
						
							|  |  |  |     // If there are other running loops in different threads,
 | 
					
						
							|  |  |  |     // those get cleared in PyThreadState_Clear.
 | 
					
						
							|  |  |  |     _PyThreadStateImpl *ts = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							|  |  |  |     Py_CLEAR(ts->asyncio_running_loop); | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     Py_CLEAR(ts->asyncio_running_task); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | module_free(void *mod) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void)module_clear((PyObject *)mod); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | module_init(asyncio_state *state) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *module = NULL; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     state->asyncio_mod = PyImport_ImportModule("asyncio"); | 
					
						
							|  |  |  |     if (state->asyncio_mod == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     state->iscoroutine_typecache = PySet_New(NULL); | 
					
						
							|  |  |  |     if (state->iscoroutine_typecache == NULL) { | 
					
						
							| 
									
										
										
										
											2017-12-19 07:18:45 -05:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     state->context_kwname = Py_BuildValue("(s)", "context"); | 
					
						
							|  |  |  |     if (state->context_kwname == NULL) { | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | #define WITH_MOD(NAME) \
 | 
					
						
							|  |  |  |     Py_CLEAR(module); \ | 
					
						
							|  |  |  |     module = PyImport_ImportModule(NAME); \ | 
					
						
							|  |  |  |     if (module == NULL) { \ | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |         goto fail; \ | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define GET_MOD_ATTR(VAR, NAME) \
 | 
					
						
							|  |  |  |     VAR = PyObject_GetAttrString(module, NAME); \ | 
					
						
							|  |  |  |     if (VAR == NULL) { \ | 
					
						
							|  |  |  |         goto fail; \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     WITH_MOD("asyncio.events") | 
					
						
							| 
									
										
										
										
											2024-12-18 18:04:20 +05:30
										 |  |  |     GET_MOD_ATTR(state->asyncio_get_event_loop_policy, "_get_event_loop_policy") | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     WITH_MOD("asyncio.base_futures") | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     GET_MOD_ATTR(state->asyncio_future_repr_func, "_future_repr") | 
					
						
							| 
									
										
										
										
											2018-09-11 10:13:04 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     WITH_MOD("asyncio.exceptions") | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     GET_MOD_ATTR(state->asyncio_InvalidStateError, "InvalidStateError") | 
					
						
							|  |  |  |     GET_MOD_ATTR(state->asyncio_CancelledError, "CancelledError") | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     WITH_MOD("asyncio.base_tasks") | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     GET_MOD_ATTR(state->asyncio_task_repr_func, "_task_repr") | 
					
						
							|  |  |  |     GET_MOD_ATTR(state->asyncio_task_get_stack_func, "_task_get_stack") | 
					
						
							|  |  |  |     GET_MOD_ATTR(state->asyncio_task_print_stack_func, "_task_print_stack") | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 07:04:38 +02:00
										 |  |  |     WITH_MOD("asyncio.coroutines") | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     GET_MOD_ATTR(state->asyncio_iscoroutine_func, "iscoroutine") | 
					
						
							| 
									
										
										
										
											2017-12-15 07:04:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     WITH_MOD("traceback") | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     GET_MOD_ATTR(state->traceback_extract_stack, "extract_stack") | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     PyObject *weak_set; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     WITH_MOD("weakref") | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     GET_MOD_ATTR(weak_set, "WeakSet"); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     state->non_asyncio_tasks = PyObject_CallNoArgs(weak_set); | 
					
						
							| 
									
										
										
										
											2017-12-23 15:04:15 -05:00
										 |  |  |     Py_CLEAR(weak_set); | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     if (state->non_asyncio_tasks == NULL) { | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 12:06:43 +05:30
										 |  |  |     state->non_asyncio_eager_tasks = PySet_New(NULL); | 
					
						
							|  |  |  |     if (state->non_asyncio_eager_tasks == NULL) { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 08:10:14 +03:00
										 |  |  |     Py_DECREF(module); | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     Py_CLEAR(module); | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef WITH_MOD
 | 
					
						
							|  |  |  | #undef GET_MOD_ATTR
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(module_doc, "Accelerator module for asyncio"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  | static PyMethodDef asyncio_methods[] = { | 
					
						
							| 
									
										
										
										
											2022-12-22 15:38:12 +02:00
										 |  |  |     _ASYNCIO_CURRENT_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     _ASYNCIO_GET_EVENT_LOOP_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_GET_RUNNING_LOOP_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO__GET_RUNNING_LOOP_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO__SET_RUNNING_LOOP_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     _ASYNCIO__REGISTER_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     _ASYNCIO__REGISTER_EAGER_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     _ASYNCIO__UNREGISTER_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     _ASYNCIO__UNREGISTER_EAGER_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  |     _ASYNCIO__ENTER_TASK_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO__LEAVE_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2023-05-01 14:10:13 -07:00
										 |  |  |     _ASYNCIO__SWAP_CURRENT_TASK_METHODDEF | 
					
						
							| 
									
										
										
										
											2024-06-22 23:28:35 +05:30
										 |  |  |     _ASYNCIO_ALL_TASKS_METHODDEF | 
					
						
							| 
									
										
										
										
											2025-01-22 08:25:29 -08:00
										 |  |  |     _ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF | 
					
						
							|  |  |  |     _ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF | 
					
						
							| 
									
										
										
										
											2017-12-13 14:49:42 -05:00
										 |  |  |     {NULL, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static int | 
					
						
							|  |  |  | module_exec(PyObject *mod) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     asyncio_state *state = get_asyncio_state(mod); | 
					
						
							| 
									
										
										
										
											2024-11-08 18:21:11 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | #define CREATE_TYPE(m, tp, spec, base)                                  \
 | 
					
						
							|  |  |  |     do {                                                                \ | 
					
						
							|  |  |  |         tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec,        \ | 
					
						
							|  |  |  |                                                   (PyObject *)base);    \ | 
					
						
							|  |  |  |         if (tp == NULL) {                                               \ | 
					
						
							|  |  |  |             return -1;                                                  \ | 
					
						
							|  |  |  |         }                                                               \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     CREATE_TYPE(mod, state->TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); | 
					
						
							|  |  |  |     CREATE_TYPE(mod, state->FutureIterType, &FutureIter_spec, NULL); | 
					
						
							|  |  |  |     CREATE_TYPE(mod, state->FutureType, &Future_spec, NULL); | 
					
						
							|  |  |  |     CREATE_TYPE(mod, state->TaskType, &Task_spec, state->FutureType); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef CREATE_TYPE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyModule_AddType(mod, state->FutureType) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     if (PyModule_AddType(mod, state->TaskType) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     // Must be done after types are added to avoid a circular dependency
 | 
					
						
							|  |  |  |     if (module_init(state) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2018-01-23 15:10:03 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | static struct PyModuleDef_Slot module_slots[] = { | 
					
						
							|  |  |  |     {Py_mod_exec, module_exec}, | 
					
						
							| 
									
										
										
										
											2023-05-05 15:11:27 -06:00
										 |  |  |     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, | 
					
						
							| 
									
										
										
										
											2024-05-03 08:30:55 -07:00
										 |  |  |     {Py_mod_gil, Py_MOD_GIL_NOT_USED}, | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  |     {0, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef _asynciomodule = { | 
					
						
							|  |  |  |     .m_base = PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     .m_name = "_asyncio", | 
					
						
							|  |  |  |     .m_doc = module_doc, | 
					
						
							|  |  |  |     .m_size = sizeof(asyncio_state), | 
					
						
							|  |  |  |     .m_methods = asyncio_methods, | 
					
						
							|  |  |  |     .m_slots = module_slots, | 
					
						
							|  |  |  |     .m_traverse = module_traverse, | 
					
						
							|  |  |  |     .m_clear = module_clear, | 
					
						
							|  |  |  |     .m_free = (freefunc)module_free, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 15:37:37 +05:30
										 |  |  | PyMODINIT_FUNC | 
					
						
							|  |  |  | PyInit__asyncio(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyModuleDef_Init(&_asynciomodule); | 
					
						
							| 
									
										
										
										
											2016-10-09 14:44:47 +09:00
										 |  |  | } |