2004-06-01 12:22:42 -03:00
|
|
|
/* Generator object implementation */
|
|
|
|
|
2022-05-18 10:38:43 -03:00
|
|
|
#define _PY_INTERPRETER
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
#include "Python.h"
|
2021-10-12 03:38:19 -03:00
|
|
|
#include "pycore_call.h" // _PyObject_CallNoArgs()
|
2020-04-14 21:35:41 -03:00
|
|
|
#include "pycore_ceval.h" // _PyEval_EvalFrame()
|
2022-04-25 19:14:30 -03:00
|
|
|
#include "pycore_frame.h" // _PyInterpreterFrame
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_genobject.h" // struct _Py_async_gen_state
|
2021-10-12 03:38:19 -03:00
|
|
|
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
|
2020-05-18 02:47:31 -03:00
|
|
|
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
|
2020-04-14 21:35:41 -03:00
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
2023-07-25 10:28:30 -03:00
|
|
|
|
2021-12-15 06:30:09 -04:00
|
|
|
#include "opcode.h" // SEND
|
2023-05-05 13:53:07 -03:00
|
|
|
#include "frameobject.h" // _PyInterpreterFrame_GetLine
|
2022-05-27 12:31:41 -03:00
|
|
|
#include "pystats.h"
|
2004-06-01 12:22:42 -03:00
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
static PyObject *gen_close(PyGenObject *, PyObject *);
|
|
|
|
static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
|
|
|
|
static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
|
|
|
|
|
2020-02-14 00:42:56 -04:00
|
|
|
static const char *NON_INIT_CORO_MSG = "can't send non-None value to a "
|
2016-09-09 02:01:51 -03:00
|
|
|
"just-started coroutine";
|
|
|
|
|
2020-02-14 00:42:56 -04:00
|
|
|
static const char *ASYNC_GEN_IGNORED_EXIT_MSG =
|
2016-09-09 02:01:51 -03:00
|
|
|
"async generator ignored GeneratorExit";
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2023-02-23 06:19:01 -04:00
|
|
|
/* Returns a borrowed reference */
|
|
|
|
static inline PyCodeObject *
|
|
|
|
_PyGen_GetCode(PyGenObject *gen) {
|
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe);
|
2023-06-14 09:46:37 -03:00
|
|
|
return _PyFrame_GetCode(frame);
|
2023-02-23 06:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyCodeObject *
|
|
|
|
PyGen_GetCode(PyGenObject *gen) {
|
|
|
|
assert(PyGen_Check(gen));
|
|
|
|
PyCodeObject *res = _PyGen_GetCode(gen);
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-22 18:41:51 -03:00
|
|
|
static inline int
|
|
|
|
exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(exc_state->exc_value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
static int
|
|
|
|
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
|
|
|
|
{
|
2014-06-16 10:59:28 -03:00
|
|
|
Py_VISIT(gen->gi_name);
|
|
|
|
Py_VISIT(gen->gi_qualname);
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state < FRAME_CLEARED) {
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe);
|
2022-03-22 09:57:19 -03:00
|
|
|
assert(frame->frame_obj == NULL ||
|
|
|
|
frame->frame_obj->f_frame->owner == FRAME_OWNED_BY_GENERATOR);
|
2021-07-26 07:22:16 -03:00
|
|
|
int err = _PyFrame_Traverse(frame, visit, arg);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 10:44:07 -04:00
|
|
|
/* No need to visit cr_origin, because it's just tuples/str/int, so can't
|
|
|
|
participate in a reference cycle. */
|
2017-10-22 18:41:51 -03:00
|
|
|
return exc_state_traverse(&gen->gi_exc_state, visit, arg);
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
|
|
|
|
2013-08-05 18:26:40 -03:00
|
|
|
void
|
|
|
|
_PyGen_Finalize(PyObject *self)
|
2013-07-30 14:59:21 -03:00
|
|
|
{
|
|
|
|
PyGenObject *gen = (PyGenObject *)self;
|
|
|
|
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state >= FRAME_COMPLETED) {
|
2016-09-05 14:14:54 -03:00
|
|
|
/* Generator isn't paused, so no need to close */
|
|
|
|
return;
|
2018-01-29 15:31:47 -04:00
|
|
|
}
|
2016-09-05 14:14:54 -03:00
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyAsyncGen_CheckExact(self)) {
|
|
|
|
PyAsyncGenObject *agen = (PyAsyncGenObject*)self;
|
2021-12-06 06:13:49 -04:00
|
|
|
PyObject *finalizer = agen->ag_origin_or_finalizer;
|
2016-09-09 02:01:51 -03:00
|
|
|
if (finalizer && !agen->ag_closed) {
|
|
|
|
/* Save the current exception, if any. */
|
2023-03-08 13:03:18 -04:00
|
|
|
PyObject *exc = PyErr_GetRaisedException();
|
2016-09-09 02:01:51 -03:00
|
|
|
|
2023-03-08 13:03:18 -04:00
|
|
|
PyObject *res = PyObject_CallOneArg(finalizer, self);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
PyErr_WriteUnraisable(self);
|
|
|
|
} else {
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
/* Restore the saved exception. */
|
2023-03-08 13:03:18 -04:00
|
|
|
PyErr_SetRaisedException(exc);
|
2016-09-09 02:01:51 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:14:54 -03:00
|
|
|
/* Save the current exception, if any. */
|
2023-03-08 13:03:18 -04:00
|
|
|
PyObject *exc = PyErr_GetRaisedException();
|
2016-09-05 14:14:54 -03:00
|
|
|
|
2015-05-11 23:57:16 -03:00
|
|
|
/* If `gen` is a coroutine, and if it was never awaited on,
|
|
|
|
issue a RuntimeWarning. */
|
2023-02-23 06:19:01 -04:00
|
|
|
assert(_PyGen_GetCode(gen) != NULL);
|
|
|
|
if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE &&
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state == FRAME_CREATED)
|
2018-01-29 15:31:47 -04:00
|
|
|
{
|
|
|
|
_PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
|
2016-09-05 14:14:54 -03:00
|
|
|
}
|
|
|
|
else {
|
2023-03-08 13:03:18 -04:00
|
|
|
PyObject *res = gen_close(gen, NULL);
|
|
|
|
if (res == NULL) {
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
PyErr_WriteUnraisable(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(res);
|
2018-01-29 15:31:47 -04:00
|
|
|
}
|
2016-09-07 12:46:59 -03:00
|
|
|
}
|
2013-07-30 14:59:21 -03:00
|
|
|
|
|
|
|
/* Restore the saved exception. */
|
2023-03-08 13:03:18 -04:00
|
|
|
PyErr_SetRaisedException(exc);
|
2013-07-30 14:59:21 -03:00
|
|
|
}
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
static void
|
|
|
|
gen_dealloc(PyGenObject *gen)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *self = (PyObject *) gen;
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(gen);
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (gen->gi_weakreflist != NULL)
|
|
|
|
PyObject_ClearWeakRefs(self);
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
_PyObject_GC_TRACK(self);
|
|
|
|
|
2013-07-30 14:59:21 -03:00
|
|
|
if (PyObject_CallFinalizerFromDealloc(self))
|
|
|
|
return; /* resurrected. :( */
|
2013-05-14 15:37:52 -03:00
|
|
|
|
|
|
|
_PyObject_GC_UNTRACK(self);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyAsyncGen_CheckExact(gen)) {
|
|
|
|
/* We have to handle this case for asynchronous generators
|
|
|
|
right here, because this code has to be between UNTRACK
|
|
|
|
and GC_Del. */
|
2021-12-06 06:13:49 -04:00
|
|
|
Py_CLEAR(((PyAsyncGenObject*)gen)->ag_origin_or_finalizer);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state < FRAME_CLEARED) {
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = FRAME_CLEARED;
|
2021-07-26 07:22:16 -03:00
|
|
|
frame->previous = NULL;
|
2023-02-23 06:19:01 -04:00
|
|
|
_PyFrame_ClearExceptCode(frame);
|
2023-08-02 14:44:20 -03:00
|
|
|
_PyErr_ClearExcState(&gen->gi_exc_state);
|
2016-09-05 14:39:57 -03:00
|
|
|
}
|
2023-08-02 14:44:20 -03:00
|
|
|
assert(gen->gi_exc_state.exc_value == NULL);
|
2023-02-23 06:19:01 -04:00
|
|
|
if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE) {
|
2021-12-06 06:13:49 -04:00
|
|
|
Py_CLEAR(((PyCoroObject *)gen)->cr_origin_or_finalizer);
|
2018-01-21 10:44:07 -04:00
|
|
|
}
|
2023-02-23 06:19:01 -04:00
|
|
|
Py_DECREF(_PyGen_GetCode(gen));
|
2014-06-16 10:59:28 -03:00
|
|
|
Py_CLEAR(gen->gi_name);
|
|
|
|
Py_CLEAR(gen->gi_qualname);
|
2023-08-02 14:44:20 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_Del(gen);
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
|
|
|
|
2020-09-22 02:08:54 -03:00
|
|
|
static PySendResult
|
|
|
|
gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
|
|
|
|
int exc, int closing)
|
2004-06-01 12:22:42 -03:00
|
|
|
{
|
2018-10-31 21:51:40 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
|
2013-05-14 15:37:52 -03:00
|
|
|
PyObject *result;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2020-09-22 02:08:54 -03:00
|
|
|
*presult = NULL;
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state == FRAME_CREATED && arg && arg != Py_None) {
|
2021-12-08 08:09:26 -04:00
|
|
|
const char *msg = "can't send non-None value to a "
|
|
|
|
"just-started generator";
|
|
|
|
if (PyCoro_CheckExact(gen)) {
|
|
|
|
msg = NON_INIT_CORO_MSG;
|
|
|
|
}
|
|
|
|
else if (PyAsyncGen_CheckExact(gen)) {
|
|
|
|
msg = "can't send non-None value to a "
|
|
|
|
"just-started async generator";
|
|
|
|
}
|
|
|
|
PyErr_SetString(PyExc_TypeError, msg);
|
|
|
|
return PYGEN_ERROR;
|
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state == FRAME_EXECUTING) {
|
2017-11-11 07:06:26 -04:00
|
|
|
const char *msg = "generator already executing";
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyCoro_CheckExact(gen)) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
msg = "coroutine already executing";
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
else if (PyAsyncGen_CheckExact(gen)) {
|
|
|
|
msg = "async generator already executing";
|
|
|
|
}
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, msg);
|
2020-09-22 02:08:54 -03:00
|
|
|
return PYGEN_ERROR;
|
2012-03-15 17:37:39 -03:00
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state >= FRAME_COMPLETED) {
|
2016-02-13 18:59:05 -04:00
|
|
|
if (PyCoro_CheckExact(gen) && !closing) {
|
|
|
|
/* `gen` is an exhausted coroutine: raise an error,
|
|
|
|
except when called from gen_close(), which should
|
|
|
|
always be a silent method. */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"cannot reuse already awaited coroutine");
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
else if (arg && !exc) {
|
2016-02-13 18:59:05 -04:00
|
|
|
/* `gen` is an exhausted generator:
|
2020-09-22 02:08:54 -03:00
|
|
|
only return value if called from send(). */
|
2022-11-10 17:22:02 -04:00
|
|
|
*presult = Py_NewRef(Py_None);
|
2020-09-22 02:08:54 -03:00
|
|
|
return PYGEN_RETURN;
|
2016-02-13 18:59:05 -04:00
|
|
|
}
|
2020-09-22 02:08:54 -03:00
|
|
|
return PYGEN_ERROR;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2022-03-22 09:57:19 -03:00
|
|
|
assert(gen->gi_frame_state < FRAME_EXECUTING);
|
2021-04-06 07:48:59 -03:00
|
|
|
/* Push arg onto the frame's value stack */
|
|
|
|
result = arg ? arg : Py_None;
|
2022-11-10 17:22:02 -04:00
|
|
|
_PyFrame_StackPush(frame, Py_NewRef(result));
|
2013-05-14 15:37:52 -03:00
|
|
|
|
2022-11-07 10:49:51 -04:00
|
|
|
_PyErr_StackItem *prev_exc_info = tstate->exc_info;
|
|
|
|
gen->gi_exc_state.previous_item = prev_exc_info;
|
2017-10-22 18:41:51 -03:00
|
|
|
tstate->exc_info = &gen->gi_exc_state;
|
2020-05-22 17:33:27 -03:00
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
assert(_PyErr_Occurred(tstate));
|
2023-06-01 17:57:28 -03:00
|
|
|
_PyErr_ChainStackItem();
|
2020-05-22 17:33:27 -03:00
|
|
|
}
|
|
|
|
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = FRAME_EXECUTING;
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC(EVAL_CALL_GENERATOR);
|
2021-07-26 07:22:16 -03:00
|
|
|
result = _PyEval_EvalFrame(tstate, frame, exc);
|
2022-11-07 10:49:51 -04:00
|
|
|
assert(tstate->exc_info == prev_exc_info);
|
|
|
|
assert(gen->gi_exc_state.previous_item == NULL);
|
2022-11-10 08:34:57 -04:00
|
|
|
assert(gen->gi_frame_state != FRAME_EXECUTING);
|
|
|
|
assert(frame->previous == NULL);
|
2013-05-14 15:37:52 -03:00
|
|
|
|
|
|
|
/* If the generator just returned (as opposed to yielding), signal
|
|
|
|
* that the generator is exhausted. */
|
2020-09-22 02:08:54 -03:00
|
|
|
if (result) {
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state == FRAME_SUSPENDED) {
|
2020-09-22 02:08:54 -03:00
|
|
|
*presult = result;
|
|
|
|
return PYGEN_NEXT;
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
2020-09-22 02:08:54 -03:00
|
|
|
assert(result == Py_None || !PyAsyncGen_CheckExact(gen));
|
|
|
|
if (result == Py_None && !PyAsyncGen_CheckExact(gen) && !arg) {
|
|
|
|
/* Return NULL if called by gen_iternext() */
|
|
|
|
Py_CLEAR(result);
|
2013-05-14 15:37:52 -03:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 02:08:54 -03:00
|
|
|
else {
|
2022-11-03 08:38:51 -03:00
|
|
|
assert(!PyErr_ExceptionMatches(PyExc_StopIteration));
|
|
|
|
assert(!PyAsyncGen_CheckExact(gen) ||
|
|
|
|
!PyErr_ExceptionMatches(PyExc_StopAsyncIteration));
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
|
2023-08-02 14:44:20 -03:00
|
|
|
assert(gen->gi_exc_state.exc_value == NULL);
|
2022-11-10 08:34:57 -04:00
|
|
|
assert(gen->gi_frame_state == FRAME_CLEARED);
|
2020-09-22 02:08:54 -03:00
|
|
|
*presult = result;
|
|
|
|
return result ? PYGEN_RETURN : PYGEN_ERROR;
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
|
2020-11-10 16:09:55 -04:00
|
|
|
static PySendResult
|
|
|
|
PyGen_am_send(PyGenObject *gen, PyObject *arg, PyObject **result)
|
2020-09-22 02:08:54 -03:00
|
|
|
{
|
2020-11-10 16:09:55 -04:00
|
|
|
return gen_send_ex2(gen, arg, result, 0, 0);
|
2020-09-22 02:08:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
|
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
if (gen_send_ex2(gen, arg, &result, exc, closing) == PYGEN_RETURN) {
|
|
|
|
if (PyAsyncGen_CheckExact(gen)) {
|
|
|
|
assert(result == Py_None);
|
|
|
|
PyErr_SetNone(PyExc_StopAsyncIteration);
|
|
|
|
}
|
|
|
|
else if (result == Py_None) {
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_PyGen_SetStopIterationValue(result);
|
|
|
|
}
|
|
|
|
Py_CLEAR(result);
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
return result;
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
PyDoc_STRVAR(send_doc,
|
2006-04-21 07:40:58 -03:00
|
|
|
"send(arg) -> send 'arg' into generator,\n\
|
|
|
|
return next yielded value or raise StopIteration.");
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2020-09-22 02:08:54 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_send(PyGenObject *gen, PyObject *arg)
|
2005-08-01 21:46:46 -03:00
|
|
|
{
|
2020-09-22 02:08:54 -03:00
|
|
|
return gen_send_ex(gen, arg, 0, 0);
|
2020-09-18 22:38:38 -03:00
|
|
|
}
|
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
PyDoc_STRVAR(close_doc,
|
2012-05-03 19:44:09 -03:00
|
|
|
"close() -> raise GeneratorExit inside generator.");
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
/*
|
|
|
|
* This helper function is used by gen_close and gen_throw to
|
|
|
|
* close a subiterator being delegated to by yield-from.
|
|
|
|
*/
|
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
static int
|
|
|
|
gen_close_iter(PyObject *yf)
|
|
|
|
{
|
|
|
|
PyObject *retval = NULL;
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
|
2013-05-14 15:37:52 -03:00
|
|
|
retval = gen_close((PyGenObject *)yf, NULL);
|
|
|
|
if (retval == NULL)
|
|
|
|
return -1;
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
else {
|
2018-01-25 04:49:40 -04:00
|
|
|
PyObject *meth;
|
2023-07-12 02:57:10 -03:00
|
|
|
if (PyObject_GetOptionalAttr(yf, &_Py_ID(close), &meth) < 0) {
|
2018-01-25 04:49:40 -04:00
|
|
|
PyErr_WriteUnraisable(yf);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
2018-01-25 04:49:40 -04:00
|
|
|
if (meth) {
|
2021-10-11 19:42:23 -03:00
|
|
|
retval = _PyObject_CallNoArgs(meth);
|
2013-05-14 15:37:52 -03:00
|
|
|
Py_DECREF(meth);
|
|
|
|
if (retval == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_XDECREF(retval);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-02 06:39:38 -03:00
|
|
|
static inline bool
|
|
|
|
is_resume(_Py_CODEUNIT *instr)
|
|
|
|
{
|
2023-09-07 10:39:03 -03:00
|
|
|
return (
|
|
|
|
instr->op.code == RESUME ||
|
|
|
|
instr->op.code == RESUME_CHECK ||
|
|
|
|
instr->op.code == INSTRUMENTED_RESUME
|
|
|
|
);
|
2023-06-02 06:39:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
is_yield(_Py_CODEUNIT *instr)
|
|
|
|
{
|
|
|
|
return instr->op.code == YIELD_VALUE || instr->op.code == INSTRUMENTED_YIELD_VALUE;
|
|
|
|
}
|
|
|
|
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *
|
|
|
|
_PyGen_yf(PyGenObject *gen)
|
2012-03-15 17:37:39 -03:00
|
|
|
{
|
2013-05-14 15:37:52 -03:00
|
|
|
PyObject *yf = NULL;
|
|
|
|
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state < FRAME_CLEARED) {
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
|
2013-05-14 15:37:52 -03:00
|
|
|
|
2022-04-07 16:31:01 -03:00
|
|
|
if (gen->gi_frame_state == FRAME_CREATED) {
|
2021-12-15 06:30:09 -04:00
|
|
|
/* Return immediately if the frame didn't start yet. SEND
|
2016-11-24 17:33:01 -04:00
|
|
|
always come after LOAD_CONST: a code object should not start
|
2021-12-15 06:30:09 -04:00
|
|
|
with SEND */
|
2023-02-23 06:19:01 -04:00
|
|
|
assert(_PyCode_CODE(_PyGen_GetCode(gen))[0].op.code != SEND);
|
2016-11-24 17:33:01 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-07 16:31:01 -03:00
|
|
|
_Py_CODEUNIT next = frame->prev_instr[1];
|
2023-06-02 06:39:38 -03:00
|
|
|
if (!is_resume(&next) || next.op.arg < 2)
|
2022-03-21 08:11:17 -03:00
|
|
|
{
|
2022-03-15 21:08:37 -03:00
|
|
|
/* Not in a yield from */
|
2013-05-14 15:37:52 -03:00
|
|
|
return NULL;
|
2022-03-15 21:08:37 -03:00
|
|
|
}
|
2022-11-16 13:34:24 -04:00
|
|
|
yf = Py_NewRef(_PyFrame_StackPeek(frame));
|
2013-05-14 15:37:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return yf;
|
2012-03-15 17:37:39 -03:00
|
|
|
}
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_close(PyGenObject *gen, PyObject *args)
|
|
|
|
{
|
2013-05-14 15:37:52 -03:00
|
|
|
PyObject *retval;
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf(gen);
|
2013-05-14 15:37:52 -03:00
|
|
|
int err = 0;
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2023-01-24 13:25:37 -04:00
|
|
|
if (gen->gi_frame_state == FRAME_CREATED) {
|
|
|
|
gen->gi_frame_state = FRAME_COMPLETED;
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
if (gen->gi_frame_state >= FRAME_COMPLETED) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
if (yf) {
|
2022-03-22 09:57:19 -03:00
|
|
|
PyFrameState state = gen->gi_frame_state;
|
|
|
|
gen->gi_frame_state = FRAME_EXECUTING;
|
2013-05-14 15:37:52 -03:00
|
|
|
err = gen_close_iter(yf);
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = state;
|
2013-05-14 15:37:52 -03:00
|
|
|
Py_DECREF(yf);
|
|
|
|
}
|
2023-01-24 13:25:37 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
|
|
|
|
/* It is possible for the previous instruction to not be a
|
|
|
|
* YIELD_VALUE if the debugger has changed the lineno. */
|
2023-06-02 06:39:38 -03:00
|
|
|
if (err == 0 && is_yield(frame->prev_instr)) {
|
|
|
|
assert(is_resume(frame->prev_instr + 1));
|
2023-02-20 10:56:48 -04:00
|
|
|
int exception_handler_depth = frame->prev_instr[0].op.code;
|
2023-01-24 13:25:37 -04:00
|
|
|
assert(exception_handler_depth > 0);
|
|
|
|
/* We can safely ignore the outermost try block
|
|
|
|
* as it automatically generated to handle
|
|
|
|
* StopIteration. */
|
|
|
|
if (exception_handler_depth == 1) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err == 0) {
|
2013-05-14 15:37:52 -03:00
|
|
|
PyErr_SetNone(PyExc_GeneratorExit);
|
2023-01-24 13:25:37 -04:00
|
|
|
}
|
2020-09-22 02:08:54 -03:00
|
|
|
retval = gen_send_ex(gen, Py_None, 1, 1);
|
2013-05-14 15:37:52 -03:00
|
|
|
if (retval) {
|
2017-11-11 07:06:26 -04:00
|
|
|
const char *msg = "generator ignored GeneratorExit";
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyCoro_CheckExact(gen)) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
msg = "coroutine ignored GeneratorExit";
|
2016-09-09 02:01:51 -03:00
|
|
|
} else if (PyAsyncGen_CheckExact(gen)) {
|
|
|
|
msg = ASYNC_GEN_IGNORED_EXIT_MSG;
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
Py_DECREF(retval);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, msg);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-05-23 17:51:56 -03:00
|
|
|
assert(PyErr_Occurred());
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
|
|
|
|
PyErr_Clear(); /* ignore this error */
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2013-05-14 15:37:52 -03:00
|
|
|
}
|
2023-05-23 17:51:56 -03:00
|
|
|
/* if the generator returned a value while closing, StopIteration was
|
|
|
|
* raised in gen_send_ex() above; retrieve and return the value here */
|
|
|
|
if (_PyGen_FetchStopIterationValue(&retval) == 0) {
|
|
|
|
return retval;
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
PyDoc_STRVAR(throw_doc,
|
2022-03-31 10:56:48 -03:00
|
|
|
"throw(value)\n\
|
|
|
|
throw(type[,value[,tb]])\n\
|
|
|
|
\n\
|
|
|
|
Raise exception in generator, return next yielded value or raise\n\
|
2022-09-30 05:43:02 -03:00
|
|
|
StopIteration.\n\
|
|
|
|
the (type, val, tb) signature is deprecated, \n\
|
|
|
|
and may be removed in a future version of Python.");
|
2005-08-01 21:46:46 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-09-09 02:01:51 -03:00
|
|
|
_gen_throw(PyGenObject *gen, int close_on_genexit,
|
|
|
|
PyObject *typ, PyObject *val, PyObject *tb)
|
2005-08-01 21:46:46 -03:00
|
|
|
{
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf(gen);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
if (yf) {
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
|
2012-01-13 07:43:40 -04:00
|
|
|
PyObject *ret;
|
|
|
|
int err;
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
|
|
|
|
close_on_genexit
|
|
|
|
) {
|
|
|
|
/* Asynchronous generators *should not* be closed right away.
|
|
|
|
We have to allow some awaits to work it through, hence the
|
|
|
|
`close_on_genexit` parameter here.
|
|
|
|
*/
|
2022-03-22 09:57:19 -03:00
|
|
|
PyFrameState state = gen->gi_frame_state;
|
|
|
|
gen->gi_frame_state = FRAME_EXECUTING;
|
2013-05-14 15:37:52 -03:00
|
|
|
err = gen_close_iter(yf);
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = state;
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_DECREF(yf);
|
|
|
|
if (err < 0)
|
2020-09-22 02:08:54 -03:00
|
|
|
return gen_send_ex(gen, Py_None, 1, 0);
|
2012-01-13 07:43:40 -04:00
|
|
|
goto throw_here;
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
|
|
|
|
/* `yf` is a generator or a coroutine. */
|
2020-07-09 10:27:23 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
/* Since we are fast-tracking things by skipping the eval loop,
|
|
|
|
we need to update the current frame so the stack trace
|
|
|
|
will be reported correctly to the user. */
|
|
|
|
/* XXX We should probably be updating the current frame
|
|
|
|
somewhere in ceval.c. */
|
2023-08-17 07:16:03 -03:00
|
|
|
_PyInterpreterFrame *prev = tstate->current_frame;
|
2021-07-26 07:22:16 -03:00
|
|
|
frame->previous = prev;
|
2023-08-17 07:16:03 -03:00
|
|
|
tstate->current_frame = frame;
|
2016-09-09 02:01:51 -03:00
|
|
|
/* Close the generator that we are currently iterating with
|
|
|
|
'yield from' or awaiting on with 'await'. */
|
2022-03-22 09:57:19 -03:00
|
|
|
PyFrameState state = gen->gi_frame_state;
|
|
|
|
gen->gi_frame_state = FRAME_EXECUTING;
|
2016-09-09 02:01:51 -03:00
|
|
|
ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
|
|
|
|
typ, val, tb);
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = state;
|
2023-08-17 07:16:03 -03:00
|
|
|
tstate->current_frame = prev;
|
2021-07-26 07:22:16 -03:00
|
|
|
frame->previous = NULL;
|
2012-01-13 07:43:40 -04:00
|
|
|
} else {
|
2016-09-09 02:01:51 -03:00
|
|
|
/* `yf` is an iterator or a coroutine-like object. */
|
2018-01-25 04:49:40 -04:00
|
|
|
PyObject *meth;
|
2023-07-12 02:57:10 -03:00
|
|
|
if (PyObject_GetOptionalAttr(yf, &_Py_ID(throw), &meth) < 0) {
|
2018-01-25 04:49:40 -04:00
|
|
|
Py_DECREF(yf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-13 07:43:40 -04:00
|
|
|
if (meth == NULL) {
|
|
|
|
Py_DECREF(yf);
|
|
|
|
goto throw_here;
|
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
PyFrameState state = gen->gi_frame_state;
|
|
|
|
gen->gi_frame_state = FRAME_EXECUTING;
|
2016-09-09 02:01:51 -03:00
|
|
|
ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = state;
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_DECREF(meth);
|
|
|
|
}
|
|
|
|
Py_DECREF(yf);
|
|
|
|
if (!ret) {
|
2022-08-19 16:33:44 -03:00
|
|
|
ret = gen_send_ex(gen, Py_None, 1, 0);
|
2012-01-13 07:43:40 -04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw_here:
|
2010-05-09 12:52:27 -03:00
|
|
|
/* First, check the traceback argument, replacing None with
|
|
|
|
NULL. */
|
2011-10-19 17:57:40 -03:00
|
|
|
if (tb == Py_None) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tb = NULL;
|
2011-10-19 17:57:40 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
else if (tb != NULL && !PyTraceBack_Check(tb)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"throw() third argument must be a traceback object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(typ);
|
|
|
|
Py_XINCREF(val);
|
|
|
|
Py_XINCREF(tb);
|
|
|
|
|
2011-10-19 17:57:40 -03:00
|
|
|
if (PyExceptionClass_Check(typ))
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_NormalizeException(&typ, &val, &tb);
|
|
|
|
|
|
|
|
else if (PyExceptionInstance_Check(typ)) {
|
|
|
|
/* Raising an instance. The value should be a dummy. */
|
|
|
|
if (val && val != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"instance exception may not have a separate value");
|
|
|
|
goto failed_throw;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Normalize to raise <class>, <instance> */
|
2022-11-22 08:39:11 -04:00
|
|
|
Py_XSETREF(val, typ);
|
2022-11-16 13:34:24 -04:00
|
|
|
typ = Py_NewRef(PyExceptionInstance_Class(typ));
|
2011-10-18 11:40:50 -03:00
|
|
|
|
2011-10-19 17:57:40 -03:00
|
|
|
if (tb == NULL)
|
2011-10-18 11:40:50 -03:00
|
|
|
/* Returns NULL if there's no traceback */
|
|
|
|
tb = PyException_GetTraceback(val);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Not something you can raise. throw() fails. */
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"exceptions must be classes or instances "
|
|
|
|
"deriving from BaseException, not %s",
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_TYPE(typ)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
goto failed_throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Restore(typ, val, tb);
|
2020-09-22 02:08:54 -03:00
|
|
|
return gen_send_ex(gen, Py_None, 1, 0);
|
2005-08-01 21:46:46 -03:00
|
|
|
|
|
|
|
failed_throw:
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Didn't use our arguments, so restore their original refcounts */
|
|
|
|
Py_DECREF(typ);
|
|
|
|
Py_XDECREF(val);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
return NULL;
|
2005-08-01 21:46:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
static PyObject *
|
2022-03-11 10:37:14 -04:00
|
|
|
gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs)
|
2016-09-09 02:01:51 -03:00
|
|
|
{
|
|
|
|
PyObject *typ;
|
|
|
|
PyObject *tb = NULL;
|
|
|
|
PyObject *val = NULL;
|
|
|
|
|
2022-03-11 10:37:14 -04:00
|
|
|
if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) {
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-09-30 05:43:02 -03:00
|
|
|
if (nargs > 1) {
|
|
|
|
if (PyErr_WarnEx(PyExc_DeprecationWarning,
|
|
|
|
"the (type, exc, tb) signature of throw() is deprecated, "
|
|
|
|
"use the single-arg signature instead.",
|
|
|
|
1) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 10:37:14 -04:00
|
|
|
typ = args[0];
|
|
|
|
if (nargs == 3) {
|
|
|
|
val = args[1];
|
|
|
|
tb = args[2];
|
|
|
|
}
|
|
|
|
else if (nargs == 2) {
|
|
|
|
val = args[1];
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
return _gen_throw(gen, 1, typ, val, tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_iternext(PyGenObject *gen)
|
|
|
|
{
|
2020-09-22 02:08:54 -03:00
|
|
|
PyObject *result;
|
|
|
|
assert(PyGen_CheckExact(gen) || PyCoro_CheckExact(gen));
|
|
|
|
if (gen_send_ex2(gen, NULL, &result, 0, 0) == PYGEN_RETURN) {
|
|
|
|
if (result != Py_None) {
|
|
|
|
_PyGen_SetStopIterationValue(result);
|
|
|
|
}
|
|
|
|
Py_CLEAR(result);
|
|
|
|
}
|
|
|
|
return result;
|
2005-08-01 21:46:46 -03:00
|
|
|
}
|
|
|
|
|
2016-11-06 12:44:42 -04:00
|
|
|
/*
|
|
|
|
* Set StopIteration with specified value. Value can be arbitrary object
|
|
|
|
* or NULL.
|
|
|
|
*
|
|
|
|
* Returns 0 if StopIteration is set and -1 if any other exception is set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_PyGen_SetStopIterationValue(PyObject *value)
|
|
|
|
{
|
|
|
|
PyObject *e;
|
|
|
|
|
|
|
|
if (value == NULL ||
|
2017-03-12 16:53:07 -03:00
|
|
|
(!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
|
2016-11-06 12:44:42 -04:00
|
|
|
{
|
|
|
|
/* Delay exception instantiation if we can */
|
|
|
|
PyErr_SetObject(PyExc_StopIteration, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Construct an exception instance manually with
|
2020-02-11 12:46:57 -04:00
|
|
|
* PyObject_CallOneArg and pass it to PyErr_SetObject.
|
2016-11-06 12:44:42 -04:00
|
|
|
*
|
|
|
|
* We do this to handle a situation when "value" is a tuple, in which
|
|
|
|
* case PyErr_SetObject would set the value of StopIteration to
|
|
|
|
* the first element of the tuple.
|
|
|
|
*
|
|
|
|
* (See PyErr_SetObject/_PyErr_CreateException code for details.)
|
|
|
|
*/
|
2020-02-11 12:46:57 -04:00
|
|
|
e = PyObject_CallOneArg(PyExc_StopIteration, value);
|
2016-11-06 12:44:42 -04:00
|
|
|
if (e == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PyErr_SetObject(PyExc_StopIteration, e);
|
|
|
|
Py_DECREF(e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
/*
|
|
|
|
* If StopIteration exception is set, fetches its 'value'
|
|
|
|
* attribute if any, otherwise sets pvalue to None.
|
|
|
|
*
|
|
|
|
* Returns 0 if no exception or StopIteration is set.
|
|
|
|
* If any other exception is set, returns -1 and leaves
|
|
|
|
* pvalue unchanged.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2016-11-06 12:44:42 -04:00
|
|
|
_PyGen_FetchStopIterationValue(PyObject **pvalue)
|
|
|
|
{
|
2012-01-13 07:43:40 -04:00
|
|
|
PyObject *value = NULL;
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
|
2023-03-08 13:03:18 -04:00
|
|
|
PyObject *exc = PyErr_GetRaisedException();
|
|
|
|
value = Py_NewRef(((PyStopIterationObject *)exc)->value);
|
|
|
|
Py_DECREF(exc);
|
2012-01-13 07:43:40 -04:00
|
|
|
} else if (PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == NULL) {
|
2022-11-10 17:22:02 -04:00
|
|
|
value = Py_NewRef(Py_None);
|
2012-01-13 07:43:40 -04:00
|
|
|
}
|
|
|
|
*pvalue = value;
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-01 21:46:46 -03:00
|
|
|
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_repr(PyGenObject *gen)
|
|
|
|
{
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
return PyUnicode_FromFormat("<generator object %S at %p>",
|
|
|
|
gen->gi_qualname, gen);
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
}
|
|
|
|
|
2014-06-16 10:59:28 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored))
|
2014-06-16 10:59:28 -03:00
|
|
|
{
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->gi_name);
|
2014-06-16 10:59:28 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2014-06-16 10:59:28 -03:00
|
|
|
{
|
|
|
|
/* Not legal to del gen.gi_name or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__name__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->gi_name, Py_NewRef(value));
|
2014-06-16 10:59:28 -03:00
|
|
|
return 0;
|
|
|
|
}
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored))
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
{
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->gi_qualname);
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
}
|
|
|
|
|
2014-06-16 10:59:28 -03:00
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2014-06-16 10:59:28 -03:00
|
|
|
{
|
|
|
|
/* Not legal to del gen.__qualname__ or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__qualname__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->gi_qualname, Py_NewRef(value));
|
2014-06-16 10:59:28 -03:00
|
|
|
return 0;
|
|
|
|
}
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
|
2015-07-03 01:23:30 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
gen_getyieldfrom(PyGenObject *gen, void *Py_UNUSED(ignored))
|
2015-07-03 01:23:30 -03:00
|
|
|
{
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf(gen);
|
2015-07-03 01:23:30 -03:00
|
|
|
if (yf == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
return yf;
|
|
|
|
}
|
|
|
|
|
2020-07-17 07:44:23 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
gen_getrunning(PyGenObject *gen, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state == FRAME_EXECUTING) {
|
|
|
|
Py_RETURN_TRUE;
|
2020-07-17 07:44:23 -03:00
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
Py_RETURN_FALSE;
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
|
|
|
|
2022-01-20 07:46:39 -04:00
|
|
|
static PyObject *
|
|
|
|
gen_getsuspended(PyGenObject *gen, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2022-03-22 09:57:19 -03:00
|
|
|
return PyBool_FromLong(gen->gi_frame_state == FRAME_SUSPENDED);
|
2022-01-20 07:46:39 -04:00
|
|
|
}
|
|
|
|
|
2021-07-26 07:22:16 -03:00
|
|
|
static PyObject *
|
|
|
|
_gen_getframe(PyGenObject *gen, const char *const name)
|
|
|
|
{
|
|
|
|
if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state == FRAME_CLEARED) {
|
2021-07-26 07:22:16 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2022-02-25 11:22:00 -04:00
|
|
|
return _Py_XNewRef((PyObject *)_PyFrame_GetFrameObject((_PyInterpreterFrame *)gen->gi_iframe));
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
gen_getframe(PyGenObject *gen, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
return _gen_getframe(gen, "gi_frame");
|
2020-07-17 07:44:23 -03:00
|
|
|
}
|
|
|
|
|
2023-02-23 06:19:01 -04:00
|
|
|
static PyObject *
|
|
|
|
_gen_getcode(PyGenObject *gen, const char *const name)
|
|
|
|
{
|
|
|
|
if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return Py_NewRef(_PyGen_GetCode(gen));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
gen_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
return _gen_getcode(gen, "gi_code");
|
|
|
|
}
|
|
|
|
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
static PyGetSetDef gen_getsetlist[] = {
|
2014-06-16 10:59:28 -03:00
|
|
|
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
|
|
|
|
PyDoc_STR("name of the generator")},
|
|
|
|
{"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
|
|
|
|
PyDoc_STR("qualified name of the generator")},
|
2015-07-03 01:23:30 -03:00
|
|
|
{"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
|
|
|
|
PyDoc_STR("object being iterated by yield from, or None")},
|
2020-07-17 07:44:23 -03:00
|
|
|
{"gi_running", (getter)gen_getrunning, NULL, NULL},
|
2021-07-26 07:22:16 -03:00
|
|
|
{"gi_frame", (getter)gen_getframe, NULL, NULL},
|
2022-01-20 07:46:39 -04:00
|
|
|
{"gi_suspended", (getter)gen_getsuspended, NULL, NULL},
|
2023-02-23 06:19:01 -04:00
|
|
|
{"gi_code", (getter)gen_getcode, NULL, NULL},
|
2014-06-16 10:59:28 -03:00
|
|
|
{NULL} /* Sentinel */
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
};
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
static PyMemberDef gen_memberlist[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
2004-06-01 12:22:42 -03:00
|
|
|
};
|
|
|
|
|
2021-12-06 06:13:49 -04:00
|
|
|
static PyObject *
|
|
|
|
gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
Py_ssize_t res;
|
2022-02-25 11:22:00 -04:00
|
|
|
res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus);
|
2023-02-23 06:19:01 -04:00
|
|
|
PyCodeObject *code = _PyGen_GetCode(gen);
|
2023-01-04 09:37:06 -04:00
|
|
|
res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *);
|
2021-12-06 06:13:49 -04:00
|
|
|
return PyLong_FromSsize_t(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(sizeof__doc__,
|
|
|
|
"gen.__sizeof__() -> size of gen in memory, in bytes");
|
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
static PyMethodDef gen_methods[] = {
|
2020-09-22 02:08:54 -03:00
|
|
|
{"send",(PyCFunction)gen_send, METH_O, send_doc},
|
2022-05-03 16:42:14 -03:00
|
|
|
{"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
|
2021-12-06 06:13:49 -04:00
|
|
|
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
2005-08-01 21:46:46 -03:00
|
|
|
};
|
|
|
|
|
2020-11-10 16:09:55 -04:00
|
|
|
static PyAsyncMethods gen_as_async = {
|
|
|
|
0, /* am_await */
|
|
|
|
0, /* am_aiter */
|
|
|
|
0, /* am_anext */
|
|
|
|
(sendfunc)PyGen_am_send, /* am_send */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
PyTypeObject PyGen_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"generator", /* tp_name */
|
2021-12-06 06:13:49 -04:00
|
|
|
offsetof(PyGenObject, gi_iframe) +
|
2022-02-25 11:22:00 -04:00
|
|
|
offsetof(_PyInterpreterFrame, localsplus), /* tp_basicsize */
|
2021-12-06 06:13:49 -04:00
|
|
|
sizeof(PyObject *), /* tp_itemsize */
|
2010-05-09 12:52:27 -03:00
|
|
|
/* methods */
|
|
|
|
(destructor)gen_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2020-11-10 16:09:55 -04:00
|
|
|
&gen_as_async, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(reprfunc)gen_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2021-07-23 10:21:11 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)gen_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyObject_SelfIter, /* tp_iter */
|
2010-05-09 12:52:27 -03:00
|
|
|
(iternextfunc)gen_iternext, /* tp_iternext */
|
|
|
|
gen_methods, /* tp_methods */
|
|
|
|
gen_memberlist, /* tp_members */
|
|
|
|
gen_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
|
|
|
0, /* tp_free */
|
|
|
|
0, /* tp_is_gc */
|
|
|
|
0, /* tp_bases */
|
|
|
|
0, /* tp_mro */
|
|
|
|
0, /* tp_cache */
|
|
|
|
0, /* tp_subclasses */
|
|
|
|
0, /* tp_weaklist */
|
2013-07-30 14:59:21 -03:00
|
|
|
0, /* tp_del */
|
|
|
|
0, /* tp_version_tag */
|
2013-08-05 18:26:40 -03:00
|
|
|
_PyGen_Finalize, /* tp_finalize */
|
2004-06-01 12:22:42 -03:00
|
|
|
};
|
|
|
|
|
2021-07-26 07:22:16 -03:00
|
|
|
static PyObject *
|
2021-12-06 06:13:49 -04:00
|
|
|
make_gen(PyTypeObject *type, PyFunctionObject *func)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
2021-12-06 06:13:49 -04:00
|
|
|
PyCodeObject *code = (PyCodeObject *)func->func_code;
|
2023-01-04 09:37:06 -04:00
|
|
|
int slots = _PyFrame_NumSlotsForCodeObject(code);
|
2021-12-06 06:13:49 -04:00
|
|
|
PyGenObject *gen = PyObject_GC_NewVar(PyGenObject, type, slots);
|
2021-07-26 07:22:16 -03:00
|
|
|
if (gen == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = FRAME_CLEARED;
|
2021-07-26 07:22:16 -03:00
|
|
|
gen->gi_weakreflist = NULL;
|
|
|
|
gen->gi_exc_state.exc_value = NULL;
|
|
|
|
gen->gi_exc_state.previous_item = NULL;
|
2022-01-20 07:46:39 -04:00
|
|
|
assert(func->func_name != NULL);
|
|
|
|
gen->gi_name = Py_NewRef(func->func_name);
|
|
|
|
assert(func->func_qualname != NULL);
|
|
|
|
gen->gi_qualname = Py_NewRef(func->func_qualname);
|
2021-07-26 07:22:16 -03:00
|
|
|
_PyObject_GC_TRACK(gen);
|
|
|
|
return (PyObject *)gen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2022-02-25 11:22:00 -04:00
|
|
|
compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame);
|
2021-07-26 07:22:16 -03:00
|
|
|
|
|
|
|
PyObject *
|
2021-12-06 06:13:49 -04:00
|
|
|
_Py_MakeCoro(PyFunctionObject *func)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
2021-11-23 05:53:24 -04:00
|
|
|
int coro_flags = ((PyCodeObject *)func->func_code)->co_flags &
|
2021-07-26 07:22:16 -03:00
|
|
|
(CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR);
|
|
|
|
assert(coro_flags);
|
|
|
|
if (coro_flags == CO_GENERATOR) {
|
2021-12-06 06:13:49 -04:00
|
|
|
return make_gen(&PyGen_Type, func);
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
|
|
|
if (coro_flags == CO_ASYNC_GENERATOR) {
|
|
|
|
PyAsyncGenObject *o;
|
2021-12-06 06:13:49 -04:00
|
|
|
o = (PyAsyncGenObject *)make_gen(&PyAsyncGen_Type, func);
|
2021-07-26 07:22:16 -03:00
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-12-06 06:13:49 -04:00
|
|
|
o->ag_origin_or_finalizer = NULL;
|
2021-07-26 07:22:16 -03:00
|
|
|
o->ag_closed = 0;
|
|
|
|
o->ag_hooks_inited = 0;
|
|
|
|
o->ag_running_async = 0;
|
|
|
|
return (PyObject*)o;
|
|
|
|
}
|
|
|
|
assert (coro_flags == CO_COROUTINE);
|
2021-12-06 06:13:49 -04:00
|
|
|
PyObject *coro = make_gen(&PyCoro_Type, func);
|
2021-07-26 07:22:16 -03:00
|
|
|
if (!coro) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
int origin_depth = tstate->coroutine_origin_tracking_depth;
|
|
|
|
|
|
|
|
if (origin_depth == 0) {
|
2021-12-06 06:13:49 -04:00
|
|
|
((PyCoroObject *)coro)->cr_origin_or_finalizer = NULL;
|
2021-07-26 07:22:16 -03:00
|
|
|
} else {
|
2023-08-17 07:16:03 -03:00
|
|
|
_PyInterpreterFrame *frame = tstate->current_frame;
|
2023-01-09 16:20:04 -04:00
|
|
|
assert(frame);
|
|
|
|
assert(_PyFrame_IsIncomplete(frame));
|
|
|
|
frame = _PyFrame_GetFirstComplete(frame->previous);
|
|
|
|
PyObject *cr_origin = compute_cr_origin(origin_depth, frame);
|
2021-12-06 06:13:49 -04:00
|
|
|
((PyCoroObject *)coro)->cr_origin_or_finalizer = cr_origin;
|
2021-07-26 07:22:16 -03:00
|
|
|
if (!cr_origin) {
|
|
|
|
Py_DECREF(coro);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return coro;
|
|
|
|
}
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
|
|
|
|
PyObject *name, PyObject *qualname)
|
2004-06-01 12:22:42 -03:00
|
|
|
{
|
2023-06-14 09:46:37 -03:00
|
|
|
PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
|
2021-12-06 06:13:49 -04:00
|
|
|
int size = code->co_nlocalsplus + code->co_stacksize;
|
|
|
|
PyGenObject *gen = PyObject_GC_NewVar(PyGenObject, type, size);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (gen == NULL) {
|
|
|
|
Py_DECREF(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-12-06 06:13:49 -04:00
|
|
|
/* Copy the frame */
|
2021-07-26 07:22:16 -03:00
|
|
|
assert(f->f_frame->frame_obj == NULL);
|
2022-03-22 09:57:19 -03:00
|
|
|
assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
|
|
|
|
_PyFrame_Copy((_PyInterpreterFrame *)f->_f_frame_data, frame);
|
2022-03-22 09:57:19 -03:00
|
|
|
gen->gi_frame_state = FRAME_CREATED;
|
2021-12-06 06:13:49 -04:00
|
|
|
assert(frame->frame_obj == f);
|
|
|
|
f->f_frame = frame;
|
2022-03-22 09:57:19 -03:00
|
|
|
frame->owner = FRAME_OWNED_BY_GENERATOR;
|
2021-07-26 07:22:16 -03:00
|
|
|
assert(PyObject_GC_IsTracked((PyObject *)f));
|
2021-12-06 06:13:49 -04:00
|
|
|
Py_DECREF(f);
|
2010-05-09 12:52:27 -03:00
|
|
|
gen->gi_weakreflist = NULL;
|
2017-10-22 18:41:51 -03:00
|
|
|
gen->gi_exc_state.exc_value = NULL;
|
|
|
|
gen->gi_exc_state.previous_item = NULL;
|
2014-06-16 10:59:28 -03:00
|
|
|
if (name != NULL)
|
2022-11-10 17:22:02 -04:00
|
|
|
gen->gi_name = Py_NewRef(name);
|
2014-06-16 10:59:28 -03:00
|
|
|
else
|
2023-02-23 06:19:01 -04:00
|
|
|
gen->gi_name = Py_NewRef(_PyGen_GetCode(gen)->co_name);
|
2014-06-16 10:59:28 -03:00
|
|
|
if (qualname != NULL)
|
2022-11-10 17:22:02 -04:00
|
|
|
gen->gi_qualname = Py_NewRef(qualname);
|
2014-06-16 10:59:28 -03:00
|
|
|
else
|
2023-02-23 06:19:01 -04:00
|
|
|
gen->gi_qualname = Py_NewRef(_PyGen_GetCode(gen)->co_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(gen);
|
|
|
|
return (PyObject *)gen;
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyObject *
|
|
|
|
PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
|
|
|
|
{
|
|
|
|
return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
|
|
|
|
}
|
|
|
|
|
2014-06-16 10:59:28 -03:00
|
|
|
PyObject *
|
|
|
|
PyGen_New(PyFrameObject *f)
|
|
|
|
{
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
|
2014-06-16 10:59:28 -03:00
|
|
|
}
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
/* Coroutine Object */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyCoroObject *cw_coroutine;
|
|
|
|
} PyCoroWrapper;
|
|
|
|
|
|
|
|
static int
|
|
|
|
gen_is_coroutine(PyObject *o)
|
|
|
|
{
|
|
|
|
if (PyGen_CheckExact(o)) {
|
2023-02-23 06:19:01 -04:00
|
|
|
PyCodeObject *code = _PyGen_GetCode((PyGenObject*)o);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
if (code->co_flags & CO_ITERABLE_COROUTINE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:57:16 -03:00
|
|
|
/*
|
|
|
|
* This helper function returns an awaitable for `o`:
|
|
|
|
* - `o` if `o` is a coroutine-object;
|
|
|
|
* - `type(o)->tp_as_async->am_await(o)`
|
|
|
|
*
|
|
|
|
* Raises a TypeError if it's not possible to return
|
|
|
|
* an awaitable and returns NULL.
|
|
|
|
*/
|
|
|
|
PyObject *
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
_PyCoro_GetAwaitableIter(PyObject *o)
|
2015-05-11 23:57:16 -03:00
|
|
|
{
|
2015-05-28 12:21:31 -03:00
|
|
|
unaryfunc getter = NULL;
|
2015-05-11 23:57:16 -03:00
|
|
|
PyTypeObject *ot;
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
|
|
|
|
/* 'o' is a coroutine. */
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(o);
|
2015-05-11 23:57:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
ot = Py_TYPE(o);
|
|
|
|
if (ot->tp_as_async != NULL) {
|
|
|
|
getter = ot->tp_as_async->am_await;
|
|
|
|
}
|
|
|
|
if (getter != NULL) {
|
|
|
|
PyObject *res = (*getter)(o);
|
|
|
|
if (res != NULL) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
|
|
|
|
/* __await__ must return an *iterator*, not
|
|
|
|
a coroutine or another awaitable (see PEP 492) */
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__await__() returned a coroutine");
|
|
|
|
Py_CLEAR(res);
|
|
|
|
} else if (!PyIter_Check(res)) {
|
2015-05-11 23:57:16 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__await__() returned non-iterator "
|
|
|
|
"of type '%.100s'",
|
|
|
|
Py_TYPE(res)->tp_name);
|
|
|
|
Py_CLEAR(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"object %.100s can't be used in 'await' expression",
|
|
|
|
ot->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_repr(PyCoroObject *coro)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromFormat("<coroutine object %S at %p>",
|
|
|
|
coro->cr_qualname, coro);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_await(PyCoroObject *coro)
|
|
|
|
{
|
|
|
|
PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
|
|
|
|
if (cw == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
cw->cw_coroutine = (PyCoroObject*)Py_NewRef(coro);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
_PyObject_GC_TRACK(cw);
|
|
|
|
return (PyObject *)cw;
|
|
|
|
}
|
|
|
|
|
2015-07-03 01:23:30 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
coro_get_cr_await(PyCoroObject *coro, void *Py_UNUSED(ignored))
|
2015-07-03 01:23:30 -03:00
|
|
|
{
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf((PyGenObject *) coro);
|
2015-07-03 01:23:30 -03:00
|
|
|
if (yf == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
return yf;
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:46:39 -04:00
|
|
|
static PyObject *
|
|
|
|
cr_getsuspended(PyCoroObject *coro, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2022-03-22 09:57:19 -03:00
|
|
|
if (coro->cr_frame_state == FRAME_SUSPENDED) {
|
|
|
|
Py_RETURN_TRUE;
|
2022-01-20 07:46:39 -04:00
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
Py_RETURN_FALSE;
|
2022-01-20 07:46:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 07:44:23 -03:00
|
|
|
static PyObject *
|
|
|
|
cr_getrunning(PyCoroObject *coro, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2022-03-22 09:57:19 -03:00
|
|
|
if (coro->cr_frame_state == FRAME_EXECUTING) {
|
|
|
|
Py_RETURN_TRUE;
|
2020-07-17 07:44:23 -03:00
|
|
|
}
|
2022-03-22 09:57:19 -03:00
|
|
|
Py_RETURN_FALSE;
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cr_getframe(PyCoroObject *coro, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
return _gen_getframe((PyGenObject *)coro, "cr_frame");
|
2020-07-17 07:44:23 -03:00
|
|
|
}
|
|
|
|
|
2023-02-23 06:19:01 -04:00
|
|
|
static PyObject *
|
|
|
|
cr_getcode(PyCoroObject *coro, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
return _gen_getcode((PyGenObject *)coro, "cr_code");
|
|
|
|
}
|
|
|
|
|
2021-07-26 07:22:16 -03:00
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
static PyGetSetDef coro_getsetlist[] = {
|
|
|
|
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
|
|
|
|
PyDoc_STR("name of the coroutine")},
|
|
|
|
{"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
|
|
|
|
PyDoc_STR("qualified name of the coroutine")},
|
2015-07-03 01:23:30 -03:00
|
|
|
{"cr_await", (getter)coro_get_cr_await, NULL,
|
|
|
|
PyDoc_STR("object being awaited on, or None")},
|
2020-07-17 07:44:23 -03:00
|
|
|
{"cr_running", (getter)cr_getrunning, NULL, NULL},
|
2021-07-26 07:22:16 -03:00
|
|
|
{"cr_frame", (getter)cr_getframe, NULL, NULL},
|
2023-02-23 06:19:01 -04:00
|
|
|
{"cr_code", (getter)cr_getcode, NULL, NULL},
|
2022-01-20 07:46:39 -04:00
|
|
|
{"cr_suspended", (getter)cr_getsuspended, NULL, NULL},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMemberDef coro_memberlist[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"cr_origin", _Py_T_OBJECT, offsetof(PyCoroObject, cr_origin_or_finalizer), Py_READONLY},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(coro_send_doc,
|
|
|
|
"send(arg) -> send 'arg' into coroutine,\n\
|
2015-06-24 12:04:15 -03:00
|
|
|
return next iterated value or raise StopIteration.");
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(coro_throw_doc,
|
2022-03-31 10:56:48 -03:00
|
|
|
"throw(value)\n\
|
|
|
|
throw(type[,value[,traceback]])\n\
|
|
|
|
\n\
|
|
|
|
Raise exception in coroutine, return next iterated value or raise\n\
|
2022-09-30 05:43:02 -03:00
|
|
|
StopIteration.\n\
|
|
|
|
the (type, val, tb) signature is deprecated, \n\
|
|
|
|
and may be removed in a future version of Python.");
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(coro_close_doc,
|
|
|
|
"close() -> raise GeneratorExit inside coroutine.");
|
|
|
|
|
|
|
|
static PyMethodDef coro_methods[] = {
|
2020-10-09 21:15:15 -03:00
|
|
|
{"send",(PyCFunction)gen_send, METH_O, coro_send_doc},
|
2022-05-03 16:42:14 -03:00
|
|
|
{"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
|
2021-12-06 06:13:49 -04:00
|
|
|
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyAsyncMethods coro_as_async = {
|
|
|
|
(unaryfunc)coro_await, /* am_await */
|
|
|
|
0, /* am_aiter */
|
2020-11-10 16:09:55 -04:00
|
|
|
0, /* am_anext */
|
|
|
|
(sendfunc)PyGen_am_send, /* am_send */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject PyCoro_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"coroutine", /* tp_name */
|
2021-12-06 06:13:49 -04:00
|
|
|
offsetof(PyCoroObject, cr_iframe) +
|
2022-02-25 11:22:00 -04:00
|
|
|
offsetof(_PyInterpreterFrame, localsplus), /* tp_basicsize */
|
2021-12-06 06:13:49 -04:00
|
|
|
sizeof(PyObject *), /* tp_itemsize */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
/* methods */
|
|
|
|
(destructor)gen_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
&coro_as_async, /* tp_as_async */
|
|
|
|
(reprfunc)coro_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2021-07-23 10:21:11 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)gen_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
coro_methods, /* tp_methods */
|
|
|
|
coro_memberlist, /* tp_members */
|
|
|
|
coro_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
|
|
|
0, /* tp_free */
|
|
|
|
0, /* tp_is_gc */
|
|
|
|
0, /* tp_bases */
|
|
|
|
0, /* tp_mro */
|
|
|
|
0, /* tp_cache */
|
|
|
|
0, /* tp_subclasses */
|
|
|
|
0, /* tp_weaklist */
|
|
|
|
0, /* tp_del */
|
|
|
|
0, /* tp_version_tag */
|
|
|
|
_PyGen_Finalize, /* tp_finalize */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
coro_wrapper_dealloc(PyCoroWrapper *cw)
|
|
|
|
{
|
|
|
|
_PyObject_GC_UNTRACK((PyObject *)cw);
|
|
|
|
Py_CLEAR(cw->cw_coroutine);
|
|
|
|
PyObject_GC_Del(cw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_iternext(PyCoroWrapper *cw)
|
|
|
|
{
|
2020-09-22 02:08:54 -03:00
|
|
|
return gen_iternext((PyGenObject *)cw->cw_coroutine);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
|
|
|
|
{
|
2020-09-22 02:08:54 -03:00
|
|
|
return gen_send((PyGenObject *)cw->cw_coroutine, arg);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2022-03-11 10:37:14 -04:00
|
|
|
coro_wrapper_throw(PyCoroWrapper *cw, PyObject *const *args, Py_ssize_t nargs)
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{
|
2022-03-11 10:37:14 -04:00
|
|
|
return gen_throw((PyGenObject *)cw->cw_coroutine, args, nargs);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
|
|
|
|
{
|
|
|
|
return gen_close((PyGenObject *)cw->cw_coroutine, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT((PyObject *)cw->cw_coroutine);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef coro_wrapper_methods[] = {
|
2015-06-24 12:04:15 -03:00
|
|
|
{"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
|
2022-05-03 16:42:14 -03:00
|
|
|
{"throw",_PyCFunction_CAST(coro_wrapper_throw),
|
2022-03-11 10:37:14 -04:00
|
|
|
METH_FASTCALL, coro_throw_doc},
|
2015-06-24 12:04:15 -03:00
|
|
|
{"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject _PyCoroWrapper_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"coroutine_wrapper",
|
|
|
|
sizeof(PyCoroWrapper), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
(destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_as_async */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
|
|
|
"A wrapper object implementing __await__ for coroutines.",
|
|
|
|
(traverseproc)coro_wrapper_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)coro_wrapper_iternext, /* tp_iternext */
|
|
|
|
coro_wrapper_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
2016-11-08 20:19:28 -04:00
|
|
|
0, /* tp_free */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
};
|
|
|
|
|
2018-01-21 10:44:07 -04:00
|
|
|
static PyObject *
|
2022-02-25 11:22:00 -04:00
|
|
|
compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame)
|
2018-01-21 10:44:07 -04:00
|
|
|
{
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyInterpreterFrame *frame = current_frame;
|
2018-01-21 10:44:07 -04:00
|
|
|
/* First count how many frames we have */
|
|
|
|
int frame_count = 0;
|
|
|
|
for (; frame && frame_count < origin_depth; ++frame_count) {
|
2023-01-09 16:20:04 -04:00
|
|
|
frame = _PyFrame_GetFirstComplete(frame->previous);
|
2018-01-21 10:44:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now collect them */
|
|
|
|
PyObject *cr_origin = PyTuple_New(frame_count);
|
2018-08-25 04:15:23 -03:00
|
|
|
if (cr_origin == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-01-20 07:46:39 -04:00
|
|
|
frame = current_frame;
|
2018-01-21 10:44:07 -04:00
|
|
|
for (int i = 0; i < frame_count; ++i) {
|
2023-06-14 09:46:37 -03:00
|
|
|
PyCodeObject *code = _PyFrame_GetCode(frame);
|
2023-05-05 13:53:07 -03:00
|
|
|
int line = PyUnstable_InterpreterFrame_GetLine(frame);
|
2022-04-07 16:31:01 -03:00
|
|
|
PyObject *frameinfo = Py_BuildValue("OiO", code->co_filename, line,
|
2020-04-28 19:56:58 -03:00
|
|
|
code->co_name);
|
2018-01-21 10:44:07 -04:00
|
|
|
if (!frameinfo) {
|
|
|
|
Py_DECREF(cr_origin);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(cr_origin, i, frameinfo);
|
2023-01-09 16:20:04 -04:00
|
|
|
frame = _PyFrame_GetFirstComplete(frame->previous);
|
2018-01-21 10:44:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return cr_origin;
|
|
|
|
}
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyObject *
|
|
|
|
PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
|
|
|
|
{
|
2018-01-21 10:44:07 -04:00
|
|
|
PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
|
|
|
|
if (!coro) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-31 21:51:40 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2018-01-21 10:44:07 -04:00
|
|
|
int origin_depth = tstate->coroutine_origin_tracking_depth;
|
|
|
|
|
|
|
|
if (origin_depth == 0) {
|
2021-12-06 06:13:49 -04:00
|
|
|
((PyCoroObject *)coro)->cr_origin_or_finalizer = NULL;
|
2018-01-21 10:44:07 -04:00
|
|
|
} else {
|
2022-01-20 07:46:39 -04:00
|
|
|
PyObject *cr_origin = compute_cr_origin(origin_depth, _PyEval_GetFrame());
|
2021-12-06 06:13:49 -04:00
|
|
|
((PyCoroObject *)coro)->cr_origin_or_finalizer = cr_origin;
|
2018-01-21 10:44:07 -04:00
|
|
|
if (!cr_origin) {
|
|
|
|
Py_DECREF(coro);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return coro;
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
}
|
2016-06-09 16:08:31 -03:00
|
|
|
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
/* ========= Asynchronous Generators ========= */
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
AWAITABLE_STATE_INIT, /* new awaitable, has not yet been iterated */
|
|
|
|
AWAITABLE_STATE_ITER, /* being iterated */
|
|
|
|
AWAITABLE_STATE_CLOSED, /* closed */
|
|
|
|
} AwaitableState;
|
|
|
|
|
|
|
|
|
2020-06-04 21:34:14 -03:00
|
|
|
typedef struct PyAsyncGenASend {
|
2016-09-09 02:01:51 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyAsyncGenObject *ags_gen;
|
|
|
|
|
|
|
|
/* Can be NULL, when in the __anext__() mode
|
|
|
|
(equivalent of "asend(None)") */
|
|
|
|
PyObject *ags_sendval;
|
|
|
|
|
|
|
|
AwaitableState ags_state;
|
|
|
|
} PyAsyncGenASend;
|
|
|
|
|
|
|
|
|
2020-06-04 21:34:14 -03:00
|
|
|
typedef struct PyAsyncGenAThrow {
|
2016-09-09 02:01:51 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyAsyncGenObject *agt_gen;
|
|
|
|
|
|
|
|
/* Can be NULL, when in the "aclose()" mode
|
|
|
|
(equivalent of "athrow(GeneratorExit)") */
|
|
|
|
PyObject *agt_args;
|
|
|
|
|
|
|
|
AwaitableState agt_state;
|
|
|
|
} PyAsyncGenAThrow;
|
|
|
|
|
|
|
|
|
2020-06-04 21:34:14 -03:00
|
|
|
typedef struct _PyAsyncGenWrappedValue {
|
2016-09-09 02:01:51 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *agw_val;
|
|
|
|
} _PyAsyncGenWrappedValue;
|
|
|
|
|
|
|
|
|
|
|
|
#define _PyAsyncGenWrappedValue_CheckExact(o) \
|
2020-03-04 09:15:20 -04:00
|
|
|
Py_IS_TYPE(o, &_PyAsyncGenWrappedValue_Type)
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg)
|
|
|
|
{
|
2021-12-06 06:13:49 -04:00
|
|
|
Py_VISIT(gen->ag_origin_or_finalizer);
|
2016-09-09 02:01:51 -03:00
|
|
|
return gen_traverse((PyGenObject*)gen, visit, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_repr(PyAsyncGenObject *o)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromFormat("<async_generator object %S at %p>",
|
|
|
|
o->ag_qualname, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
async_gen_init_hooks(PyAsyncGenObject *o)
|
|
|
|
{
|
|
|
|
PyThreadState *tstate;
|
|
|
|
PyObject *finalizer;
|
|
|
|
PyObject *firstiter;
|
|
|
|
|
|
|
|
if (o->ag_hooks_inited) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
o->ag_hooks_inited = 1;
|
|
|
|
|
2018-10-31 21:51:40 -03:00
|
|
|
tstate = _PyThreadState_GET();
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
finalizer = tstate->async_gen_finalizer;
|
|
|
|
if (finalizer) {
|
2022-11-10 17:22:02 -04:00
|
|
|
o->ag_origin_or_finalizer = Py_NewRef(finalizer);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
firstiter = tstate->async_gen_firstiter;
|
|
|
|
if (firstiter) {
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
Py_INCREF(firstiter);
|
2020-02-11 12:46:57 -04:00
|
|
|
res = PyObject_CallOneArg(firstiter, (PyObject *)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
Py_DECREF(firstiter);
|
|
|
|
if (res == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_anext(PyAsyncGenObject *o)
|
|
|
|
{
|
|
|
|
if (async_gen_init_hooks(o)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return async_gen_asend_new(o, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_asend(PyAsyncGenObject *o, PyObject *arg)
|
|
|
|
{
|
|
|
|
if (async_gen_init_hooks(o)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return async_gen_asend_new(o, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_aclose(PyAsyncGenObject *o, PyObject *arg)
|
|
|
|
{
|
|
|
|
if (async_gen_init_hooks(o)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return async_gen_athrow_new(o, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
|
|
|
|
{
|
2022-09-30 05:43:02 -03:00
|
|
|
if (PyTuple_GET_SIZE(args) > 1) {
|
|
|
|
if (PyErr_WarnEx(PyExc_DeprecationWarning,
|
|
|
|
"the (type, exc, tb) signature of athrow() is deprecated, "
|
|
|
|
"use the single-arg signature instead.",
|
|
|
|
1) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
if (async_gen_init_hooks(o)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return async_gen_athrow_new(o, args);
|
|
|
|
}
|
|
|
|
|
2021-07-26 07:22:16 -03:00
|
|
|
static PyObject *
|
|
|
|
ag_getframe(PyAsyncGenObject *ag, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
return _gen_getframe((PyGenObject *)ag, "ag_frame");
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
|
2023-02-23 06:19:01 -04:00
|
|
|
static PyObject *
|
|
|
|
ag_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2023-03-07 23:39:50 -04:00
|
|
|
return _gen_getcode(gen, "ag_code");
|
2023-02-23 06:19:01 -04:00
|
|
|
}
|
|
|
|
|
2023-03-11 09:19:40 -04:00
|
|
|
static PyObject *
|
|
|
|
ag_getsuspended(PyAsyncGenObject *ag, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
if (ag->ag_frame_state == FRAME_SUSPENDED) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
static PyGetSetDef async_gen_getsetlist[] = {
|
|
|
|
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
|
|
|
|
PyDoc_STR("name of the async generator")},
|
|
|
|
{"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
|
|
|
|
PyDoc_STR("qualified name of the async generator")},
|
|
|
|
{"ag_await", (getter)coro_get_cr_await, NULL,
|
|
|
|
PyDoc_STR("object being awaited on, or None")},
|
2021-07-26 07:22:16 -03:00
|
|
|
{"ag_frame", (getter)ag_getframe, NULL, NULL},
|
2023-02-23 06:19:01 -04:00
|
|
|
{"ag_code", (getter)ag_getcode, NULL, NULL},
|
2023-03-11 09:19:40 -04:00
|
|
|
{"ag_suspended", (getter)ag_getsuspended, NULL, NULL},
|
2016-09-09 02:01:51 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMemberDef async_gen_memberlist[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"ag_running", Py_T_BOOL, offsetof(PyAsyncGenObject, ag_running_async),
|
|
|
|
Py_READONLY},
|
2016-09-09 02:01:51 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(async_aclose_doc,
|
|
|
|
"aclose() -> raise GeneratorExit inside generator.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(async_asend_doc,
|
|
|
|
"asend(v) -> send 'v' in generator.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(async_athrow_doc,
|
2022-09-30 05:43:02 -03:00
|
|
|
"athrow(value)\n\
|
|
|
|
athrow(type[,value[,tb]])\n\
|
|
|
|
\n\
|
|
|
|
raise exception in generator.\n\
|
|
|
|
the (type, val, tb) signature is deprecated, \n\
|
|
|
|
and may be removed in a future version of Python.");
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
static PyMethodDef async_gen_methods[] = {
|
|
|
|
{"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
|
|
|
|
{"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
|
|
|
|
{"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
|
2021-12-06 06:13:49 -04:00
|
|
|
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
|
2021-09-19 12:05:30 -03:00
|
|
|
{"__class_getitem__", Py_GenericAlias,
|
2020-04-10 01:25:53 -03:00
|
|
|
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
|
2016-09-09 02:01:51 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyAsyncMethods async_gen_as_async = {
|
|
|
|
0, /* am_await */
|
|
|
|
PyObject_SelfIter, /* am_aiter */
|
2020-11-10 16:09:55 -04:00
|
|
|
(unaryfunc)async_gen_anext, /* am_anext */
|
|
|
|
(sendfunc)PyGen_am_send, /* am_send */
|
2016-09-09 02:01:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyTypeObject PyAsyncGen_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"async_generator", /* tp_name */
|
2021-12-06 06:13:49 -04:00
|
|
|
offsetof(PyAsyncGenObject, ag_iframe) +
|
2022-02-25 11:22:00 -04:00
|
|
|
offsetof(_PyInterpreterFrame, localsplus), /* tp_basicsize */
|
2021-12-06 06:13:49 -04:00
|
|
|
sizeof(PyObject *), /* tp_itemsize */
|
2016-09-09 02:01:51 -03:00
|
|
|
/* methods */
|
|
|
|
(destructor)gen_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
&async_gen_as_async, /* tp_as_async */
|
|
|
|
(reprfunc)async_gen_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2021-07-23 10:21:11 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)async_gen_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
async_gen_methods, /* tp_methods */
|
|
|
|
async_gen_memberlist, /* tp_members */
|
|
|
|
async_gen_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
|
|
|
0, /* tp_free */
|
|
|
|
0, /* tp_is_gc */
|
|
|
|
0, /* tp_bases */
|
|
|
|
0, /* tp_mro */
|
|
|
|
0, /* tp_cache */
|
|
|
|
0, /* tp_subclasses */
|
|
|
|
0, /* tp_weaklist */
|
|
|
|
0, /* tp_del */
|
|
|
|
0, /* tp_version_tag */
|
|
|
|
_PyGen_Finalize, /* tp_finalize */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-10-21 10:12:20 -03:00
|
|
|
#if _PyAsyncGen_MAXFREELIST > 0
|
2020-06-23 11:40:40 -03:00
|
|
|
static struct _Py_async_gen_state *
|
|
|
|
get_async_gen_state(void)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
return &interp->async_gen;
|
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
#endif
|
2020-06-23 11:40:40 -03:00
|
|
|
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
PyObject *
|
|
|
|
PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
|
|
|
|
{
|
|
|
|
PyAsyncGenObject *o;
|
|
|
|
o = (PyAsyncGenObject *)gen_new_with_qualname(
|
|
|
|
&PyAsyncGen_Type, f, name, qualname);
|
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-12-06 06:13:49 -04:00
|
|
|
o->ag_origin_or_finalizer = NULL;
|
2016-09-09 02:01:51 -03:00
|
|
|
o->ag_closed = 0;
|
|
|
|
o->ag_hooks_inited = 0;
|
2019-09-30 02:59:11 -03:00
|
|
|
o->ag_running_async = 0;
|
2016-09-09 02:01:51 -03:00
|
|
|
return (PyObject*)o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-28 21:29:20 -03:00
|
|
|
void
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyAsyncGen_ClearFreeLists(PyInterpreterState *interp)
|
2016-09-09 02:01:51 -03:00
|
|
|
{
|
2021-10-21 10:12:20 -03:00
|
|
|
#if _PyAsyncGen_MAXFREELIST > 0
|
2021-02-19 10:10:45 -04:00
|
|
|
struct _Py_async_gen_state *state = &interp->async_gen;
|
2020-06-04 21:34:14 -03:00
|
|
|
|
|
|
|
while (state->value_numfree) {
|
2016-09-09 02:01:51 -03:00
|
|
|
_PyAsyncGenWrappedValue *o;
|
2020-06-04 21:34:14 -03:00
|
|
|
o = state->value_freelist[--state->value_numfree];
|
2016-09-09 02:01:51 -03:00
|
|
|
assert(_PyAsyncGenWrappedValue_CheckExact(o));
|
2016-11-08 20:46:22 -04:00
|
|
|
PyObject_GC_Del(o);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:34:14 -03:00
|
|
|
while (state->asend_numfree) {
|
2016-09-09 02:01:51 -03:00
|
|
|
PyAsyncGenASend *o;
|
2020-06-04 21:34:14 -03:00
|
|
|
o = state->asend_freelist[--state->asend_numfree];
|
2020-03-04 09:15:20 -04:00
|
|
|
assert(Py_IS_TYPE(o, &_PyAsyncGenASend_Type));
|
2016-11-08 20:46:22 -04:00
|
|
|
PyObject_GC_Del(o);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
#endif
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyAsyncGen_Fini(PyInterpreterState *interp)
|
2016-09-09 02:01:51 -03:00
|
|
|
{
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyAsyncGen_ClearFreeLists(interp);
|
2021-10-21 10:12:20 -03:00
|
|
|
#if defined(Py_DEBUG) && _PyAsyncGen_MAXFREELIST > 0
|
2021-02-19 10:10:45 -04:00
|
|
|
struct _Py_async_gen_state *state = &interp->async_gen;
|
2020-06-07 21:14:47 -03:00
|
|
|
state->value_numfree = -1;
|
|
|
|
state->asend_numfree = -1;
|
|
|
|
#endif
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
|
|
|
|
{
|
|
|
|
if (result == NULL) {
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
PyErr_SetNone(PyExc_StopAsyncIteration);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)
|
|
|
|
|| PyErr_ExceptionMatches(PyExc_GeneratorExit)
|
|
|
|
) {
|
|
|
|
gen->ag_closed = 1;
|
|
|
|
}
|
|
|
|
|
2019-09-30 02:59:11 -03:00
|
|
|
gen->ag_running_async = 0;
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyAsyncGenWrappedValue_CheckExact(result)) {
|
|
|
|
/* async yield */
|
2016-11-06 12:47:03 -04:00
|
|
|
_PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val);
|
2016-09-09 02:01:51 -03:00
|
|
|
Py_DECREF(result);
|
2019-09-30 02:59:11 -03:00
|
|
|
gen->ag_running_async = 0;
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------- Async Generator ASend Awaitable ------------ */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_gen_asend_dealloc(PyAsyncGenASend *o)
|
|
|
|
{
|
2023-05-26 08:23:29 -03:00
|
|
|
if (PyObject_CallFinalizerFromDealloc((PyObject *)o)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:46:22 -04:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
Py_CLEAR(o->ags_gen);
|
|
|
|
Py_CLEAR(o->ags_sendval);
|
2021-10-21 10:12:20 -03:00
|
|
|
#if _PyAsyncGen_MAXFREELIST > 0
|
2020-06-23 11:40:40 -03:00
|
|
|
struct _Py_async_gen_state *state = get_async_gen_state();
|
2020-06-07 21:14:47 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
// async_gen_asend_dealloc() must not be called after _PyAsyncGen_Fini()
|
|
|
|
assert(state->asend_numfree != -1);
|
|
|
|
#endif
|
2020-06-04 21:34:14 -03:00
|
|
|
if (state->asend_numfree < _PyAsyncGen_MAXFREELIST) {
|
2016-09-09 02:01:51 -03:00
|
|
|
assert(PyAsyncGenASend_CheckExact(o));
|
2020-06-04 21:34:14 -03:00
|
|
|
state->asend_freelist[state->asend_numfree++] = o;
|
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-11-08 20:46:22 -04:00
|
|
|
PyObject_GC_Del(o);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:46:22 -04:00
|
|
|
static int
|
|
|
|
async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(o->ags_gen);
|
|
|
|
Py_VISIT(o->ags_sendval);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg)
|
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
if (o->ags_state == AWAITABLE_STATE_CLOSED) {
|
2020-01-20 18:49:30 -04:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"cannot reuse already awaited __anext__()/asend()");
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o->ags_state == AWAITABLE_STATE_INIT) {
|
2019-09-30 02:59:11 -03:00
|
|
|
if (o->ags_gen->ag_running_async) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"anext(): asynchronous generator is already running");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
if (arg == NULL || arg == Py_None) {
|
|
|
|
arg = o->ags_sendval;
|
|
|
|
}
|
|
|
|
o->ags_state = AWAITABLE_STATE_ITER;
|
|
|
|
}
|
|
|
|
|
2019-09-30 02:59:11 -03:00
|
|
|
o->ags_gen->ag_running_async = 1;
|
2020-09-22 02:08:54 -03:00
|
|
|
result = gen_send((PyGenObject*)o->ags_gen, arg);
|
2016-09-09 02:01:51 -03:00
|
|
|
result = async_gen_unwrap_value(o->ags_gen, result);
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
o->ags_state = AWAITABLE_STATE_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_asend_iternext(PyAsyncGenASend *o)
|
|
|
|
{
|
|
|
|
return async_gen_asend_send(o, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2022-03-11 10:37:14 -04:00
|
|
|
async_gen_asend_throw(PyAsyncGenASend *o, PyObject *const *args, Py_ssize_t nargs)
|
2016-09-09 02:01:51 -03:00
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
if (o->ags_state == AWAITABLE_STATE_CLOSED) {
|
2020-01-20 18:49:30 -04:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"cannot reuse already awaited __anext__()/asend()");
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:37:14 -04:00
|
|
|
result = gen_throw((PyGenObject*)o->ags_gen, args, nargs);
|
2016-09-09 02:01:51 -03:00
|
|
|
result = async_gen_unwrap_value(o->ags_gen, result);
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
o->ags_state = AWAITABLE_STATE_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
|
|
|
|
{
|
|
|
|
o->ags_state = AWAITABLE_STATE_CLOSED;
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2023-05-26 08:23:29 -03:00
|
|
|
static void
|
|
|
|
async_gen_asend_finalize(PyAsyncGenASend *o)
|
|
|
|
{
|
|
|
|
if (o->ags_state == AWAITABLE_STATE_INIT) {
|
|
|
|
_PyErr_WarnUnawaitedAgenMethod(o->ags_gen, &_Py_ID(asend));
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
static PyMethodDef async_gen_asend_methods[] = {
|
|
|
|
{"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
|
2022-05-03 16:42:14 -03:00
|
|
|
{"throw", _PyCFunction_CAST(async_gen_asend_throw), METH_FASTCALL, throw_doc},
|
2016-09-09 02:01:51 -03:00
|
|
|
{"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyAsyncMethods async_gen_asend_as_async = {
|
|
|
|
PyObject_SelfIter, /* am_await */
|
|
|
|
0, /* am_aiter */
|
2020-11-10 16:09:55 -04:00
|
|
|
0, /* am_anext */
|
|
|
|
0, /* am_send */
|
2016-09-09 02:01:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyTypeObject _PyAsyncGenASend_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"async_generator_asend", /* tp_name */
|
|
|
|
sizeof(PyAsyncGenASend), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)async_gen_asend_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
&async_gen_asend_as_async, /* tp_as_async */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2016-11-08 20:46:22 -04:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_doc */
|
2016-11-08 20:46:22 -04:00
|
|
|
(traverseproc)async_gen_asend_traverse, /* tp_traverse */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)async_gen_asend_iternext, /* tp_iternext */
|
|
|
|
async_gen_asend_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
2023-05-26 08:23:29 -03:00
|
|
|
.tp_finalize = (destructor)async_gen_asend_finalize,
|
2016-09-09 02:01:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
|
|
|
|
{
|
|
|
|
PyAsyncGenASend *o;
|
2021-10-21 10:12:20 -03:00
|
|
|
#if _PyAsyncGen_MAXFREELIST > 0
|
2020-06-23 11:40:40 -03:00
|
|
|
struct _Py_async_gen_state *state = get_async_gen_state();
|
2020-06-07 21:14:47 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
// async_gen_asend_new() must not be called after _PyAsyncGen_Fini()
|
|
|
|
assert(state->asend_numfree != -1);
|
|
|
|
#endif
|
2020-06-04 21:34:14 -03:00
|
|
|
if (state->asend_numfree) {
|
|
|
|
state->asend_numfree--;
|
|
|
|
o = state->asend_freelist[state->asend_numfree];
|
2016-09-09 02:01:51 -03:00
|
|
|
_Py_NewReference((PyObject *)o);
|
2020-06-04 21:34:14 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-11-08 20:46:22 -04:00
|
|
|
o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:22:02 -04:00
|
|
|
o->ags_gen = (PyAsyncGenObject*)Py_NewRef(gen);
|
2016-09-09 02:01:51 -03:00
|
|
|
|
2022-11-10 17:22:02 -04:00
|
|
|
o->ags_sendval = Py_XNewRef(sendval);
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
o->ags_state = AWAITABLE_STATE_INIT;
|
2016-11-08 20:46:22 -04:00
|
|
|
|
|
|
|
_PyObject_GC_TRACK((PyObject*)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
return (PyObject*)o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------- Async Generator Value Wrapper ------------ */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
|
|
|
|
{
|
2016-11-08 20:46:22 -04:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
Py_CLEAR(o->agw_val);
|
2021-10-21 10:12:20 -03:00
|
|
|
#if _PyAsyncGen_MAXFREELIST > 0
|
2020-06-23 11:40:40 -03:00
|
|
|
struct _Py_async_gen_state *state = get_async_gen_state();
|
2020-06-07 21:14:47 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
// async_gen_wrapped_val_dealloc() must not be called after _PyAsyncGen_Fini()
|
|
|
|
assert(state->value_numfree != -1);
|
|
|
|
#endif
|
2020-06-04 21:34:14 -03:00
|
|
|
if (state->value_numfree < _PyAsyncGen_MAXFREELIST) {
|
2016-09-09 02:01:51 -03:00
|
|
|
assert(_PyAsyncGenWrappedValue_CheckExact(o));
|
2020-06-04 21:34:14 -03:00
|
|
|
state->value_freelist[state->value_numfree++] = o;
|
2022-05-03 19:40:24 -03:00
|
|
|
OBJECT_STAT_INC(to_freelist);
|
2020-06-04 21:34:14 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-11-08 20:46:22 -04:00
|
|
|
PyObject_GC_Del(o);
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-08 20:46:22 -04:00
|
|
|
static int
|
|
|
|
async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o,
|
|
|
|
visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(o->agw_val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
PyTypeObject _PyAsyncGenWrappedValue_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"async_generator_wrapped_value", /* tp_name */
|
|
|
|
sizeof(_PyAsyncGenWrappedValue), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)async_gen_wrapped_val_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_as_async */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2016-11-08 20:46:22 -04:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_doc */
|
2016-11-08 20:46:22 -04:00
|
|
|
(traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
2023-01-06 10:47:57 -04:00
|
|
|
_PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val)
|
2016-09-09 02:01:51 -03:00
|
|
|
{
|
|
|
|
_PyAsyncGenWrappedValue *o;
|
|
|
|
assert(val);
|
|
|
|
|
2021-10-21 10:12:20 -03:00
|
|
|
#if _PyAsyncGen_MAXFREELIST > 0
|
2023-01-06 10:47:57 -04:00
|
|
|
struct _Py_async_gen_state *state = &tstate->interp->async_gen;
|
2020-06-07 21:14:47 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
// _PyAsyncGenValueWrapperNew() must not be called after _PyAsyncGen_Fini()
|
|
|
|
assert(state->value_numfree != -1);
|
|
|
|
#endif
|
2020-06-04 21:34:14 -03:00
|
|
|
if (state->value_numfree) {
|
|
|
|
state->value_numfree--;
|
|
|
|
o = state->value_freelist[state->value_numfree];
|
2022-05-03 19:40:24 -03:00
|
|
|
OBJECT_STAT_INC(from_freelist);
|
2016-09-09 02:01:51 -03:00
|
|
|
assert(_PyAsyncGenWrappedValue_CheckExact(o));
|
|
|
|
_Py_NewReference((PyObject*)o);
|
2020-06-04 21:34:14 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-11-08 20:46:22 -04:00
|
|
|
o = PyObject_GC_New(_PyAsyncGenWrappedValue,
|
|
|
|
&_PyAsyncGenWrappedValue_Type);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
o->agw_val = Py_NewRef(val);
|
2016-11-08 20:46:22 -04:00
|
|
|
_PyObject_GC_TRACK((PyObject*)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
return (PyObject*)o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------- Async Generator AThrow awaitable ------------ */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
|
|
|
|
{
|
2023-05-26 08:23:29 -03:00
|
|
|
if (PyObject_CallFinalizerFromDealloc((PyObject *)o)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:46:22 -04:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
Py_CLEAR(o->agt_gen);
|
|
|
|
Py_CLEAR(o->agt_args);
|
2016-11-08 20:46:22 -04:00
|
|
|
PyObject_GC_Del(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(o->agt_gen);
|
|
|
|
Py_VISIT(o->agt_args);
|
|
|
|
return 0;
|
2016-09-09 02:01:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
|
|
|
|
{
|
|
|
|
PyGenObject *gen = (PyGenObject*)o->agt_gen;
|
|
|
|
PyObject *retval;
|
|
|
|
|
2020-02-13 04:15:38 -04:00
|
|
|
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
|
2020-01-20 18:49:30 -04:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"cannot reuse already awaited aclose()/athrow()");
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-22 09:57:19 -03:00
|
|
|
if (gen->gi_frame_state >= FRAME_COMPLETED) {
|
2020-02-13 04:15:38 -04:00
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o->agt_state == AWAITABLE_STATE_INIT) {
|
2019-09-30 02:59:11 -03:00
|
|
|
if (o->agt_gen->ag_running_async) {
|
2020-02-13 04:15:38 -04:00
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
2019-09-30 02:59:11 -03:00
|
|
|
if (o->agt_args == NULL) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"aclose(): asynchronous generator is already running");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"athrow(): asynchronous generator is already running");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o->agt_gen->ag_closed) {
|
2019-09-30 02:59:11 -03:00
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
|
|
|
PyErr_SetNone(PyExc_StopAsyncIteration);
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
o->agt_state = AWAITABLE_STATE_ITER;
|
2019-09-30 02:59:11 -03:00
|
|
|
o->agt_gen->ag_running_async = 1;
|
2016-09-09 02:01:51 -03:00
|
|
|
|
|
|
|
if (o->agt_args == NULL) {
|
|
|
|
/* aclose() mode */
|
|
|
|
o->agt_gen->ag_closed = 1;
|
|
|
|
|
|
|
|
retval = _gen_throw((PyGenObject *)gen,
|
|
|
|
0, /* Do not close generator when
|
|
|
|
PyExc_GeneratorExit is passed */
|
|
|
|
PyExc_GeneratorExit, NULL, NULL);
|
|
|
|
|
|
|
|
if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
goto yield_close;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PyObject *typ;
|
|
|
|
PyObject *tb = NULL;
|
|
|
|
PyObject *val = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3,
|
|
|
|
&typ, &val, &tb)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = _gen_throw((PyGenObject *)gen,
|
|
|
|
0, /* Do not close generator when
|
|
|
|
PyExc_GeneratorExit is passed */
|
|
|
|
typ, val, tb);
|
|
|
|
retval = async_gen_unwrap_value(o->agt_gen, retval);
|
|
|
|
}
|
|
|
|
if (retval == NULL) {
|
|
|
|
goto check_error;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(o->agt_state == AWAITABLE_STATE_ITER);
|
|
|
|
|
2020-09-22 02:08:54 -03:00
|
|
|
retval = gen_send((PyGenObject *)gen, arg);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o->agt_args) {
|
|
|
|
return async_gen_unwrap_value(o->agt_gen, retval);
|
|
|
|
} else {
|
|
|
|
/* aclose() mode */
|
|
|
|
if (retval) {
|
|
|
|
if (_PyAsyncGenWrappedValue_CheckExact(retval)) {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
goto yield_close;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto check_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yield_close:
|
2019-09-30 02:59:11 -03:00
|
|
|
o->agt_gen->ag_running_async = 0;
|
2020-02-13 04:15:38 -04:00
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
2016-09-09 02:01:51 -03:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
check_error:
|
2019-09-30 02:59:11 -03:00
|
|
|
o->agt_gen->ag_running_async = 0;
|
2020-02-13 04:15:38 -04:00
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
2018-06-07 21:31:26 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
|
|
|
|
PyErr_ExceptionMatches(PyExc_GeneratorExit))
|
|
|
|
{
|
2016-11-16 19:16:17 -04:00
|
|
|
if (o->agt_args == NULL) {
|
|
|
|
/* when aclose() is called we don't want to propagate
|
2018-06-07 21:31:26 -03:00
|
|
|
StopAsyncIteration or GeneratorExit; just raise
|
|
|
|
StopIteration, signalling that this 'aclose()' await
|
|
|
|
is done.
|
|
|
|
*/
|
2016-11-16 19:16:17 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2022-03-11 10:37:14 -04:00
|
|
|
async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t nargs)
|
2016-09-09 02:01:51 -03:00
|
|
|
{
|
|
|
|
PyObject *retval;
|
|
|
|
|
|
|
|
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
|
2020-01-20 18:49:30 -04:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"cannot reuse already awaited aclose()/athrow()");
|
2016-09-09 02:01:51 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:37:14 -04:00
|
|
|
retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o->agt_args) {
|
|
|
|
return async_gen_unwrap_value(o->agt_gen, retval);
|
|
|
|
} else {
|
|
|
|
/* aclose() mode */
|
|
|
|
if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
|
2019-09-30 02:59:11 -03:00
|
|
|
o->agt_gen->ag_running_async = 0;
|
2020-02-13 04:15:38 -04:00
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
2016-09-09 02:01:51 -03:00
|
|
|
Py_DECREF(retval);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-19 09:53:52 -04:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
|
|
|
|
PyErr_ExceptionMatches(PyExc_GeneratorExit))
|
|
|
|
{
|
|
|
|
/* when aclose() is called we don't want to propagate
|
|
|
|
StopAsyncIteration or GeneratorExit; just raise
|
|
|
|
StopIteration, signalling that this 'aclose()' await
|
|
|
|
is done.
|
|
|
|
*/
|
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
}
|
2016-09-09 02:01:51 -03:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_athrow_iternext(PyAsyncGenAThrow *o)
|
|
|
|
{
|
|
|
|
return async_gen_athrow_send(o, Py_None);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
|
|
|
|
{
|
|
|
|
o->agt_state = AWAITABLE_STATE_CLOSED;
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 08:23:29 -03:00
|
|
|
static void
|
|
|
|
async_gen_athrow_finalize(PyAsyncGenAThrow *o)
|
|
|
|
{
|
|
|
|
if (o->agt_state == AWAITABLE_STATE_INIT) {
|
|
|
|
PyObject *method = o->agt_args ? &_Py_ID(athrow) : &_Py_ID(aclose);
|
|
|
|
_PyErr_WarnUnawaitedAgenMethod(o->agt_gen, method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:01:51 -03:00
|
|
|
static PyMethodDef async_gen_athrow_methods[] = {
|
|
|
|
{"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
|
2022-05-03 16:42:14 -03:00
|
|
|
{"throw", _PyCFunction_CAST(async_gen_athrow_throw),
|
2022-03-11 10:37:14 -04:00
|
|
|
METH_FASTCALL, throw_doc},
|
2016-09-09 02:01:51 -03:00
|
|
|
{"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyAsyncMethods async_gen_athrow_as_async = {
|
|
|
|
PyObject_SelfIter, /* am_await */
|
|
|
|
0, /* am_aiter */
|
2020-11-10 16:09:55 -04:00
|
|
|
0, /* am_anext */
|
|
|
|
0, /* am_send */
|
2016-09-09 02:01:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyTypeObject _PyAsyncGenAThrow_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"async_generator_athrow", /* tp_name */
|
|
|
|
sizeof(PyAsyncGenAThrow), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)async_gen_athrow_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
&async_gen_athrow_as_async, /* tp_as_async */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2016-11-08 20:46:22 -04:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_doc */
|
2016-11-08 20:46:22 -04:00
|
|
|
(traverseproc)async_gen_athrow_traverse, /* tp_traverse */
|
2016-09-09 02:01:51 -03:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)async_gen_athrow_iternext, /* tp_iternext */
|
|
|
|
async_gen_athrow_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
2023-05-26 08:23:29 -03:00
|
|
|
.tp_finalize = (destructor)async_gen_athrow_finalize,
|
2016-09-09 02:01:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
|
|
|
|
{
|
|
|
|
PyAsyncGenAThrow *o;
|
2016-11-08 20:46:22 -04:00
|
|
|
o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type);
|
2016-09-09 02:01:51 -03:00
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
o->agt_gen = (PyAsyncGenObject*)Py_NewRef(gen);
|
|
|
|
o->agt_args = Py_XNewRef(args);
|
2016-09-09 02:01:51 -03:00
|
|
|
o->agt_state = AWAITABLE_STATE_INIT;
|
2016-11-08 20:46:22 -04:00
|
|
|
_PyObject_GC_TRACK((PyObject*)o);
|
2016-09-09 02:01:51 -03:00
|
|
|
return (PyObject*)o;
|
|
|
|
}
|