2016-10-09 02:44:47 -03:00
|
|
|
#include "Python.h"
|
|
|
|
#include "structmember.h"
|
|
|
|
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*[clinic input]
|
|
|
|
module _asyncio
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/
|
|
|
|
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
/* identifiers used from some functions */
|
2017-12-13 15:49:42 -04:00
|
|
|
_Py_IDENTIFIER(__asyncio_running_event_loop__);
|
2018-09-21 03:11:32 -03:00
|
|
|
_Py_IDENTIFIER(_asyncio_future_blocking);
|
2016-10-28 13:52:37 -03:00
|
|
|
_Py_IDENTIFIER(add_done_callback);
|
2018-05-28 18:54:02 -03:00
|
|
|
_Py_IDENTIFIER(_all_tasks_compat);
|
2016-10-09 02:44:47 -03:00
|
|
|
_Py_IDENTIFIER(call_soon);
|
2016-10-28 13:52:37 -03:00
|
|
|
_Py_IDENTIFIER(cancel);
|
2017-12-16 15:58:38 -04:00
|
|
|
_Py_IDENTIFIER(current_task);
|
2017-12-13 15:49:42 -04:00
|
|
|
_Py_IDENTIFIER(get_event_loop);
|
2016-10-28 13:52:37 -03:00
|
|
|
_Py_IDENTIFIER(send);
|
|
|
|
_Py_IDENTIFIER(throw);
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
|
2016-10-15 03:39:19 -03:00
|
|
|
/* State of the _asyncio module */
|
2017-12-16 15:58:38 -04:00
|
|
|
static PyObject *asyncio_mod;
|
2016-10-09 02:44:47 -03:00
|
|
|
static PyObject *traceback_extract_stack;
|
2017-12-13 15:49:42 -04:00
|
|
|
static PyObject *asyncio_get_event_loop_policy;
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *asyncio_future_repr_info_func;
|
2017-12-16 15:58:38 -04:00
|
|
|
static PyObject *asyncio_iscoroutine_func;
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *asyncio_task_get_stack_func;
|
|
|
|
static PyObject *asyncio_task_print_stack_func;
|
2017-12-16 15:58:38 -04:00
|
|
|
static PyObject *asyncio_task_repr_info_func;
|
2016-10-09 02:44:47 -03:00
|
|
|
static PyObject *asyncio_InvalidStateError;
|
|
|
|
static PyObject *asyncio_CancelledError;
|
2018-01-22 20:11:18 -04:00
|
|
|
static PyObject *context_kwname;
|
2017-12-16 15:58:38 -04:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
static PyObject *cached_running_holder;
|
|
|
|
static volatile uint64_t cached_running_holder_tsid;
|
|
|
|
|
2018-08-08 18:06:47 -03:00
|
|
|
/* Counter for autogenerated Task names */
|
|
|
|
static uint64_t task_name_counter = 0;
|
2017-12-16 15:58:38 -04:00
|
|
|
|
2017-12-23 16:04:15 -04:00
|
|
|
/* WeakSet containing all alive tasks. */
|
|
|
|
static PyObject *all_tasks;
|
2017-12-16 15:58:38 -04:00
|
|
|
|
|
|
|
/* Dictionary containing tasks that are currently active in
|
|
|
|
all running event loops. {EventLoop: Task} */
|
2017-12-23 16:04:15 -04:00
|
|
|
static PyObject *current_tasks;
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2017-12-19 08:18:45 -04:00
|
|
|
/* An isinstance type cache for the 'is_coroutine()' function. */
|
|
|
|
static PyObject *iscoroutine_typecache;
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
STATE_PENDING,
|
|
|
|
STATE_CANCELLED,
|
|
|
|
STATE_FINISHED
|
|
|
|
} fut_state;
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
#define FutureObj_HEAD(prefix) \
|
|
|
|
PyObject_HEAD \
|
|
|
|
PyObject *prefix##_loop; \
|
2017-12-17 21:19:47 -04:00
|
|
|
PyObject *prefix##_callback0; \
|
2018-09-27 15:55:55 -03:00
|
|
|
PyObject *prefix##_context0; \
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *prefix##_callbacks; \
|
|
|
|
PyObject *prefix##_exception; \
|
|
|
|
PyObject *prefix##_result; \
|
|
|
|
PyObject *prefix##_source_tb; \
|
|
|
|
fut_state prefix##_state; \
|
|
|
|
int prefix##_log_tb; \
|
|
|
|
int prefix##_blocking; \
|
|
|
|
PyObject *dict; \
|
|
|
|
PyObject *prefix##_weakreflist;
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
typedef struct {
|
2016-10-28 13:52:37 -03:00
|
|
|
FutureObj_HEAD(fut)
|
2016-10-09 02:44:47 -03:00
|
|
|
} FutureObj;
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
typedef struct {
|
|
|
|
FutureObj_HEAD(task)
|
|
|
|
PyObject *task_fut_waiter;
|
|
|
|
PyObject *task_coro;
|
2018-08-08 18:06:47 -03:00
|
|
|
PyObject *task_name;
|
2018-09-27 15:55:55 -03:00
|
|
|
PyObject *task_context;
|
2016-10-28 13:52:37 -03:00
|
|
|
int task_must_cancel;
|
|
|
|
int task_log_destroy_pending;
|
|
|
|
} TaskObj;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
TaskObj *sw_task;
|
|
|
|
PyObject *sw_arg;
|
2017-09-03 02:10:14 -03:00
|
|
|
} TaskStepMethWrapper;
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
TaskObj *ww_task;
|
|
|
|
} TaskWakeupMethWrapper;
|
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *rl_loop;
|
|
|
|
#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
|
|
|
|
pid_t rl_pid;
|
|
|
|
#endif
|
|
|
|
} PyRunningLoopHolder;
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
static PyTypeObject FutureType;
|
|
|
|
static PyTypeObject TaskType;
|
2018-01-23 16:10:03 -04:00
|
|
|
static PyTypeObject PyRunningLoopHolder_Type;
|
2017-12-17 21:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
#define Future_CheckExact(obj) (Py_TYPE(obj) == &FutureType)
|
|
|
|
#define Task_CheckExact(obj) (Py_TYPE(obj) == &TaskType)
|
|
|
|
|
|
|
|
#define Future_Check(obj) PyObject_TypeCheck(obj, &FutureType)
|
|
|
|
#define Task_Check(obj) PyObject_TypeCheck(obj, &TaskType)
|
|
|
|
|
2016-10-28 13:52:37 -03: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 21:19:47 -04:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/* Get FutureIter from Future */
|
2018-01-23 16:10:03 -04:00
|
|
|
static PyObject * future_new_iter(PyObject *);
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
static PyRunningLoopHolder * new_running_loop_holder(PyObject *);
|
|
|
|
|
2017-12-13 15:49:42 -04:00
|
|
|
|
2017-12-19 08:18:45 -04:00
|
|
|
static int
|
|
|
|
_is_coroutine(PyObject *coro)
|
|
|
|
{
|
|
|
|
/* '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.
|
|
|
|
*/
|
|
|
|
PyObject *res = PyObject_CallFunctionObjArgs(
|
|
|
|
asyncio_iscoroutine_func, coro, NULL);
|
|
|
|
if (res == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_res_true = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
if (is_res_true <= 0) {
|
|
|
|
return is_res_true;
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:06:46 -04:00
|
|
|
if (PySet_GET_SIZE(iscoroutine_typecache) < 100) {
|
2017-12-19 08:18:45 -04: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.
|
|
|
|
*/
|
|
|
|
if (PySet_Add(iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
is_coroutine(PyObject *coro)
|
|
|
|
{
|
|
|
|
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(
|
|
|
|
iscoroutine_typecache, (PyObject*) Py_TYPE(coro));
|
|
|
|
if (has_it == 0) {
|
|
|
|
/* type(coro) is not in iscoroutine_typecache */
|
|
|
|
return _is_coroutine(coro);
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:36:10 -04:00
|
|
|
/* either an error has occurred or
|
2017-12-19 08:18:45 -04:00
|
|
|
type(coro) is in iscoroutine_typecache
|
|
|
|
*/
|
|
|
|
return has_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-23 16:04:15 -04:00
|
|
|
static PyObject *
|
|
|
|
get_future_loop(PyObject *fut)
|
|
|
|
{
|
|
|
|
/* Implementation of `asyncio.futures._get_loop` */
|
|
|
|
|
|
|
|
_Py_IDENTIFIER(get_loop);
|
|
|
|
_Py_IDENTIFIER(_loop);
|
2018-05-20 10:30:31 -03:00
|
|
|
PyObject *getloop;
|
2017-12-23 16:04:15 -04:00
|
|
|
|
|
|
|
if (Future_CheckExact(fut) || Task_CheckExact(fut)) {
|
|
|
|
PyObject *loop = ((FutureObj *)fut)->fut_loop;
|
|
|
|
Py_INCREF(loop);
|
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2018-05-20 10:30:31 -03:00
|
|
|
if (_PyObject_LookupAttrId(fut, &PyId_get_loop, &getloop) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-23 16:04:15 -04:00
|
|
|
if (getloop != NULL) {
|
|
|
|
PyObject *res = _PyObject_CallNoArg(getloop);
|
|
|
|
Py_DECREF(getloop);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _PyObject_GetAttrId(fut, &PyId__loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 15:49:42 -04:00
|
|
|
static int
|
|
|
|
get_running_loop(PyObject **loop)
|
|
|
|
{
|
2018-01-23 16:10:03 -04:00
|
|
|
PyObject *rl;
|
2017-12-13 15:49:42 -04:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
PyThreadState *ts = PyThreadState_Get();
|
|
|
|
if (ts->id == cached_running_holder_tsid && cached_running_holder != NULL) {
|
|
|
|
// Fast path, check the cache.
|
|
|
|
rl = cached_running_holder; // borrowed
|
2017-12-13 15:49:42 -04:00
|
|
|
}
|
2018-01-23 16:10:03 -04:00
|
|
|
else {
|
|
|
|
if (ts->dict == NULL) {
|
|
|
|
goto not_found;
|
|
|
|
}
|
2017-12-13 15:49:42 -04:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
rl = _PyDict_GetItemIdWithError(
|
|
|
|
ts->dict, &PyId___asyncio_running_event_loop__); // borrowed
|
|
|
|
if (rl == NULL) {
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cached_running_holder = rl; // borrowed
|
|
|
|
cached_running_holder_tsid = ts->id;
|
2017-12-13 15:49:42 -04:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
assert(Py_TYPE(rl) == &PyRunningLoopHolder_Type);
|
|
|
|
PyObject *running_loop = ((PyRunningLoopHolder *)rl)->rl_loop;
|
2017-12-13 15:49:42 -04:00
|
|
|
|
|
|
|
if (running_loop == Py_None) {
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
|
|
|
|
/* On Windows there is no getpid, but there is also no os.fork(),
|
|
|
|
so there is no need for this check.
|
|
|
|
*/
|
|
|
|
if (getpid() != ((PyRunningLoopHolder *)rl)->rl_pid) {
|
|
|
|
goto not_found;
|
2017-12-13 15:49:42 -04:00
|
|
|
}
|
2018-01-23 16:10:03 -04:00
|
|
|
#endif
|
2017-12-13 15:49:42 -04:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
Py_INCREF(running_loop);
|
|
|
|
*loop = running_loop;
|
|
|
|
return 0;
|
2017-12-13 15:49:42 -04:00
|
|
|
|
|
|
|
not_found:
|
|
|
|
*loop = NULL;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
*loop = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_running_loop(PyObject *loop)
|
|
|
|
{
|
2018-01-23 16:10:03 -04:00
|
|
|
cached_running_holder = NULL;
|
|
|
|
cached_running_holder_tsid = 0;
|
2017-12-13 15:49:42 -04:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
PyObject *ts_dict = PyThreadState_GetDict(); // borrowed
|
2017-12-13 15:49:42 -04:00
|
|
|
if (ts_dict == NULL) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError, "thread-local storage is not available");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
PyRunningLoopHolder *rl = new_running_loop_holder(loop);
|
|
|
|
if (rl == NULL) {
|
2017-12-13 15:49:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyDict_SetItemId(
|
2018-01-23 16:10:03 -04:00
|
|
|
ts_dict, &PyId___asyncio_running_event_loop__, (PyObject *)rl) < 0)
|
|
|
|
{
|
|
|
|
Py_DECREF(rl); // will cleanup loop & current_pid
|
2017-12-13 15:49:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2018-01-23 16:10:03 -04:00
|
|
|
Py_DECREF(rl);
|
2017-12-13 15:49:42 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
get_event_loop(void)
|
|
|
|
{
|
|
|
|
PyObject *loop;
|
|
|
|
PyObject *policy;
|
|
|
|
|
|
|
|
if (get_running_loop(&loop)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (loop != NULL) {
|
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
policy = _PyObject_CallNoArg(asyncio_get_event_loop_policy);
|
|
|
|
if (policy == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop = _PyObject_CallMethodId(policy, &PyId_get_event_loop, NULL);
|
|
|
|
Py_DECREF(policy);
|
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
static int
|
2018-09-27 15:55:55 -03:00
|
|
|
call_soon(PyObject *loop, PyObject *func, PyObject *arg, PyObject *ctx)
|
2017-12-17 21:19:47 -04:00
|
|
|
{
|
|
|
|
PyObject *handle;
|
2018-01-22 20:11:18 -04:00
|
|
|
PyObject *stack[3];
|
|
|
|
Py_ssize_t nargs;
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
handle = _PyObject_CallMethodIdObjArgs(
|
|
|
|
loop, &PyId_call_soon, func, arg, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Use FASTCALL to pass a keyword-only argument to call_soon */
|
|
|
|
|
|
|
|
PyObject *callable = _PyObject_GetAttrId(loop, &PyId_call_soon);
|
|
|
|
if (callable == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All refs in 'stack' are borrowed. */
|
|
|
|
nargs = 1;
|
|
|
|
stack[0] = func;
|
|
|
|
if (arg != NULL) {
|
|
|
|
stack[1] = arg;
|
|
|
|
nargs++;
|
|
|
|
}
|
|
|
|
stack[nargs] = (PyObject *)ctx;
|
|
|
|
|
|
|
|
handle = _PyObject_FastCallKeywords(
|
|
|
|
callable, stack, nargs, context_kwname);
|
|
|
|
Py_DECREF(callable);
|
|
|
|
}
|
|
|
|
|
2017-12-17 21:19:47 -04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define ENSURE_FUTURE_ALIVE(fut) \
|
|
|
|
do { \
|
|
|
|
assert(Future_Check(fut) || Task_Check(fut)); \
|
|
|
|
if (future_ensure_alive((FutureObj*)fut)) { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
static int
|
2016-10-28 13:52:37 -03:00
|
|
|
future_schedule_callbacks(FutureObj *fut)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
|
|
|
Py_ssize_t len;
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
if (fut->fut_callback0 != NULL) {
|
|
|
|
/* There's a 1st callback */
|
|
|
|
|
|
|
|
int ret = call_soon(
|
2018-01-22 20:11:18 -04:00
|
|
|
fut->fut_loop, fut->fut_callback0,
|
|
|
|
(PyObject *)fut, fut->fut_context0);
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_CLEAR(fut->fut_callback0);
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_CLEAR(fut->fut_context0);
|
2017-12-17 21:19:47 -04: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 02:44:47 -03:00
|
|
|
|
|
|
|
if (fut->fut_callbacks == NULL) {
|
2017-12-17 21:19:47 -04:00
|
|
|
/* No more callbacks, return. */
|
|
|
|
return 0;
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
len = PyList_GET_SIZE(fut->fut_callbacks);
|
|
|
|
if (len == 0) {
|
2017-12-17 21:19:47 -04:00
|
|
|
/* The list of callbacks was empty; clear it and return. */
|
|
|
|
Py_CLEAR(fut->fut_callbacks);
|
2016-10-09 02:44:47 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2018-01-22 20:11:18 -04:00
|
|
|
PyObject *cb_tup = PyList_GET_ITEM(fut->fut_callbacks, i);
|
|
|
|
PyObject *cb = PyTuple_GET_ITEM(cb_tup, 0);
|
|
|
|
PyObject *ctx = PyTuple_GET_ITEM(cb_tup, 1);
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2018-09-27 15:55:55 -03:00
|
|
|
if (call_soon(fut->fut_loop, cb, (PyObject *)fut, ctx)) {
|
2017-12-17 21:19:47 -04:00
|
|
|
/* If an error occurs in pure-Python implementation,
|
|
|
|
all callbacks are cleared. */
|
|
|
|
Py_CLEAR(fut->fut_callbacks);
|
2016-10-09 02:44:47 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_CLEAR(fut->fut_callbacks);
|
2016-10-09 02:44:47 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-13 06:28:33 -04:00
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
static int
|
2016-10-28 13:52:37 -03:00
|
|
|
future_init(FutureObj *fut, PyObject *loop)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *res;
|
|
|
|
int is_true;
|
2016-10-09 02:44:47 -03:00
|
|
|
_Py_IDENTIFIER(get_debug);
|
|
|
|
|
2018-02-13 06:28:33 -04:00
|
|
|
// Same to FutureObj_clear() but not clearing fut->dict
|
|
|
|
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);
|
|
|
|
Py_CLEAR(fut->fut_source_tb);
|
|
|
|
|
|
|
|
fut->fut_state = STATE_PENDING;
|
|
|
|
fut->fut_log_tb = 0;
|
|
|
|
fut->fut_blocking = 0;
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
if (loop == Py_None) {
|
2017-12-13 15:49:42 -04:00
|
|
|
loop = get_event_loop();
|
2016-10-09 02:44:47 -03:00
|
|
|
if (loop == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_INCREF(loop);
|
|
|
|
}
|
2018-02-13 06:28:33 -04:00
|
|
|
fut->fut_loop = loop;
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-29 04:05:39 -03:00
|
|
|
res = _PyObject_CallMethodId(fut->fut_loop, &PyId_get_debug, NULL);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
is_true = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
if (is_true < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-28 12:11:31 -03:00
|
|
|
if (is_true && !_Py_IsFinalizing()) {
|
|
|
|
/* Only try to capture the traceback if the interpreter is not being
|
|
|
|
finalized. The original motivation to add a `_Py_IsFinalizing()`
|
|
|
|
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.
|
|
|
|
*/
|
2018-02-13 06:28:33 -04:00
|
|
|
fut->fut_source_tb = _PyObject_CallNoArg(traceback_extract_stack);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_source_tb == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2016-10-28 13:52:37 -03:00
|
|
|
future_set_result(FutureObj *fut, PyObject *res)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (future_ensure_alive(fut)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_state != STATE_PENDING) {
|
|
|
|
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
assert(!fut->fut_result);
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_INCREF(res);
|
|
|
|
fut->fut_result = res;
|
|
|
|
fut->fut_state = STATE_FINISHED;
|
|
|
|
|
2018-01-24 12:31:01 -04:00
|
|
|
if (future_schedule_callbacks(fut) == -1) {
|
2016-10-09 02:44:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2016-10-28 13:52:37 -03:00
|
|
|
future_set_exception(FutureObj *fut, PyObject *exc)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
|
|
|
PyObject *exc_val = NULL;
|
|
|
|
|
|
|
|
if (fut->fut_state != STATE_PENDING) {
|
|
|
|
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyExceptionClass_Check(exc)) {
|
2016-12-06 13:45:50 -04:00
|
|
|
exc_val = _PyObject_CallNoArg(exc);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (exc_val == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
if (fut->fut_state != STATE_PENDING) {
|
|
|
|
Py_DECREF(exc_val);
|
|
|
|
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
exc_val = exc;
|
|
|
|
Py_INCREF(exc_val);
|
|
|
|
}
|
|
|
|
if (!PyExceptionInstance_Check(exc_val)) {
|
|
|
|
Py_DECREF(exc_val);
|
|
|
|
PyErr_SetString(PyExc_TypeError, "invalid exception object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((PyObject*)Py_TYPE(exc_val) == PyExc_StopIteration) {
|
|
|
|
Py_DECREF(exc_val);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"StopIteration interacts badly with generators "
|
|
|
|
"and cannot be raised into a Future");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
assert(!fut->fut_exception);
|
2016-10-09 02:44:47 -03:00
|
|
|
fut->fut_exception = exc_val;
|
|
|
|
fut->fut_state = STATE_FINISHED;
|
|
|
|
|
2018-01-24 12:31:01 -04:00
|
|
|
if (future_schedule_callbacks(fut) == -1) {
|
2016-10-09 02:44:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fut->fut_log_tb = 1;
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static int
|
|
|
|
future_get_result(FutureObj *fut, PyObject **result)
|
|
|
|
{
|
|
|
|
if (fut->fut_state == STATE_CANCELLED) {
|
2017-09-03 02:10:14 -03:00
|
|
|
PyErr_SetNone(asyncio_CancelledError);
|
|
|
|
return -1;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fut->fut_state != STATE_FINISHED) {
|
2017-09-03 02:10:14 -03:00
|
|
|
PyErr_SetString(asyncio_InvalidStateError, "Result is not set.");
|
|
|
|
return -1;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
fut->fut_log_tb = 0;
|
|
|
|
if (fut->fut_exception != NULL) {
|
|
|
|
Py_INCREF(fut->fut_exception);
|
|
|
|
*result = fut->fut_exception;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(fut->fut_result);
|
|
|
|
*result = fut->fut_result;
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-09-27 15:55:55 -03:00
|
|
|
future_add_done_callback(FutureObj *fut, PyObject *arg, PyObject *ctx)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (!future_is_alive(fut)) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "uninitialized Future object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_state != STATE_PENDING) {
|
2017-12-17 21:19:47 -04:00
|
|
|
/* The future is done/cancelled, so schedule the callback
|
|
|
|
right away. */
|
2018-01-22 20:11:18 -04:00
|
|
|
if (call_soon(fut->fut_loop, arg, (PyObject*) fut, ctx)) {
|
2016-10-09 02:44:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-12-17 21:19:47 -04: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 20:11:18 -04:00
|
|
|
if (fut->fut_callbacks == NULL && fut->fut_callback0 == NULL) {
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_INCREF(arg);
|
|
|
|
fut->fut_callback0 = arg;
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_INCREF(ctx);
|
|
|
|
fut->fut_context0 = ctx;
|
2017-12-17 21:19:47 -04:00
|
|
|
}
|
|
|
|
else {
|
2018-01-22 20:11:18 -04:00
|
|
|
PyObject *tup = PyTuple_New(2);
|
|
|
|
if (tup == NULL) {
|
2017-12-17 21:19:47 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(arg);
|
2018-01-22 20:11:18 -04: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) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyList_SET_ITEM(fut->fut_callbacks, 0, tup); /* borrow */
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
}
|
2017-12-17 21:19:47 -04:00
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *
|
|
|
|
future_cancel(FutureObj *fut)
|
|
|
|
{
|
2017-06-11 10:49:18 -03:00
|
|
|
fut->fut_log_tb = 0;
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
if (fut->fut_state != STATE_PENDING) {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
fut->fut_state = STATE_CANCELLED;
|
|
|
|
|
2018-01-24 12:31:01 -04:00
|
|
|
if (future_schedule_callbacks(fut) == -1) {
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.__init__
|
|
|
|
|
|
|
|
*
|
2017-09-03 02:10:14 -03:00
|
|
|
loop: object = None
|
2016-10-28 13:52:37 -03: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 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=9ed75799eaccb5d6 input=89af317082bc0bf8]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
{
|
|
|
|
return future_init(self, loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
FutureObj_clear(FutureObj *fut)
|
|
|
|
{
|
|
|
|
Py_CLEAR(fut->fut_loop);
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_CLEAR(fut->fut_callback0);
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_CLEAR(fut->fut_context0);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(fut->fut_callbacks);
|
|
|
|
Py_CLEAR(fut->fut_result);
|
|
|
|
Py_CLEAR(fut->fut_exception);
|
|
|
|
Py_CLEAR(fut->fut_source_tb);
|
|
|
|
Py_CLEAR(fut->dict);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
FutureObj_traverse(FutureObj *fut, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(fut->fut_loop);
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_VISIT(fut->fut_callback0);
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_VISIT(fut->fut_context0);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_VISIT(fut->fut_callbacks);
|
|
|
|
Py_VISIT(fut->fut_result);
|
|
|
|
Py_VISIT(fut->fut_exception);
|
|
|
|
Py_VISIT(fut->fut_source_tb);
|
|
|
|
Py_VISIT(fut->dict);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_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)
|
|
|
|
/*[clinic end generated code: output=f35f940936a4b1e5 input=49ecf9cf5ec50dc5]*/
|
|
|
|
{
|
|
|
|
PyObject *result;
|
2017-12-17 21:19:47 -04:00
|
|
|
|
|
|
|
if (!future_is_alive(self)) {
|
|
|
|
PyErr_SetString(asyncio_InvalidStateError,
|
|
|
|
"Future object is not initialized.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
int res = future_get_result(self, &result);
|
|
|
|
|
|
|
|
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]
|
|
|
|
_asyncio.Future.exception
|
|
|
|
|
|
|
|
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 *
|
|
|
|
_asyncio_Future_exception_impl(FutureObj *self)
|
|
|
|
/*[clinic end generated code: output=88b20d4f855e0710 input=733547a70c841c68]*/
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (!future_is_alive(self)) {
|
|
|
|
PyErr_SetString(asyncio_InvalidStateError,
|
|
|
|
"Future object is not initialized.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
if (self->fut_state == STATE_CANCELLED) {
|
2017-09-03 02:10:14 -03:00
|
|
|
PyErr_SetNone(asyncio_CancelledError);
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->fut_state != STATE_FINISHED) {
|
2017-09-03 02:10:14 -03:00
|
|
|
PyErr_SetString(asyncio_InvalidStateError, "Exception is not set.");
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->fut_exception != NULL) {
|
|
|
|
self->fut_log_tb = 0;
|
|
|
|
Py_INCREF(self->fut_exception);
|
|
|
|
return self->fut_exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.set_result
|
|
|
|
|
2017-12-25 11:48:15 -04:00
|
|
|
result: object
|
2016-10-28 13:52:37 -03: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 *
|
2017-12-25 11:48:15 -04:00
|
|
|
_asyncio_Future_set_result(FutureObj *self, PyObject *result)
|
|
|
|
/*[clinic end generated code: output=1ec2e6bcccd6f2ce input=8b75172c2a7b05f1]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(self)
|
2017-12-25 11:48:15 -04:00
|
|
|
return future_set_result(self, result);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.set_exception
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
exception: object
|
2016-10-28 13:52:37 -03: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 *
|
|
|
|
_asyncio_Future_set_exception(FutureObj *self, PyObject *exception)
|
2017-09-03 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=f1c1b0cd321be360 input=e45b7d7aa71cc66d]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(self)
|
2016-10-28 13:52:37 -03:00
|
|
|
return future_set_exception(self, exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.add_done_callback
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
fn: object
|
2016-10-28 13:52:37 -03:00
|
|
|
/
|
2018-01-22 20:11:18 -04:00
|
|
|
*
|
|
|
|
context: object = NULL
|
2016-10-28 13:52:37 -03: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 *
|
2018-01-22 20:11:18 -04:00
|
|
|
_asyncio_Future_add_done_callback_impl(FutureObj *self, PyObject *fn,
|
|
|
|
PyObject *context)
|
|
|
|
/*[clinic end generated code: output=7ce635bbc9554c1e input=15ab0693a96e9533]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
2018-01-22 20:11:18 -04:00
|
|
|
if (context == NULL) {
|
2018-09-27 15:55:55 -03:00
|
|
|
context = PyContext_CopyCurrent();
|
2018-01-22 20:11:18 -04:00
|
|
|
if (context == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-27 15:55:55 -03:00
|
|
|
PyObject *res = future_add_done_callback(self, fn, context);
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_DECREF(context);
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-27 15:55:55 -03:00
|
|
|
return future_add_done_callback(self, fn, context);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.remove_done_callback
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
fn: object
|
2016-10-28 13:52:37 -03: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 02:44:47 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-10-28 13:52:37 -03:00
|
|
|
_asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
|
2017-09-03 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=5ab1fb52b24ef31f input=0a43280a149d505b]*/
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
|
|
|
PyObject *newlist;
|
|
|
|
Py_ssize_t len, i, j=0;
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_ssize_t cleared_callback0 = 0;
|
|
|
|
|
|
|
|
ENSURE_FUTURE_ALIVE(self)
|
|
|
|
|
|
|
|
if (self->fut_callback0 != NULL) {
|
|
|
|
int cmp = PyObject_RichCompareBool(fn, self->fut_callback0, Py_EQ);
|
|
|
|
if (cmp == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cmp == 1) {
|
|
|
|
/* callback0 == fn */
|
|
|
|
Py_CLEAR(self->fut_callback0);
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_CLEAR(self->fut_context0);
|
2017-12-17 21:19:47 -04:00
|
|
|
cleared_callback0 = 1;
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
if (self->fut_callbacks == NULL) {
|
2017-12-17 21:19:47 -04:00
|
|
|
return PyLong_FromSsize_t(cleared_callback0);
|
2017-09-03 02:10:14 -03:00
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
len = PyList_GET_SIZE(self->fut_callbacks);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (len == 0) {
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_CLEAR(self->fut_callbacks);
|
|
|
|
return PyLong_FromSsize_t(cleared_callback0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 1) {
|
2018-01-22 20:11:18 -04:00
|
|
|
PyObject *cb_tup = PyList_GET_ITEM(self->fut_callbacks, 0);
|
2017-12-17 21:19:47 -04:00
|
|
|
int cmp = PyObject_RichCompareBool(
|
2018-01-22 20:11:18 -04:00
|
|
|
fn, PyTuple_GET_ITEM(cb_tup, 0), Py_EQ);
|
2017-12-17 21:19:47 -04: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 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
newlist = PyList_New(len);
|
|
|
|
if (newlist == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:46:56 -04:00
|
|
|
for (i = 0; i < PyList_GET_SIZE(self->fut_callbacks); i++) {
|
2016-10-09 02:44:47 -03:00
|
|
|
int ret;
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i);
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_INCREF(item);
|
2018-01-22 20:11:18 -04:00
|
|
|
ret = PyObject_RichCompareBool(fn, PyTuple_GET_ITEM(item, 0), Py_EQ);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (ret == 0) {
|
2017-07-05 14:32:03 -03:00
|
|
|
if (j < len) {
|
|
|
|
PyList_SET_ITEM(newlist, j, item);
|
|
|
|
j++;
|
2017-09-03 02:10:14 -03:00
|
|
|
continue;
|
2017-07-05 14:32:03 -03:00
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
ret = PyList_Append(newlist, item);
|
|
|
|
}
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
if (j == 0) {
|
|
|
|
Py_CLEAR(self->fut_callbacks);
|
|
|
|
Py_DECREF(newlist);
|
|
|
|
return PyLong_FromSsize_t(len + cleared_callback0);
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
if (j < len) {
|
|
|
|
Py_SIZE(newlist) = j;
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
2017-09-03 02: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 02:44:47 -03:00
|
|
|
}
|
|
|
|
Py_DECREF(newlist);
|
2017-12-17 21:19:47 -04:00
|
|
|
return PyLong_FromSsize_t(len - j + cleared_callback0);
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
Py_DECREF(newlist);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.cancel
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
Cancel the future and schedule callbacks.
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03: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 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *
|
|
|
|
_asyncio_Future_cancel_impl(FutureObj *self)
|
|
|
|
/*[clinic end generated code: output=e45b932ba8bd68a1 input=515709a127995109]*/
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(self)
|
2016-10-28 13:52:37 -03:00
|
|
|
return future_cancel(self);
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.cancelled
|
|
|
|
|
|
|
|
Return True if the future was cancelled.
|
|
|
|
[clinic start generated code]*/
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-10-28 13:52:37 -03:00
|
|
|
_asyncio_Future_cancelled_impl(FutureObj *self)
|
|
|
|
/*[clinic end generated code: output=145197ced586357d input=943ab8b7b7b17e45]*/
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (future_is_alive(self) && self->fut_state == STATE_CANCELLED) {
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_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 02:44:47 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-10-28 13:52:37 -03:00
|
|
|
_asyncio_Future_done_impl(FutureObj *self)
|
|
|
|
/*[clinic end generated code: output=244c5ac351145096 input=28d7b23fdb65d2ac]*/
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (!future_is_alive(self) || self->fut_state == STATE_PENDING) {
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:04:15 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future.get_loop
|
|
|
|
|
|
|
|
Return the event loop the Future is bound to.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Future_get_loop_impl(FutureObj *self)
|
|
|
|
/*[clinic end generated code: output=119b6ea0c9816c3f input=cba48c2136c79d1f]*/
|
|
|
|
{
|
|
|
|
Py_INCREF(self->fut_loop);
|
|
|
|
return self->fut_loop;
|
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_blocking(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (future_is_alive(fut) && fut->fut_blocking) {
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
FutureObj_set_blocking(FutureObj *fut, PyObject *val)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (future_ensure_alive(fut)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
int is_true = PyObject_IsTrue(val);
|
|
|
|
if (is_true < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fut->fut_blocking = is_true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_log_traceback(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_log_tb) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:49:18 -03:00
|
|
|
static int
|
|
|
|
FutureObj_set_log_traceback(FutureObj *fut, PyObject *val)
|
|
|
|
{
|
|
|
|
int is_true = PyObject_IsTrue(val);
|
|
|
|
if (is_true < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-12-25 17:16:10 -04:00
|
|
|
if (is_true) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"_log_traceback can only be set to False");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-11 10:49:18 -03:00
|
|
|
fut->fut_log_tb = is_true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_loop(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (!future_is_alive(fut)) {
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
Py_INCREF(fut->fut_loop);
|
|
|
|
return fut->fut_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_callbacks(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
if (fut->fut_callback0 == NULL) {
|
|
|
|
if (fut->fut_callbacks == NULL) {
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(fut->fut_callbacks);
|
|
|
|
return fut->fut_callbacks;
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_ssize_t len = 1;
|
|
|
|
if (fut->fut_callbacks != NULL) {
|
|
|
|
len += PyList_GET_SIZE(fut->fut_callbacks);
|
|
|
|
}
|
2017-12-17 21:19:47 -04:00
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
|
|
|
|
PyObject *new_list = PyList_New(len);
|
2017-12-17 21:19:47 -04:00
|
|
|
if (new_list == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
PyObject *tup0 = PyTuple_New(2);
|
|
|
|
if (tup0 == NULL) {
|
|
|
|
Py_DECREF(new_list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_INCREF(fut->fut_callback0);
|
2018-01-22 20:11:18 -04:00
|
|
|
PyTuple_SET_ITEM(tup0, 0, fut->fut_callback0);
|
|
|
|
assert(fut->fut_context0 != NULL);
|
|
|
|
Py_INCREF(fut->fut_context0);
|
|
|
|
PyTuple_SET_ITEM(tup0, 1, (PyObject *)fut->fut_context0);
|
|
|
|
|
|
|
|
PyList_SET_ITEM(new_list, 0, tup0);
|
|
|
|
|
|
|
|
if (fut->fut_callbacks != NULL) {
|
|
|
|
for (i = 0; i < PyList_GET_SIZE(fut->fut_callbacks); i++) {
|
|
|
|
PyObject *cb = PyList_GET_ITEM(fut->fut_callbacks, i);
|
|
|
|
Py_INCREF(cb);
|
|
|
|
PyList_SET_ITEM(new_list, i + 1, cb);
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
2017-12-17 21:19:47 -04:00
|
|
|
|
|
|
|
return new_list;
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_result(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_result == NULL) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
Py_INCREF(fut->fut_result);
|
|
|
|
return fut->fut_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_exception(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_exception == NULL) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
Py_INCREF(fut->fut_exception);
|
|
|
|
return fut->fut_exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_source_traceback(FutureObj *fut)
|
|
|
|
{
|
2017-12-17 21:19:47 -04:00
|
|
|
if (!future_is_alive(fut) || fut->fut_source_tb == NULL) {
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
Py_INCREF(fut->fut_source_tb);
|
|
|
|
return fut->fut_source_tb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureObj_get_state(FutureObj *fut)
|
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(PENDING);
|
|
|
|
_Py_IDENTIFIER(CANCELLED);
|
|
|
|
_Py_IDENTIFIER(FINISHED);
|
|
|
|
PyObject *ret = NULL;
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
switch (fut->fut_state) {
|
|
|
|
case STATE_PENDING:
|
|
|
|
ret = _PyUnicode_FromId(&PyId_PENDING);
|
|
|
|
break;
|
|
|
|
case STATE_CANCELLED:
|
|
|
|
ret = _PyUnicode_FromId(&PyId_CANCELLED);
|
|
|
|
break;
|
|
|
|
case STATE_FINISHED:
|
|
|
|
ret = _PyUnicode_FromId(&PyId_FINISHED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_XINCREF(ret);
|
2016-10-09 02:44:47 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Future._repr_info
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Future__repr_info_impl(FutureObj *self)
|
|
|
|
/*[clinic end generated code: output=fa69e901bd176cfb input=f21504d8e2ae1ca2]*/
|
|
|
|
{
|
2016-12-04 17:59:09 -04:00
|
|
|
return PyObject_CallFunctionObjArgs(
|
|
|
|
asyncio_future_repr_info_func, self, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
static PyObject *
|
|
|
|
FutureObj_repr(FutureObj *fut)
|
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(_repr_info);
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *rinfo = _PyObject_CallMethodIdObjArgs((PyObject*)fut,
|
|
|
|
&PyId__repr_info,
|
|
|
|
NULL);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (rinfo == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *rinfo_s = PyUnicode_Join(NULL, rinfo);
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_DECREF(rinfo);
|
|
|
|
if (rinfo_s == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
PyObject *rstr = PyUnicode_FromFormat("<%s %U>",
|
|
|
|
_PyType_Name(Py_TYPE(fut)), rinfo_s);
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_DECREF(rinfo_s);
|
|
|
|
return rstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
FutureObj_finalize(FutureObj *fut)
|
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(call_exception_handler);
|
|
|
|
_Py_IDENTIFIER(message);
|
|
|
|
_Py_IDENTIFIER(exception);
|
|
|
|
_Py_IDENTIFIER(future);
|
|
|
|
_Py_IDENTIFIER(source_traceback);
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
PyObject *context;
|
|
|
|
PyObject *message = NULL;
|
|
|
|
PyObject *func;
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
if (!fut->fut_log_tb) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(fut->fut_exception != NULL);
|
2017-09-03 02:10:14 -03:00
|
|
|
fut->fut_log_tb = 0;
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
/* Save the current exception, if any. */
|
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
|
|
|
|
|
|
|
context = PyDict_New();
|
|
|
|
if (context == NULL) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = PyUnicode_FromFormat(
|
2018-09-21 03:11:32 -03:00
|
|
|
"%s exception was never retrieved", _PyType_Name(Py_TYPE(fut)));
|
2016-10-09 02:44:47 -03:00
|
|
|
if (message == NULL) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyDict_SetItemId(context, &PyId_message, message) < 0 ||
|
|
|
|
_PyDict_SetItemId(context, &PyId_exception, fut->fut_exception) < 0 ||
|
|
|
|
_PyDict_SetItemId(context, &PyId_future, (PyObject*)fut) < 0) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
if (fut->fut_source_tb != NULL) {
|
|
|
|
if (_PyDict_SetItemId(context, &PyId_source_traceback,
|
|
|
|
fut->fut_source_tb) < 0) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func = _PyObject_GetAttrId(fut->fut_loop, &PyId_call_exception_handler);
|
|
|
|
if (func != NULL) {
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *res = PyObject_CallFunctionObjArgs(func, context, NULL);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
PyErr_WriteUnraisable(func);
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
else {
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
Py_DECREF(func);
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
finally:
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_XDECREF(context);
|
|
|
|
Py_XDECREF(message);
|
2016-10-09 02:44:47 -03:00
|
|
|
|
|
|
|
/* Restore the saved exception. */
|
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyAsyncMethods FutureType_as_async = {
|
2016-10-28 13:52:37 -03:00
|
|
|
(unaryfunc)future_new_iter, /* am_await */
|
2016-10-09 02:44:47 -03:00
|
|
|
0, /* am_aiter */
|
|
|
|
0 /* am_anext */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMethodDef FutureType_methods[] = {
|
2016-10-28 13:52:37 -03: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 16:04:15 -04:00
|
|
|
_ASYNCIO_FUTURE_GET_LOOP_METHODDEF
|
2016-10-28 13:52:37 -03:00
|
|
|
_ASYNCIO_FUTURE__REPR_INFO_METHODDEF
|
2016-10-09 02:44:47 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
#define FUTURE_COMMON_GETSETLIST \
|
|
|
|
{"_state", (getter)FutureObj_get_state, NULL, NULL}, \
|
|
|
|
{"_asyncio_future_blocking", (getter)FutureObj_get_blocking, \
|
|
|
|
(setter)FutureObj_set_blocking, NULL}, \
|
|
|
|
{"_loop", (getter)FutureObj_get_loop, NULL, NULL}, \
|
|
|
|
{"_callbacks", (getter)FutureObj_get_callbacks, NULL, NULL}, \
|
|
|
|
{"_result", (getter)FutureObj_get_result, NULL, NULL}, \
|
|
|
|
{"_exception", (getter)FutureObj_get_exception, NULL, NULL}, \
|
2017-06-11 10:49:18 -03:00
|
|
|
{"_log_traceback", (getter)FutureObj_get_log_traceback, \
|
|
|
|
(setter)FutureObj_set_log_traceback, NULL}, \
|
2016-10-09 02:44:47 -03:00
|
|
|
{"_source_traceback", (getter)FutureObj_get_source_traceback, NULL, NULL},
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
static PyGetSetDef FutureType_getsetlist[] = {
|
|
|
|
FUTURE_COMMON_GETSETLIST
|
2016-10-09 02:44:47 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void FutureObj_dealloc(PyObject *self);
|
|
|
|
|
|
|
|
static PyTypeObject FutureType = {
|
2016-10-28 14:13:52 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2016-10-15 03:39:19 -03:00
|
|
|
"_asyncio.Future",
|
2016-10-09 02:44:47 -03:00
|
|
|
sizeof(FutureObj), /* tp_basicsize */
|
|
|
|
.tp_dealloc = FutureObj_dealloc,
|
|
|
|
.tp_as_async = &FutureType_as_async,
|
|
|
|
.tp_repr = (reprfunc)FutureObj_repr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE
|
|
|
|
| Py_TPFLAGS_HAVE_FINALIZE,
|
2016-10-28 13:52:37 -03:00
|
|
|
.tp_doc = _asyncio_Future___init____doc__,
|
2016-10-09 02:44:47 -03:00
|
|
|
.tp_traverse = (traverseproc)FutureObj_traverse,
|
|
|
|
.tp_clear = (inquiry)FutureObj_clear,
|
|
|
|
.tp_weaklistoffset = offsetof(FutureObj, fut_weakreflist),
|
2016-10-28 13:52:37 -03:00
|
|
|
.tp_iter = (getiterfunc)future_new_iter,
|
2016-10-09 02:44:47 -03:00
|
|
|
.tp_methods = FutureType_methods,
|
|
|
|
.tp_getset = FutureType_getsetlist,
|
|
|
|
.tp_dictoffset = offsetof(FutureObj, dict),
|
2016-10-28 13:52:37 -03:00
|
|
|
.tp_init = (initproc)_asyncio_Future___init__,
|
2016-10-09 02:44:47 -03:00
|
|
|
.tp_new = PyType_GenericNew,
|
|
|
|
.tp_finalize = (destructor)FutureObj_finalize,
|
|
|
|
};
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static void
|
|
|
|
FutureObj_dealloc(PyObject *self)
|
|
|
|
{
|
|
|
|
FutureObj *fut = (FutureObj *)self;
|
|
|
|
|
|
|
|
if (Future_CheckExact(fut)) {
|
|
|
|
/* When fut is subclass of Future, finalizer is called from
|
2016-10-09 02:44:47 -03:00
|
|
|
* subtype_dealloc.
|
|
|
|
*/
|
|
|
|
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
|
|
|
|
// resurrected.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 03:31:07 -03:00
|
|
|
PyObject_GC_UnTrack(self);
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
if (fut->fut_weakreflist != NULL) {
|
|
|
|
PyObject_ClearWeakRefs(self);
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
(void)FutureObj_clear(fut);
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_TYPE(fut)->tp_free(fut);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************** Future Iterator **************************/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
FutureObj *future;
|
|
|
|
} futureiterobject;
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
|
|
|
|
#define FI_FREELIST_MAXLEN 255
|
|
|
|
static futureiterobject *fi_freelist = NULL;
|
|
|
|
static Py_ssize_t fi_freelist_len = 0;
|
|
|
|
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
static void
|
|
|
|
FutureIter_dealloc(futureiterobject *it)
|
|
|
|
{
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject_GC_UnTrack(it);
|
2017-12-17 21:19:47 -04:00
|
|
|
Py_CLEAR(it->future);
|
|
|
|
|
|
|
|
if (fi_freelist_len < FI_FREELIST_MAXLEN) {
|
|
|
|
fi_freelist_len++;
|
|
|
|
it->future = (FutureObj*) fi_freelist;
|
|
|
|
fi_freelist = it;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyObject_GC_Del(it);
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureIter_iternext(futureiterobject *it)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
FutureObj *fut = it->future;
|
|
|
|
|
|
|
|
if (fut == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fut->fut_state == STATE_PENDING) {
|
|
|
|
if (!fut->fut_blocking) {
|
|
|
|
fut->fut_blocking = 1;
|
|
|
|
Py_INCREF(fut);
|
|
|
|
return (PyObject *)fut;
|
|
|
|
}
|
2017-12-25 11:48:15 -04:00
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"await wasn't used with future");
|
2016-10-09 02:44:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
it->future = NULL;
|
2016-10-28 13:52:37 -03:00
|
|
|
res = _asyncio_Future_result_impl(fut);
|
2016-10-09 02:44:47 -03:00
|
|
|
if (res != NULL) {
|
2016-11-06 12:47:03 -04:00
|
|
|
/* The result of the Future is not an exception. */
|
2017-09-03 02:10:14 -03:00
|
|
|
(void)_PyGen_SetStopIterationValue(res);
|
2016-11-06 12:47:03 -04:00
|
|
|
Py_DECREF(res);
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(fut);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2016-10-25 07:00:45 -03:00
|
|
|
FutureIter_send(futureiterobject *self, PyObject *unused)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2016-10-25 07:00:45 -03:00
|
|
|
/* Future.__iter__ doesn't care about values that are pushed to the
|
2018-09-21 03:11:32 -03:00
|
|
|
* generator, it just returns self.result().
|
2016-10-25 07:00:45 -03:00
|
|
|
*/
|
2016-10-09 02:44:47 -03:00
|
|
|
return FutureIter_iternext(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureIter_throw(futureiterobject *self, PyObject *args)
|
|
|
|
{
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *type, *val = NULL, *tb = NULL;
|
2016-10-09 02:44:47 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|OO", &type, &val, &tb))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (val == Py_None) {
|
|
|
|
val = NULL;
|
|
|
|
}
|
|
|
|
if (tb == Py_None) {
|
|
|
|
tb = NULL;
|
2016-11-14 04:15:44 -04:00
|
|
|
} else if (tb != NULL && !PyTraceBack_Check(tb)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "throw() third argument must be a traceback");
|
|
|
|
return NULL;
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
2016-11-14 04:15:44 -04:00
|
|
|
Py_INCREF(type);
|
|
|
|
Py_XINCREF(val);
|
|
|
|
Py_XINCREF(tb);
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-11-14 04:15:44 -04:00
|
|
|
if (PyExceptionClass_Check(type)) {
|
|
|
|
PyErr_NormalizeException(&type, &val, &tb);
|
2016-12-01 12:37:47 -04:00
|
|
|
/* No need to call PyException_SetTraceback since we'll be calling
|
|
|
|
PyErr_Restore for `type`, `val`, and `tb`. */
|
2016-11-14 04:15:44 -04: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 02:44:47 -03:00
|
|
|
}
|
2016-11-14 04:15:44 -04: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 02:44:47 -03:00
|
|
|
}
|
2016-11-14 04:15:44 -04:00
|
|
|
|
|
|
|
Py_CLEAR(self->future);
|
|
|
|
|
|
|
|
PyErr_Restore(type, val, tb);
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
return NULL;
|
2016-11-14 04:15:44 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
Py_DECREF(type);
|
|
|
|
Py_XDECREF(val);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
return NULL;
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
FutureIter_close(futureiterobject *self, PyObject *arg)
|
|
|
|
{
|
|
|
|
Py_CLEAR(self->future);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
FutureIter_traverse(futureiterobject *it, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(it->future);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef FutureIter_methods[] = {
|
|
|
|
{"send", (PyCFunction)FutureIter_send, METH_O, NULL},
|
|
|
|
{"throw", (PyCFunction)FutureIter_throw, METH_VARARGS, NULL},
|
|
|
|
{"close", (PyCFunction)FutureIter_close, METH_NOARGS, NULL},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject FutureIterType = {
|
2016-10-28 14:13:52 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2016-10-15 03:39:19 -03:00
|
|
|
"_asyncio.FutureIter",
|
2016-10-28 13:52:37 -03:00
|
|
|
.tp_basicsize = sizeof(futureiterobject),
|
|
|
|
.tp_itemsize = 0,
|
|
|
|
.tp_dealloc = (destructor)FutureIter_dealloc,
|
|
|
|
.tp_getattro = PyObject_GenericGetAttr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
|
|
|
.tp_traverse = (traverseproc)FutureIter_traverse,
|
|
|
|
.tp_iter = PyObject_SelfIter,
|
|
|
|
.tp_iternext = (iternextfunc)FutureIter_iternext,
|
|
|
|
.tp_methods = FutureIter_methods,
|
2016-10-09 02:44:47 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
2016-10-28 13:52:37 -03:00
|
|
|
future_new_iter(PyObject *fut)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
|
|
|
futureiterobject *it;
|
|
|
|
|
|
|
|
if (!PyObject_TypeCheck(fut, &FutureType)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-17 21:19:47 -04:00
|
|
|
|
|
|
|
ENSURE_FUTURE_ALIVE(fut)
|
|
|
|
|
|
|
|
if (fi_freelist_len) {
|
|
|
|
fi_freelist_len--;
|
|
|
|
it = fi_freelist;
|
|
|
|
fi_freelist = (futureiterobject*) it->future;
|
|
|
|
it->future = NULL;
|
|
|
|
_Py_NewReference((PyObject*) it);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
it = PyObject_GC_New(futureiterobject, &FutureIterType);
|
|
|
|
if (it == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
2017-12-17 21:19:47 -04:00
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
Py_INCREF(fut);
|
|
|
|
it->future = (FutureObj*)fut;
|
2016-10-10 14:12:34 -03:00
|
|
|
PyObject_GC_Track(it);
|
2016-10-09 02:44:47 -03:00
|
|
|
return (PyObject*)it;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
/*********************** Task **************************/
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
class _asyncio.Task "TaskObj *" "&Task_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=719dcef0fcc03b37]*/
|
|
|
|
|
|
|
|
static int task_call_step_soon(TaskObj *, PyObject *);
|
|
|
|
static PyObject * task_wakeup(TaskObj *, PyObject *);
|
|
|
|
static PyObject * task_step(TaskObj *, PyObject *);
|
|
|
|
|
|
|
|
/* ----- Task._step wrapper */
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-17 23:48:14 -03:00
|
|
|
static int
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper_clear(TaskStepMethWrapper *o)
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(o->sw_task);
|
|
|
|
Py_CLEAR(o->sw_arg);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static void
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper_dealloc(TaskStepMethWrapper *o)
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
|
|
|
PyObject_GC_UnTrack(o);
|
2017-09-03 02:10:14 -03:00
|
|
|
(void)TaskStepMethWrapper_clear(o);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_TYPE(o)->tp_free(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper_call(TaskStepMethWrapper *o,
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2017-09-03 02: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;
|
|
|
|
}
|
2018-01-24 12:31:01 -04:00
|
|
|
return task_step(o->sw_task, o->sw_arg);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper_traverse(TaskStepMethWrapper *o,
|
2016-10-28 13:52:37 -03:00
|
|
|
visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(o->sw_task);
|
|
|
|
Py_VISIT(o->sw_arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper_get___self__(TaskStepMethWrapper *o)
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
|
|
|
if (o->sw_task) {
|
|
|
|
Py_INCREF(o->sw_task);
|
|
|
|
return (PyObject*)o->sw_task;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
static PyGetSetDef TaskStepMethWrapper_getsetlist[] = {
|
|
|
|
{"__self__", (getter)TaskStepMethWrapper_get___self__, NULL, NULL},
|
2016-10-28 13:52:37 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2018-07-06 02:39:34 -03:00
|
|
|
static PyTypeObject TaskStepMethWrapper_Type = {
|
2016-10-28 14:13:52 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2017-09-03 02:10:14 -03:00
|
|
|
"TaskStepMethWrapper",
|
|
|
|
.tp_basicsize = sizeof(TaskStepMethWrapper),
|
2016-10-28 13:52:37 -03:00
|
|
|
.tp_itemsize = 0,
|
2017-09-03 02:10:14 -03:00
|
|
|
.tp_getset = TaskStepMethWrapper_getsetlist,
|
|
|
|
.tp_dealloc = (destructor)TaskStepMethWrapper_dealloc,
|
|
|
|
.tp_call = (ternaryfunc)TaskStepMethWrapper_call,
|
2016-10-28 13:52:37 -03:00
|
|
|
.tp_getattro = PyObject_GenericGetAttr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
2017-09-03 02:10:14 -03:00
|
|
|
.tp_traverse = (traverseproc)TaskStepMethWrapper_traverse,
|
|
|
|
.tp_clear = (inquiry)TaskStepMethWrapper_clear,
|
2016-10-28 13:52:37 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper_new(TaskObj *task, PyObject *arg)
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
2017-09-03 02:10:14 -03:00
|
|
|
TaskStepMethWrapper *o;
|
|
|
|
o = PyObject_GC_New(TaskStepMethWrapper, &TaskStepMethWrapper_Type);
|
2016-10-28 13:52:37 -03:00
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_INCREF(task);
|
|
|
|
o->sw_task = task;
|
|
|
|
|
|
|
|
Py_XINCREF(arg);
|
|
|
|
o->sw_arg = arg;
|
|
|
|
|
|
|
|
PyObject_GC_Track(o);
|
|
|
|
return (PyObject*) o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----- Task._wakeup wrapper */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
TaskWakeupMethWrapper_call(TaskWakeupMethWrapper *o,
|
|
|
|
PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyObject *fut;
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "function takes no keyword arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!PyArg_ParseTuple(args, "O", &fut)) {
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-01-24 12:31:01 -04:00
|
|
|
return task_wakeup(o->ww_task, fut);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
TaskWakeupMethWrapper_clear(TaskWakeupMethWrapper *o)
|
|
|
|
{
|
|
|
|
Py_CLEAR(o->ww_task);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
TaskWakeupMethWrapper_traverse(TaskWakeupMethWrapper *o,
|
|
|
|
visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(o->ww_task);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TaskWakeupMethWrapper_dealloc(TaskWakeupMethWrapper *o)
|
|
|
|
{
|
|
|
|
PyObject_GC_UnTrack(o);
|
|
|
|
(void)TaskWakeupMethWrapper_clear(o);
|
|
|
|
Py_TYPE(o)->tp_free(o);
|
|
|
|
}
|
|
|
|
|
2018-07-06 02:39:34 -03:00
|
|
|
static PyTypeObject TaskWakeupMethWrapper_Type = {
|
2016-10-28 14:13:52 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2016-10-28 13:52:37 -03:00
|
|
|
"TaskWakeupMethWrapper",
|
|
|
|
.tp_basicsize = sizeof(TaskWakeupMethWrapper),
|
|
|
|
.tp_itemsize = 0,
|
|
|
|
.tp_dealloc = (destructor)TaskWakeupMethWrapper_dealloc,
|
|
|
|
.tp_call = (ternaryfunc)TaskWakeupMethWrapper_call,
|
|
|
|
.tp_getattro = PyObject_GenericGetAttr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
|
|
|
.tp_traverse = (traverseproc)TaskWakeupMethWrapper_traverse,
|
|
|
|
.tp_clear = (inquiry)TaskWakeupMethWrapper_clear,
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
TaskWakeupMethWrapper_new(TaskObj *task)
|
|
|
|
{
|
|
|
|
TaskWakeupMethWrapper *o;
|
|
|
|
o = PyObject_GC_New(TaskWakeupMethWrapper, &TaskWakeupMethWrapper_Type);
|
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_INCREF(task);
|
|
|
|
o->ww_task = task;
|
|
|
|
|
|
|
|
PyObject_GC_Track(o);
|
|
|
|
return (PyObject*) o;
|
|
|
|
}
|
|
|
|
|
2017-12-16 15:58:38 -04:00
|
|
|
/* ----- Task introspection helpers */
|
|
|
|
|
|
|
|
static int
|
2017-12-23 16:04:15 -04:00
|
|
|
register_task(PyObject *task)
|
2017-12-16 15:58:38 -04:00
|
|
|
{
|
2017-12-23 16:04:15 -04:00
|
|
|
_Py_IDENTIFIER(add);
|
|
|
|
|
|
|
|
PyObject *res = _PyObject_CallMethodIdObjArgs(
|
|
|
|
all_tasks, &PyId_add, task, NULL);
|
|
|
|
if (res == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 0;
|
2017-12-16 15:58:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2017-12-23 16:04:15 -04:00
|
|
|
unregister_task(PyObject *task)
|
2017-12-16 15:58:38 -04:00
|
|
|
{
|
2017-12-23 16:04:15 -04:00
|
|
|
_Py_IDENTIFIER(discard);
|
2017-12-16 15:58:38 -04:00
|
|
|
|
2017-12-23 16:04:15 -04:00
|
|
|
PyObject *res = _PyObject_CallMethodIdObjArgs(
|
|
|
|
all_tasks, &PyId_discard, task, NULL);
|
2017-12-16 15:58:38 -04:00
|
|
|
if (res == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
enter_task(PyObject *loop, PyObject *task)
|
|
|
|
{
|
|
|
|
PyObject *item;
|
|
|
|
Py_hash_t hash;
|
|
|
|
hash = PyObject_Hash(loop);
|
|
|
|
if (hash == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
item = _PyDict_GetItem_KnownHash(current_tasks, loop, hash);
|
|
|
|
if (item != NULL) {
|
2018-05-20 10:30:31 -03:00
|
|
|
Py_INCREF(item);
|
2017-12-16 15:58:38 -04:00
|
|
|
PyErr_Format(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"Cannot enter into task %R while another " \
|
|
|
|
"task %R is being executed.",
|
|
|
|
task, item, NULL);
|
2018-05-20 10:30:31 -03:00
|
|
|
Py_DECREF(item);
|
2017-12-16 15:58:38 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2018-05-20 10:30:31 -03:00
|
|
|
if (PyErr_Occurred()) {
|
2017-12-16 15:58:38 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2018-05-20 10:30:31 -03:00
|
|
|
return _PyDict_SetItem_KnownHash(current_tasks, loop, task, hash);
|
2017-12-16 15:58:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
leave_task(PyObject *loop, PyObject *task)
|
|
|
|
/*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/
|
|
|
|
{
|
|
|
|
PyObject *item;
|
|
|
|
Py_hash_t hash;
|
|
|
|
hash = PyObject_Hash(loop);
|
|
|
|
if (hash == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
item = _PyDict_GetItem_KnownHash(current_tasks, loop, hash);
|
|
|
|
if (item != task) {
|
|
|
|
if (item == NULL) {
|
|
|
|
/* Not entered, replace with None */
|
|
|
|
item = Py_None;
|
|
|
|
}
|
|
|
|
PyErr_Format(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"Leaving task %R does not match the current task %R.",
|
|
|
|
task, item, NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return _PyDict_DelItem_KnownHash(current_tasks, loop, hash);
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/* ----- Task */
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.__init__
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
coro: object
|
2016-10-28 13:52:37 -03:00
|
|
|
*
|
2017-09-03 02:10:14 -03:00
|
|
|
loop: object = None
|
2018-08-08 18:06:47 -03:00
|
|
|
name: object = None
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
A coroutine wrapped in a Future.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static int
|
2018-08-08 18:06:47 -03:00
|
|
|
_asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
|
|
|
|
PyObject *name)
|
|
|
|
/*[clinic end generated code: output=88b12b83d570df50 input=352a3137fe60091d]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
|
|
|
if (future_init((FutureObj*)self, loop)) {
|
|
|
|
return -1;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2017-12-19 08:18:45 -04:00
|
|
|
int is_coro = is_coroutine(coro);
|
|
|
|
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 01:04:38 -04:00
|
|
|
}
|
|
|
|
|
2018-02-13 06:28:33 -04:00
|
|
|
Py_XSETREF(self->task_context, PyContext_CopyCurrent());
|
2018-01-22 20:11:18 -04:00
|
|
|
if (self->task_context == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-13 06:28:33 -04:00
|
|
|
Py_CLEAR(self->task_fut_waiter);
|
2016-10-28 13:52:37 -03:00
|
|
|
self->task_must_cancel = 0;
|
|
|
|
self->task_log_destroy_pending = 1;
|
|
|
|
Py_INCREF(coro);
|
2018-02-13 06:28:33 -04:00
|
|
|
Py_XSETREF(self->task_coro, coro);
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-08-08 18:06:47 -03:00
|
|
|
if (name == Py_None) {
|
|
|
|
name = PyUnicode_FromFormat("Task-%" PRIu64, ++task_name_counter);
|
2018-08-09 17:49:49 -03:00
|
|
|
} else if (!PyUnicode_CheckExact(name)) {
|
2018-08-08 18: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;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
if (task_call_step_soon(self, NULL)) {
|
|
|
|
return -1;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2017-12-23 16:04:15 -04:00
|
|
|
return register_task((PyObject*)self);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
TaskObj_clear(TaskObj *task)
|
|
|
|
{
|
|
|
|
(void)FutureObj_clear((FutureObj*) task);
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_CLEAR(task->task_context);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(task->task_coro);
|
2018-08-08 18:06:47 -03:00
|
|
|
Py_CLEAR(task->task_name);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(task->task_fut_waiter);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
TaskObj_traverse(TaskObj *task, visitproc visit, void *arg)
|
|
|
|
{
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_VISIT(task->task_context);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_VISIT(task->task_coro);
|
2018-08-08 18:06:47 -03:00
|
|
|
Py_VISIT(task->task_name);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_VISIT(task->task_fut_waiter);
|
|
|
|
(void)FutureObj_traverse((FutureObj*) task, visit, arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
TaskObj_get_log_destroy_pending(TaskObj *task)
|
|
|
|
{
|
|
|
|
if (task->task_log_destroy_pending) {
|
|
|
|
Py_RETURN_TRUE;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
else {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static int
|
|
|
|
TaskObj_set_log_destroy_pending(TaskObj *task, PyObject *val)
|
|
|
|
{
|
|
|
|
int is_true = PyObject_IsTrue(val);
|
|
|
|
if (is_true < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
task->task_log_destroy_pending = is_true;
|
2016-10-17 23:48:14 -03:00
|
|
|
return 0;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *
|
|
|
|
TaskObj_get_must_cancel(TaskObj *task)
|
|
|
|
{
|
|
|
|
if (task->task_must_cancel) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *
|
|
|
|
TaskObj_get_coro(TaskObj *task)
|
|
|
|
{
|
|
|
|
if (task->task_coro) {
|
|
|
|
Py_INCREF(task->task_coro);
|
|
|
|
return task->task_coro;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static PyObject *
|
|
|
|
TaskObj_get_fut_waiter(TaskObj *task)
|
|
|
|
{
|
|
|
|
if (task->task_fut_waiter) {
|
|
|
|
Py_INCREF(task->task_fut_waiter);
|
|
|
|
return task->task_fut_waiter;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2016-10-09 02:44:47 -03:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
_asyncio.Task.current_task
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
loop: object = None
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
Return the currently running task in an event loop or None.
|
|
|
|
|
|
|
|
By default the current task for the current event loop is returned.
|
|
|
|
|
|
|
|
None is returned when called not in the context of a Task.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task_current_task_impl(PyTypeObject *type, PyObject *loop)
|
2017-09-03 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=99fbe7332c516e03 input=cd14770c5b79c7eb]*/
|
2016-10-09 02:44:47 -03:00
|
|
|
{
|
2017-12-16 15:58:38 -04:00
|
|
|
PyObject *ret;
|
|
|
|
PyObject *current_task_func;
|
|
|
|
|
|
|
|
if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
|
|
|
|
"Task.current_task() is deprecated, " \
|
|
|
|
"use asyncio.current_task() instead",
|
|
|
|
1) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_task_func = _PyObject_GetAttrId(asyncio_mod, &PyId_current_task);
|
|
|
|
if (current_task_func == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2017-03-02 23:16:33 -04:00
|
|
|
if (loop == Py_None) {
|
2017-12-13 15:49:42 -04:00
|
|
|
loop = get_event_loop();
|
2016-10-28 13:52:37 -03:00
|
|
|
if (loop == NULL) {
|
2018-05-20 10:30:31 -03:00
|
|
|
Py_DECREF(current_task_func);
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-16 15:58:38 -04:00
|
|
|
ret = PyObject_CallFunctionObjArgs(current_task_func, loop, NULL);
|
|
|
|
Py_DECREF(current_task_func);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_DECREF(loop);
|
2017-12-16 15:58:38 -04:00
|
|
|
return ret;
|
2016-10-17 23:48:14 -03:00
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
else {
|
2017-12-16 15:58:38 -04:00
|
|
|
ret = PyObject_CallFunctionObjArgs(current_task_func, loop, NULL);
|
|
|
|
Py_DECREF(current_task_func);
|
|
|
|
return ret;
|
2017-09-03 02:10:14 -03:00
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
_asyncio.Task.all_tasks
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
loop: object = None
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
Return a set of all tasks for an event loop.
|
|
|
|
|
|
|
|
By default all tasks for the current event loop are returned.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task_all_tasks_impl(PyTypeObject *type, PyObject *loop)
|
2017-09-03 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=11f9b20749ccca5d input=497f80bc9ce726b5]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
|
|
|
PyObject *res;
|
2017-12-16 15:58:38 -04:00
|
|
|
PyObject *all_tasks_func;
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2017-12-16 15:58:38 -04:00
|
|
|
if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
|
|
|
|
"Task.all_tasks() is deprecated, " \
|
|
|
|
"use asyncio.all_tasks() instead",
|
|
|
|
1) < 0) {
|
|
|
|
return NULL;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
2018-05-28 18:54:02 -03:00
|
|
|
all_tasks_func = _PyObject_GetAttrId(asyncio_mod, &PyId__all_tasks_compat);
|
2018-05-20 10:30:31 -03:00
|
|
|
if (all_tasks_func == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-16 15:58:38 -04:00
|
|
|
res = PyObject_CallFunctionObjArgs(all_tasks_func, loop, NULL);
|
|
|
|
Py_DECREF(all_tasks_func);
|
2016-10-28 13:52:37 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task._repr_info
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task__repr_info_impl(TaskObj *self)
|
|
|
|
/*[clinic end generated code: output=6a490eb66d5ba34b input=3c6d051ed3ddec8b]*/
|
|
|
|
{
|
2016-12-04 17:59:09 -04:00
|
|
|
return PyObject_CallFunctionObjArgs(
|
|
|
|
asyncio_task_repr_info_func, self, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.cancel
|
|
|
|
|
|
|
|
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).
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task_cancel_impl(TaskObj *self)
|
|
|
|
/*[clinic end generated code: output=6bfc0479da9d5757 input=13f9bf496695cb52]*/
|
|
|
|
{
|
2017-06-11 10:49:18 -03:00
|
|
|
self->task_log_tb = 0;
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
if (self->task_state != STATE_PENDING) {
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->task_fut_waiter) {
|
|
|
|
PyObject *res;
|
|
|
|
int is_true;
|
|
|
|
|
|
|
|
res = _PyObject_CallMethodId(
|
|
|
|
self->task_fut_waiter, &PyId_cancel, NULL);
|
|
|
|
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;
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.get_stack
|
|
|
|
|
|
|
|
*
|
2017-09-03 02:10:14 -03:00
|
|
|
limit: object = None
|
2016-10-28 13:52:37 -03: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 *
|
|
|
|
_asyncio_Task_get_stack_impl(TaskObj *self, PyObject *limit)
|
2017-09-03 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=c9aeeeebd1e18118 input=05b323d42b809b90]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
|
|
|
return PyObject_CallFunctionObjArgs(
|
|
|
|
asyncio_task_get_stack_func, self, limit, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.print_stack
|
|
|
|
|
|
|
|
*
|
2017-09-03 02:10:14 -03:00
|
|
|
limit: object = None
|
|
|
|
file: object = None
|
2016-10-28 13:52:37 -03: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 *
|
|
|
|
_asyncio_Task_print_stack_impl(TaskObj *self, PyObject *limit,
|
|
|
|
PyObject *file)
|
2017-09-03 02:10:14 -03:00
|
|
|
/*[clinic end generated code: output=7339e10314cd3f4d input=1a0352913b7fcd92]*/
|
2016-10-28 13:52:37 -03:00
|
|
|
{
|
|
|
|
return PyObject_CallFunctionObjArgs(
|
|
|
|
asyncio_task_print_stack_func, self, limit, file, NULL);
|
|
|
|
}
|
|
|
|
|
2017-12-25 11:48:15 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.set_result
|
|
|
|
|
|
|
|
result: object
|
|
|
|
/
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task_set_result(TaskObj *self, PyObject *result)
|
|
|
|
/*[clinic end generated code: output=1dcae308bfcba318 input=9d1a00c07be41bab]*/
|
|
|
|
{
|
|
|
|
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 *
|
|
|
|
_asyncio_Task_set_exception(TaskObj *self, PyObject *exception)
|
|
|
|
/*[clinic end generated code: output=bc377fc28067303d input=9a8f65c83dcf893a]*/
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
2017-12-30 09:40:27 -04:00
|
|
|
"Task does not support set_exception operation");
|
2017-12-25 11:48:15 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:06:47 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.get_name
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task_get_name_impl(TaskObj *self)
|
|
|
|
/*[clinic end generated code: output=0ecf1570c3b37a8f input=a4a6595d12f4f0f8]*/
|
|
|
|
{
|
|
|
|
if (self->task_name) {
|
|
|
|
Py_INCREF(self->task_name);
|
|
|
|
return self->task_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio.Task.set_name
|
|
|
|
|
|
|
|
value: object
|
|
|
|
/
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_asyncio_Task_set_name(TaskObj *self, PyObject *value)
|
|
|
|
/*[clinic end generated code: output=138a8d51e32057d6 input=a8359b6e65f8fd31]*/
|
|
|
|
{
|
2018-08-09 17:49:49 -03:00
|
|
|
if (!PyUnicode_CheckExact(value)) {
|
|
|
|
value = PyObject_Str(value);
|
|
|
|
if (value == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Py_INCREF(value);
|
2018-08-08 18:06:47 -03:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:49:49 -03:00
|
|
|
Py_XSETREF(self->task_name, value);
|
2018-08-08 18:06:47 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2017-12-25 11:48:15 -04:00
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static void
|
|
|
|
TaskObj_finalize(TaskObj *task)
|
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(call_exception_handler);
|
|
|
|
_Py_IDENTIFIER(task);
|
|
|
|
_Py_IDENTIFIER(message);
|
|
|
|
_Py_IDENTIFIER(source_traceback);
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *context;
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *message = NULL;
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *func;
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
|
|
|
|
if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the current exception, if any. */
|
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
|
|
|
|
|
|
|
context = PyDict_New();
|
|
|
|
if (context == NULL) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = PyUnicode_FromString("Task was destroyed but it is pending!");
|
|
|
|
if (message == NULL) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyDict_SetItemId(context, &PyId_message, message) < 0 ||
|
|
|
|
_PyDict_SetItemId(context, &PyId_task, (PyObject*)task) < 0)
|
|
|
|
{
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task->task_source_tb != NULL) {
|
|
|
|
if (_PyDict_SetItemId(context, &PyId_source_traceback,
|
|
|
|
task->task_source_tb) < 0)
|
|
|
|
{
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func = _PyObject_GetAttrId(task->task_loop, &PyId_call_exception_handler);
|
|
|
|
if (func != NULL) {
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *res = PyObject_CallFunctionObjArgs(func, context, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
PyErr_WriteUnraisable(func);
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
else {
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
Py_DECREF(func);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
finally:
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_XDECREF(context);
|
|
|
|
Py_XDECREF(message);
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
/* Restore the saved exception. */
|
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
|
|
|
|
|
|
|
done:
|
|
|
|
FutureObj_finalize((FutureObj*)task);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:48:15 -04:00
|
|
|
_ASYNCIO_TASK_SET_RESULT_METHODDEF
|
|
|
|
_ASYNCIO_TASK_SET_EXCEPTION_METHODDEF
|
2016-10-28 13:52:37 -03:00
|
|
|
_ASYNCIO_TASK_CURRENT_TASK_METHODDEF
|
|
|
|
_ASYNCIO_TASK_ALL_TASKS_METHODDEF
|
|
|
|
_ASYNCIO_TASK_CANCEL_METHODDEF
|
|
|
|
_ASYNCIO_TASK_GET_STACK_METHODDEF
|
|
|
|
_ASYNCIO_TASK_PRINT_STACK_METHODDEF
|
|
|
|
_ASYNCIO_TASK__REPR_INFO_METHODDEF
|
2018-08-08 18:06:47 -03:00
|
|
|
_ASYNCIO_TASK_GET_NAME_METHODDEF
|
|
|
|
_ASYNCIO_TASK_SET_NAME_METHODDEF
|
2016-10-28 13:52:37 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyGetSetDef TaskType_getsetlist[] = {
|
|
|
|
FUTURE_COMMON_GETSETLIST
|
|
|
|
{"_log_destroy_pending", (getter)TaskObj_get_log_destroy_pending,
|
|
|
|
(setter)TaskObj_set_log_destroy_pending, NULL},
|
|
|
|
{"_must_cancel", (getter)TaskObj_get_must_cancel, NULL, NULL},
|
|
|
|
{"_coro", (getter)TaskObj_get_coro, NULL, NULL},
|
|
|
|
{"_fut_waiter", (getter)TaskObj_get_fut_waiter, NULL, NULL},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject TaskType = {
|
2016-10-28 14:13:52 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2016-10-28 13:52:37 -03:00
|
|
|
"_asyncio.Task",
|
|
|
|
sizeof(TaskObj), /* tp_basicsize */
|
|
|
|
.tp_base = &FutureType,
|
|
|
|
.tp_dealloc = TaskObj_dealloc,
|
|
|
|
.tp_as_async = &FutureType_as_async,
|
|
|
|
.tp_repr = (reprfunc)FutureObj_repr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE
|
|
|
|
| Py_TPFLAGS_HAVE_FINALIZE,
|
|
|
|
.tp_doc = _asyncio_Task___init____doc__,
|
|
|
|
.tp_traverse = (traverseproc)TaskObj_traverse,
|
|
|
|
.tp_clear = (inquiry)TaskObj_clear,
|
|
|
|
.tp_weaklistoffset = offsetof(TaskObj, task_weakreflist),
|
|
|
|
.tp_iter = (getiterfunc)future_new_iter,
|
|
|
|
.tp_methods = TaskType_methods,
|
|
|
|
.tp_getset = TaskType_getsetlist,
|
|
|
|
.tp_dictoffset = offsetof(TaskObj, dict),
|
|
|
|
.tp_init = (initproc)_asyncio_Task___init__,
|
|
|
|
.tp_new = PyType_GenericNew,
|
|
|
|
.tp_finalize = (destructor)TaskObj_finalize,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
TaskObj_dealloc(PyObject *self)
|
|
|
|
{
|
|
|
|
TaskObj *task = (TaskObj *)self;
|
|
|
|
|
|
|
|
if (Task_CheckExact(self)) {
|
|
|
|
/* When fut is subclass of Task, finalizer is called from
|
|
|
|
* subtype_dealloc.
|
|
|
|
*/
|
|
|
|
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
|
|
|
|
// resurrected.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 03:31:07 -03:00
|
|
|
PyObject_GC_UnTrack(self);
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
if (task->task_weakreflist != NULL) {
|
|
|
|
PyObject_ClearWeakRefs(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)TaskObj_clear(task);
|
|
|
|
Py_TYPE(task)->tp_free(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
task_call_step_soon(TaskObj *task, PyObject *arg)
|
|
|
|
{
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *cb = TaskStepMethWrapper_new(task, arg);
|
2016-10-28 13:52:37 -03:00
|
|
|
if (cb == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
int ret = call_soon(task->task_loop, cb, NULL, task->task_context);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_DECREF(cb);
|
2017-12-17 21:19:47 -04:00
|
|
|
return ret;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...)
|
|
|
|
{
|
|
|
|
PyObject* msg;
|
|
|
|
|
|
|
|
va_list vargs;
|
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
va_start(vargs, format);
|
|
|
|
#else
|
|
|
|
va_start(vargs);
|
|
|
|
#endif
|
|
|
|
msg = PyUnicode_FromFormatV(format, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-04 17:59:09 -04:00
|
|
|
PyObject *e = PyObject_CallFunctionObjArgs(et, msg, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_DECREF(msg);
|
|
|
|
if (e == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task_call_step_soon(task, e) == -1) {
|
|
|
|
Py_DECREF(e);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(e);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
task_step_impl(TaskObj *task, PyObject *exc)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
int clear_exc = 0;
|
|
|
|
PyObject *result = NULL;
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *coro;
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *o;
|
|
|
|
|
|
|
|
if (task->task_state != STATE_PENDING) {
|
2017-12-25 11:48:15 -04:00
|
|
|
PyErr_Format(asyncio_InvalidStateError,
|
2016-10-28 13:52:37 -03:00
|
|
|
"_step(): already done: %R %R",
|
|
|
|
task,
|
|
|
|
exc ? exc : Py_None);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task->task_must_cancel) {
|
|
|
|
assert(exc != Py_None);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
/* Check if exc is a CancelledError */
|
|
|
|
res = PyObject_IsInstance(exc, asyncio_CancelledError);
|
|
|
|
if (res == -1) {
|
|
|
|
/* An error occurred, abort */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* exc is not CancelledError; reset it to NULL */
|
|
|
|
exc = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exc) {
|
|
|
|
/* exc was not a CancelledError */
|
2016-12-06 13:46:19 -04:00
|
|
|
exc = _PyObject_CallNoArg(asyncio_CancelledError);
|
2016-10-28 13:52:37 -03:00
|
|
|
if (!exc) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
clear_exc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
task->task_must_cancel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_CLEAR(task->task_fut_waiter);
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
coro = task->task_coro;
|
|
|
|
if (coro == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "uninitialized Task object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
if (exc == NULL) {
|
|
|
|
if (PyGen_CheckExact(coro) || PyCoro_CheckExact(coro)) {
|
|
|
|
result = _PyGen_Send((PyGenObject*)coro, Py_None);
|
|
|
|
}
|
|
|
|
else {
|
2016-12-09 09:24:02 -04:00
|
|
|
result = _PyObject_CallMethodIdObjArgs(coro, &PyId_send,
|
|
|
|
Py_None, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-12-09 09:24:02 -04:00
|
|
|
result = _PyObject_CallMethodIdObjArgs(coro, &PyId_throw,
|
|
|
|
exc, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
if (clear_exc) {
|
|
|
|
/* We created 'exc' during this call */
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_DECREF(exc);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
PyObject *et, *ev, *tb;
|
|
|
|
|
|
|
|
if (_PyGen_FetchStopIterationValue(&o) == 0) {
|
|
|
|
/* The error is StopIteration and that means that
|
|
|
|
the underlying coroutine has resolved */
|
2017-05-11 09:18:38 -03:00
|
|
|
if (task->task_must_cancel) {
|
|
|
|
// Task is cancelled right before coro stops.
|
|
|
|
Py_DECREF(o);
|
|
|
|
task->task_must_cancel = 0;
|
|
|
|
et = asyncio_CancelledError;
|
|
|
|
Py_INCREF(et);
|
|
|
|
ev = NULL;
|
|
|
|
tb = NULL;
|
|
|
|
goto set_exception;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
PyObject *res = future_set_result((FutureObj*)task, o);
|
|
|
|
Py_DECREF(o);
|
|
|
|
if (res == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyErr_ExceptionMatches(asyncio_CancelledError)) {
|
|
|
|
/* CancelledError */
|
|
|
|
PyErr_Clear();
|
|
|
|
return future_cancel((FutureObj*)task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some other exception; pop it and call Task.set_exception() */
|
|
|
|
PyErr_Fetch(&et, &ev, &tb);
|
2017-05-11 09:18:38 -03:00
|
|
|
|
|
|
|
set_exception:
|
2016-10-28 13:52:37 -03:00
|
|
|
assert(et);
|
|
|
|
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
|
|
|
|
PyErr_NormalizeException(&et, &ev, &tb);
|
|
|
|
}
|
2016-12-01 12:37:47 -04:00
|
|
|
if (tb != NULL) {
|
|
|
|
PyException_SetTraceback(ev, tb);
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
o = future_set_exception((FutureObj*)task, ev);
|
|
|
|
if (!o) {
|
|
|
|
/* An exception in Task.set_exception() */
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_DECREF(et);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_XDECREF(tb);
|
|
|
|
Py_XDECREF(ev);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
assert(o == Py_None);
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_DECREF(o);
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
if (!PyErr_GivenExceptionMatches(et, PyExc_Exception)) {
|
|
|
|
/* We've got a BaseException; re-raise it */
|
|
|
|
PyErr_Restore(et, ev, tb);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_DECREF(et);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_XDECREF(tb);
|
|
|
|
Py_XDECREF(ev);
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) */
|
|
|
|
if (Future_CheckExact(result) || Task_CheckExact(result)) {
|
|
|
|
PyObject *wrapper;
|
|
|
|
PyObject *res;
|
|
|
|
FutureObj *fut = (FutureObj*)result;
|
|
|
|
|
|
|
|
/* Check if `result` future is attached to a different loop */
|
|
|
|
if (fut->fut_loop != task->task_loop) {
|
|
|
|
goto different_loop;
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
if (!fut->fut_blocking) {
|
|
|
|
goto yield_insteadof_yf;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
fut->fut_blocking = 0;
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
/* result.add_done_callback(task._wakeup) */
|
|
|
|
wrapper = TaskWakeupMethWrapper_new(task);
|
|
|
|
if (wrapper == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
res = future_add_done_callback(
|
|
|
|
(FutureObj*)result, wrapper, task->task_context);
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
if (res == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
/* task._fut_waiter = result */
|
|
|
|
task->task_fut_waiter = result; /* no incref is necessary */
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
if (task->task_must_cancel) {
|
|
|
|
PyObject *r;
|
2018-10-03 11:30:31 -03:00
|
|
|
int is_true;
|
2018-10-05 15:20:02 -03:00
|
|
|
r = _PyObject_CallMethodId(result, &PyId_cancel, NULL);
|
2018-09-21 03:11:32 -03:00
|
|
|
if (r == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-03 11:30:31 -03:00
|
|
|
is_true = PyObject_IsTrue(r);
|
|
|
|
Py_DECREF(r);
|
|
|
|
if (is_true < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (is_true) {
|
2018-09-21 03:11:32 -03:00
|
|
|
task->task_must_cancel = 0;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2018-09-21 03: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. */
|
|
|
|
if (task_call_step_soon(task, NULL)) {
|
|
|
|
goto fail;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2018-09-21 03:11:32 -03:00
|
|
|
return result;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if `result` is a Future-compatible object */
|
2018-09-21 03:11:32 -03:00
|
|
|
if (_PyObject_LookupAttrId(result, &PyId__asyncio_future_blocking, &o) < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (o != NULL && o != Py_None) {
|
|
|
|
/* `result` is a Future-compatible object */
|
|
|
|
PyObject *wrapper;
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
int blocking = PyObject_IsTrue(o);
|
|
|
|
Py_DECREF(o);
|
|
|
|
if (blocking < 0) {
|
|
|
|
goto fail;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2018-09-21 03:11:32 -03:00
|
|
|
|
|
|
|
/* Check if `result` future is attached to a different loop */
|
|
|
|
PyObject *oloop = get_future_loop(result);
|
|
|
|
if (oloop == NULL) {
|
2016-10-28 13:52:37 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-09-21 03:11:32 -03:00
|
|
|
if (oloop != task->task_loop) {
|
|
|
|
Py_DECREF(oloop);
|
|
|
|
goto different_loop;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2018-09-21 03:11:32 -03:00
|
|
|
Py_DECREF(oloop);
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
if (!blocking) {
|
|
|
|
goto yield_insteadof_yf;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
/* result._asyncio_future_blocking = False */
|
|
|
|
if (_PyObject_SetAttrId(
|
|
|
|
result, &PyId__asyncio_future_blocking, Py_False) == -1) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
wrapper = TaskWakeupMethWrapper_new(task);
|
|
|
|
if (wrapper == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
/* result.add_done_callback(task._wakeup) */
|
|
|
|
PyObject *add_cb = _PyObject_GetAttrId(
|
|
|
|
result, &PyId_add_done_callback);
|
|
|
|
if (add_cb == NULL) {
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
PyObject *stack[2];
|
|
|
|
stack[0] = wrapper;
|
|
|
|
stack[1] = (PyObject *)task->task_context;
|
|
|
|
res = _PyObject_FastCallKeywords(
|
|
|
|
add_cb, stack, 1, context_kwname);
|
|
|
|
Py_DECREF(add_cb);
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
if (res == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
2018-01-22 20:11:18 -04:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
/* task._fut_waiter = result */
|
|
|
|
task->task_fut_waiter = result; /* no incref is necessary */
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
if (task->task_must_cancel) {
|
|
|
|
PyObject *r;
|
|
|
|
int is_true;
|
|
|
|
r = _PyObject_CallMethodId(result, &PyId_cancel, NULL);
|
|
|
|
if (r == NULL) {
|
|
|
|
return NULL;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2018-09-21 03: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 13:52:37 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
Py_RETURN_NONE;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
Py_XDECREF(o);
|
2016-10-28 13:52:37 -03:00
|
|
|
/* Check if `result` is a generator */
|
2018-09-21 03:11:32 -03:00
|
|
|
res = PyObject_IsInstance(result, (PyObject*)&PyGen_Type);
|
|
|
|
if (res < 0) {
|
2016-10-28 13:52:37 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-09-21 03:11:32 -03:00
|
|
|
if (res) {
|
2016-10-28 13:52:37 -03:00
|
|
|
/* `result` is a generator */
|
2018-05-20 10:30:31 -03:00
|
|
|
o = task_set_error_soon(
|
2016-10-28 13:52:37 -03:00
|
|
|
task, PyExc_RuntimeError,
|
|
|
|
"yield was used instead of yield from for "
|
2018-05-20 10:30:31 -03:00
|
|
|
"generator in task %R with %R", task, result);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_DECREF(result);
|
2018-05-20 10:30:31 -03:00
|
|
|
return o;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The `result` is none of the above */
|
2018-05-20 10:30:31 -03:00
|
|
|
o = task_set_error_soon(
|
2016-10-28 13:52:37 -03:00
|
|
|
task, PyExc_RuntimeError, "Task got bad yield: %R", result);
|
2018-05-20 10:30:31 -03:00
|
|
|
Py_DECREF(result);
|
|
|
|
return o;
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
self_await:
|
|
|
|
o = task_set_error_soon(
|
|
|
|
task, PyExc_RuntimeError,
|
|
|
|
"Task cannot await on itself: %R", task);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return o;
|
|
|
|
|
|
|
|
yield_insteadof_yf:
|
|
|
|
o = task_set_error_soon(
|
|
|
|
task, PyExc_RuntimeError,
|
|
|
|
"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(
|
|
|
|
task, PyExc_RuntimeError,
|
|
|
|
"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 *
|
|
|
|
task_step(TaskObj *task, PyObject *exc)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
|
2017-12-16 15:58:38 -04:00
|
|
|
if (enter_task(task->task_loop, (PyObject*)task) < 0) {
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = task_step_impl(task, exc);
|
|
|
|
|
|
|
|
if (res == NULL) {
|
|
|
|
PyObject *et, *ev, *tb;
|
|
|
|
PyErr_Fetch(&et, &ev, &tb);
|
2017-12-16 15:58:38 -04:00
|
|
|
leave_task(task->task_loop, (PyObject*)task);
|
2017-09-03 02:10:14 -03:00
|
|
|
_PyErr_ChainExceptions(et, ev, tb);
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
2018-09-21 03:11:32 -03:00
|
|
|
if (leave_task(task->task_loop, (PyObject*)task) < 0) {
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
task_wakeup(TaskObj *task, PyObject *o)
|
|
|
|
{
|
2017-09-03 02:10:14 -03:00
|
|
|
PyObject *et, *ev, *tb;
|
|
|
|
PyObject *result;
|
2016-10-28 13:52:37 -03:00
|
|
|
assert(o);
|
|
|
|
|
|
|
|
if (Future_CheckExact(o) || Task_CheckExact(o)) {
|
|
|
|
PyObject *fut_result = NULL;
|
|
|
|
int res = future_get_result((FutureObj*)o, &fut_result);
|
|
|
|
|
|
|
|
switch(res) {
|
|
|
|
case -1:
|
|
|
|
assert(fut_result == NULL);
|
2017-09-03 02:10:14 -03:00
|
|
|
break; /* exception raised */
|
2016-10-28 13:52:37 -03:00
|
|
|
case 0:
|
|
|
|
Py_DECREF(fut_result);
|
2018-01-24 12:31:01 -04:00
|
|
|
return task_step(task, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
default:
|
|
|
|
assert(res == 1);
|
2018-01-24 12:31:01 -04:00
|
|
|
result = task_step(task, fut_result);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_DECREF(fut_result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
else {
|
|
|
|
PyObject *fut_result = PyObject_CallMethod(o, "result", NULL);
|
|
|
|
if (fut_result != NULL) {
|
|
|
|
Py_DECREF(fut_result);
|
2018-01-24 12:31:01 -04:00
|
|
|
return task_step(task, NULL);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
/* exception raised */
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
PyErr_Fetch(&et, &ev, &tb);
|
|
|
|
if (!PyErr_GivenExceptionMatches(et, PyExc_Exception)) {
|
|
|
|
/* We've got a BaseException; re-raise it */
|
|
|
|
PyErr_Restore(et, ev, tb);
|
|
|
|
return NULL;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
|
|
|
|
PyErr_NormalizeException(&et, &ev, &tb);
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
|
2018-01-24 12:31:01 -04:00
|
|
|
result = task_step(task, ev);
|
2017-09-03 02:10:14 -03:00
|
|
|
|
|
|
|
Py_DECREF(et);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
Py_XDECREF(ev);
|
|
|
|
|
|
|
|
return result;
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 15:49:42 -04: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]*/
|
|
|
|
{
|
|
|
|
PyObject *loop;
|
|
|
|
if (get_running_loop(&loop)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
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]*/
|
|
|
|
{
|
|
|
|
if (set_running_loop(loop)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
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]*/
|
|
|
|
{
|
|
|
|
return get_event_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[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;
|
|
|
|
if (get_running_loop(&loop)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (loop == NULL) {
|
|
|
|
/* There's no currently running event loop */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError, "no running event loop");
|
|
|
|
}
|
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2017-12-16 15:58:38 -04: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 16:04:15 -04:00
|
|
|
_asyncio__register_task_impl(PyObject *module, PyObject *task)
|
|
|
|
/*[clinic end generated code: output=8672dadd69a7d4e2 input=21075aaea14dfbad]*/
|
2017-12-16 15:58:38 -04:00
|
|
|
{
|
2017-12-23 16:04:15 -04:00
|
|
|
if (register_task(task) < 0) {
|
2017-12-16 15:58:38 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_asyncio._unregister_task
|
|
|
|
|
|
|
|
task: object
|
|
|
|
|
|
|
|
Unregister a task.
|
|
|
|
|
|
|
|
Returns None.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2017-12-23 16:04:15 -04:00
|
|
|
_asyncio__unregister_task_impl(PyObject *module, PyObject *task)
|
|
|
|
/*[clinic end generated code: output=6e5585706d568a46 input=28fb98c3975f7bdc]*/
|
2017-12-16 15:58:38 -04:00
|
|
|
{
|
2017-12-23 16:04:15 -04:00
|
|
|
if (unregister_task(task) < 0) {
|
2017-12-16 15:58:38 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[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]*/
|
|
|
|
{
|
|
|
|
if (enter_task(loop, task) < 0) {
|
|
|
|
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]*/
|
|
|
|
{
|
|
|
|
if (leave_task(loop, task) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:49:42 -04:00
|
|
|
|
2018-01-23 16:10:03 -04:00
|
|
|
/*********************** PyRunningLoopHolder ********************/
|
|
|
|
|
|
|
|
|
|
|
|
static PyRunningLoopHolder *
|
|
|
|
new_running_loop_holder(PyObject *loop)
|
|
|
|
{
|
|
|
|
PyRunningLoopHolder *rl = PyObject_New(
|
|
|
|
PyRunningLoopHolder, &PyRunningLoopHolder_Type);
|
|
|
|
if (rl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
|
|
|
|
rl->rl_pid = getpid();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Py_INCREF(loop);
|
|
|
|
rl->rl_loop = loop;
|
|
|
|
|
|
|
|
return rl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
PyRunningLoopHolder_tp_dealloc(PyRunningLoopHolder *rl)
|
|
|
|
{
|
|
|
|
Py_CLEAR(rl->rl_loop);
|
|
|
|
PyObject_Free(rl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyTypeObject PyRunningLoopHolder_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"_RunningLoopHolder",
|
|
|
|
sizeof(PyRunningLoopHolder),
|
|
|
|
.tp_getattro = PyObject_GenericGetAttr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
|
|
.tp_dealloc = (destructor)PyRunningLoopHolder_tp_dealloc,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
/*********************** Module **************************/
|
|
|
|
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
static void
|
2017-12-19 14:48:13 -04:00
|
|
|
module_free_freelists(void)
|
2017-12-17 21:19:47 -04:00
|
|
|
{
|
|
|
|
PyObject *next;
|
|
|
|
PyObject *current;
|
|
|
|
|
|
|
|
next = (PyObject*) fi_freelist;
|
|
|
|
while (next != NULL) {
|
|
|
|
assert(fi_freelist_len > 0);
|
|
|
|
fi_freelist_len--;
|
|
|
|
|
|
|
|
current = next;
|
|
|
|
next = (PyObject*) ((futureiterobject*) current)->future;
|
|
|
|
PyObject_GC_Del(current);
|
|
|
|
}
|
|
|
|
assert(fi_freelist_len == 0);
|
|
|
|
fi_freelist = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static void
|
|
|
|
module_free(void *m)
|
|
|
|
{
|
2017-12-16 15:58:38 -04:00
|
|
|
Py_CLEAR(asyncio_mod);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(traceback_extract_stack);
|
|
|
|
Py_CLEAR(asyncio_future_repr_info_func);
|
2017-12-16 15:58:38 -04:00
|
|
|
Py_CLEAR(asyncio_get_event_loop_policy);
|
2017-12-15 01:04:38 -04:00
|
|
|
Py_CLEAR(asyncio_iscoroutine_func);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(asyncio_task_get_stack_func);
|
|
|
|
Py_CLEAR(asyncio_task_print_stack_func);
|
2017-12-16 15:58:38 -04:00
|
|
|
Py_CLEAR(asyncio_task_repr_info_func);
|
2016-10-28 13:52:37 -03:00
|
|
|
Py_CLEAR(asyncio_InvalidStateError);
|
|
|
|
Py_CLEAR(asyncio_CancelledError);
|
2017-12-16 15:58:38 -04:00
|
|
|
|
|
|
|
Py_CLEAR(all_tasks);
|
2017-12-19 08:18:45 -04:00
|
|
|
Py_CLEAR(current_tasks);
|
|
|
|
Py_CLEAR(iscoroutine_typecache);
|
2017-12-17 21:19:47 -04:00
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
Py_CLEAR(context_kwname);
|
|
|
|
|
2017-12-17 21:19:47 -04:00
|
|
|
module_free_freelists();
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
module_init(void)
|
|
|
|
{
|
|
|
|
PyObject *module = NULL;
|
2017-12-16 15:58:38 -04:00
|
|
|
|
|
|
|
asyncio_mod = PyImport_ImportModule("asyncio");
|
|
|
|
if (asyncio_mod == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_tasks = PyDict_New();
|
|
|
|
if (current_tasks == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
2017-12-19 08:18:45 -04:00
|
|
|
iscoroutine_typecache = PySet_New(NULL);
|
|
|
|
if (iscoroutine_typecache == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:11:18 -04:00
|
|
|
|
2018-09-21 03:11:32 -03:00
|
|
|
context_kwname = Py_BuildValue("(s)", "context");
|
2018-01-22 20:11:18 -04:00
|
|
|
if (context_kwname == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
#define WITH_MOD(NAME) \
|
|
|
|
Py_CLEAR(module); \
|
|
|
|
module = PyImport_ImportModule(NAME); \
|
|
|
|
if (module == NULL) { \
|
2017-09-03 02:10:14 -03:00
|
|
|
goto fail; \
|
2016-10-28 13:52:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_MOD_ATTR(VAR, NAME) \
|
|
|
|
VAR = PyObject_GetAttrString(module, NAME); \
|
|
|
|
if (VAR == NULL) { \
|
|
|
|
goto fail; \
|
|
|
|
}
|
|
|
|
|
|
|
|
WITH_MOD("asyncio.events")
|
2017-12-13 15:49:42 -04:00
|
|
|
GET_MOD_ATTR(asyncio_get_event_loop_policy, "get_event_loop_policy")
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
WITH_MOD("asyncio.base_futures")
|
|
|
|
GET_MOD_ATTR(asyncio_future_repr_info_func, "_future_repr_info")
|
2018-09-11 14:13:04 -03:00
|
|
|
|
|
|
|
WITH_MOD("asyncio.exceptions")
|
2016-10-28 13:52:37 -03:00
|
|
|
GET_MOD_ATTR(asyncio_InvalidStateError, "InvalidStateError")
|
|
|
|
GET_MOD_ATTR(asyncio_CancelledError, "CancelledError")
|
|
|
|
|
|
|
|
WITH_MOD("asyncio.base_tasks")
|
|
|
|
GET_MOD_ATTR(asyncio_task_repr_info_func, "_task_repr_info")
|
|
|
|
GET_MOD_ATTR(asyncio_task_get_stack_func, "_task_get_stack")
|
|
|
|
GET_MOD_ATTR(asyncio_task_print_stack_func, "_task_print_stack")
|
|
|
|
|
2017-12-15 01:04:38 -04:00
|
|
|
WITH_MOD("asyncio.coroutines")
|
|
|
|
GET_MOD_ATTR(asyncio_iscoroutine_func, "iscoroutine")
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
WITH_MOD("traceback")
|
|
|
|
GET_MOD_ATTR(traceback_extract_stack, "extract_stack")
|
|
|
|
|
2017-12-23 16:04:15 -04:00
|
|
|
PyObject *weak_set;
|
2016-10-28 13:52:37 -03:00
|
|
|
WITH_MOD("weakref")
|
2017-12-23 16:04:15 -04:00
|
|
|
GET_MOD_ATTR(weak_set, "WeakSet");
|
|
|
|
all_tasks = _PyObject_CallNoArg(weak_set);
|
|
|
|
Py_CLEAR(weak_set);
|
2016-10-28 13:52:37 -03:00
|
|
|
if (all_tasks == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-09-03 02:10:14 -03:00
|
|
|
Py_DECREF(module);
|
2016-10-28 13:52:37 -03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
Py_CLEAR(module);
|
|
|
|
module_free(NULL);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#undef WITH_MOD
|
|
|
|
#undef GET_MOD_ATTR
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");
|
|
|
|
|
2017-12-13 15:49:42 -04:00
|
|
|
static PyMethodDef asyncio_methods[] = {
|
|
|
|
_ASYNCIO_GET_EVENT_LOOP_METHODDEF
|
|
|
|
_ASYNCIO_GET_RUNNING_LOOP_METHODDEF
|
|
|
|
_ASYNCIO__GET_RUNNING_LOOP_METHODDEF
|
|
|
|
_ASYNCIO__SET_RUNNING_LOOP_METHODDEF
|
2017-12-16 15:58:38 -04:00
|
|
|
_ASYNCIO__REGISTER_TASK_METHODDEF
|
|
|
|
_ASYNCIO__UNREGISTER_TASK_METHODDEF
|
|
|
|
_ASYNCIO__ENTER_TASK_METHODDEF
|
|
|
|
_ASYNCIO__LEAVE_TASK_METHODDEF
|
2017-12-13 15:49:42 -04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2016-10-28 13:52:37 -03:00
|
|
|
static struct PyModuleDef _asynciomodule = {
|
|
|
|
PyModuleDef_HEAD_INIT, /* m_base */
|
|
|
|
"_asyncio", /* m_name */
|
|
|
|
module_doc, /* m_doc */
|
|
|
|
-1, /* m_size */
|
2017-12-13 15:49:42 -04:00
|
|
|
asyncio_methods, /* m_methods */
|
2016-10-28 13:52:37 -03:00
|
|
|
NULL, /* m_slots */
|
|
|
|
NULL, /* m_traverse */
|
|
|
|
NULL, /* m_clear */
|
|
|
|
(freefunc)module_free /* m_free */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__asyncio(void)
|
|
|
|
{
|
|
|
|
if (module_init() < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyType_Ready(&FutureType) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyType_Ready(&FutureIterType) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-03 02:10:14 -03:00
|
|
|
if (PyType_Ready(&TaskStepMethWrapper_Type) < 0) {
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-21 03:11:32 -03:00
|
|
|
if (PyType_Ready(&TaskWakeupMethWrapper_Type) < 0) {
|
2016-10-28 13:52:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyType_Ready(&TaskType) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-23 16:10:03 -04:00
|
|
|
if (PyType_Ready(&PyRunningLoopHolder_Type) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-28 13:52:37 -03:00
|
|
|
|
|
|
|
PyObject *m = PyModule_Create(&_asynciomodule);
|
|
|
|
if (m == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(&FutureType);
|
|
|
|
if (PyModule_AddObject(m, "Future", (PyObject *)&FutureType) < 0) {
|
|
|
|
Py_DECREF(&FutureType);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(&TaskType);
|
|
|
|
if (PyModule_AddObject(m, "Task", (PyObject *)&TaskType) < 0) {
|
|
|
|
Py_DECREF(&TaskType);
|
2016-10-09 02:44:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-16 15:58:38 -04:00
|
|
|
Py_INCREF(all_tasks);
|
|
|
|
if (PyModule_AddObject(m, "_all_tasks", all_tasks) < 0) {
|
|
|
|
Py_DECREF(all_tasks);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(current_tasks);
|
|
|
|
if (PyModule_AddObject(m, "_current_tasks", current_tasks) < 0) {
|
|
|
|
Py_DECREF(current_tasks);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-09 02:44:47 -03:00
|
|
|
return m;
|
|
|
|
}
|