cpython/Objects/genobject.c

/* Generator object implementation */

#define _PY_INTERPRETER

#include "Python.h"
#include "pycore_call.h"          // _PyObject_CallNoArgs()
#include "pycore_ceval.h"         // _PyEval_EvalFrame()
#include "pycore_frame.h"         // _PyInterpreterFrame
#include "pycore_freelist.h"      // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
#include "pycore_gc.h"            // _PyGC_CLEAR_FINALIZED()
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
#include "pycore_object.h"        // _PyObject_GC_UNTRACK()
#include "pycore_opcode_utils.h"  // RESUME_AFTER_YIELD_FROM
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_*
#include "pycore_pyerrors.h"      // _PyErr_ClearExcState()
#include "pycore_pystate.h"       // _PyThreadState_GET()

#include "pystats.h"

static PyObject *gen_close(PyGenObject *, PyObject *);
static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *);

static const char *NON_INIT_CORO_MSG =;

static const char *ASYNC_GEN_IGNORED_EXIT_MSG =;

/* Returns a borrowed reference */
static inline PyCodeObject *
_PyGen_GetCode(PyGenObject *gen) {}

PyCodeObject *
PyGen_GetCode(PyGenObject *gen) {}

static int
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
{}

void
_PyGen_Finalize(PyObject *self)
{}

static void
gen_dealloc(PyGenObject *gen)
{}

static PySendResult
gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
             int exc, int closing)
{}

static PySendResult
PyGen_am_send(PyGenObject *gen, PyObject *arg, PyObject **result)
{}

static PyObject *
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
{}

PyDoc_STRVAR(send_doc,
"send(arg) -> send 'arg' into generator,\n\
return next yielded value or raise StopIteration.");

static PyObject *
gen_send(PyGenObject *gen, PyObject *arg)
{}

PyDoc_STRVAR(close_doc,
"close() -> raise GeneratorExit inside generator.");

/*
 *   This helper function is used by gen_close and gen_throw to
 *   close a subiterator being delegated to by yield-from.
 */

static int
gen_close_iter(PyObject *yf)
{}

static inline bool
is_resume(_Py_CODEUNIT *instr)
{}

PyObject *
_PyGen_yf(PyGenObject *gen)
{}

static PyObject *
gen_close(PyGenObject *gen, PyObject *args)
{}


PyDoc_STRVAR(throw_doc,
"throw(value)\n\
throw(type[,value[,tb]])\n\
\n\
Raise exception in generator, return next yielded value or raise\n\
StopIteration.\n\
the (type, val, tb) signature is deprecated, \n\
and may be removed in a future version of Python.");

static PyObject *
_gen_throw(PyGenObject *gen, int close_on_genexit,
           PyObject *typ, PyObject *val, PyObject *tb)
{}


static PyObject *
gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs)
{}


static PyObject *
gen_iternext(PyGenObject *gen)
{}

/*
 * 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)
{}

/*
 *   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
_PyGen_FetchStopIterationValue(PyObject **pvalue)
{}

static PyObject *
gen_repr(PyGenObject *gen)
{}

static PyObject *
gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored))
{}

static int
gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
{}

static PyObject *
gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored))
{}

static int
gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
{}

static PyObject *
gen_getyieldfrom(PyGenObject *gen, void *Py_UNUSED(ignored))
{}


static PyObject *
gen_getrunning(PyGenObject *gen, void *Py_UNUSED(ignored))
{}

static PyObject *
gen_getsuspended(PyGenObject *gen, void *Py_UNUSED(ignored))
{}

static PyObject *
_gen_getframe(PyGenObject *gen, const char *const name)
{}

static PyObject *
gen_getframe(PyGenObject *gen, void *Py_UNUSED(ignored))
{}

static PyObject *
_gen_getcode(PyGenObject *gen, const char *const name)
{}

static PyObject *
gen_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
{}

static PyGetSetDef gen_getsetlist[] =;

static PyMemberDef gen_memberlist[] =;

static PyObject *
gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored))
{}

PyDoc_STRVAR(sizeof__doc__,
"gen.__sizeof__() -> size of gen in memory, in bytes");

static PyMethodDef gen_methods[] =;

static PyAsyncMethods gen_as_async =;


PyTypeObject PyGen_Type =;

static PyObject *
make_gen(PyTypeObject *type, PyFunctionObject *func)
{}

static PyObject *
compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame);

PyObject *
_Py_MakeCoro(PyFunctionObject *func)
{}

static PyObject *
gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
                      PyObject *name, PyObject *qualname)
{}

PyObject *
PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
{}

PyObject *
PyGen_New(PyFrameObject *f)
{}

/* Coroutine Object */

PyCoroWrapper;

static int
gen_is_coroutine(PyObject *o)
{}

/*
 *   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 *
_PyCoro_GetAwaitableIter(PyObject *o)
{}

static PyObject *
coro_repr(PyCoroObject *coro)
{}

static PyObject *
coro_await(PyCoroObject *coro)
{}

static PyObject *
coro_get_cr_await(PyCoroObject *coro, void *Py_UNUSED(ignored))
{}

static PyObject *
cr_getsuspended(PyCoroObject *coro, void *Py_UNUSED(ignored))
{}

static PyObject *
cr_getrunning(PyCoroObject *coro, void *Py_UNUSED(ignored))
{}

static PyObject *
cr_getframe(PyCoroObject *coro, void *Py_UNUSED(ignored))
{}

static PyObject *
cr_getcode(PyCoroObject *coro, void *Py_UNUSED(ignored))
{}


static PyGetSetDef coro_getsetlist[] =;

static PyMemberDef coro_memberlist[] =;

PyDoc_STRVAR(coro_send_doc,
"send(arg) -> send 'arg' into coroutine,\n\
return next iterated value or raise StopIteration.");

PyDoc_STRVAR(coro_throw_doc,
"throw(value)\n\
throw(type[,value[,traceback]])\n\
\n\
Raise exception in coroutine, return next iterated value or raise\n\
StopIteration.\n\
the (type, val, tb) signature is deprecated, \n\
and may be removed in a future version of Python.");


PyDoc_STRVAR(coro_close_doc,
"close() -> raise GeneratorExit inside coroutine.");

static PyMethodDef coro_methods[] =;

static PyAsyncMethods coro_as_async =;

PyTypeObject PyCoro_Type =;

static void
coro_wrapper_dealloc(PyCoroWrapper *cw)
{}

static PyObject *
coro_wrapper_iternext(PyCoroWrapper *cw)
{}

static PyObject *
coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
{}

static PyObject *
coro_wrapper_throw(PyCoroWrapper *cw, PyObject *const *args, Py_ssize_t nargs)
{}

static PyObject *
coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
{}

static int
coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
{}

static PyMethodDef coro_wrapper_methods[] =;

PyTypeObject _PyCoroWrapper_Type =;

static PyObject *
compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame)
{}

PyObject *
PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
{}


/* ========= Asynchronous Generators ========= */


AwaitableState;


PyAsyncGenASend;


PyAsyncGenAThrow;


_PyAsyncGenWrappedValue;


#define _PyAsyncGenWrappedValue_CheckExact(o)


static int
async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg)
{}


static PyObject *
async_gen_repr(PyAsyncGenObject *o)
{}


static int
async_gen_init_hooks(PyAsyncGenObject *o)
{}


static PyObject *
async_gen_anext(PyAsyncGenObject *o)
{}


static PyObject *
async_gen_asend(PyAsyncGenObject *o, PyObject *arg)
{}


static PyObject *
async_gen_aclose(PyAsyncGenObject *o, PyObject *arg)
{}

static PyObject *
async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
{}

static PyObject *
ag_getframe(PyAsyncGenObject *ag, void *Py_UNUSED(ignored))
{}

static PyObject *
ag_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
{}

static PyObject *
ag_getsuspended(PyAsyncGenObject *ag, void *Py_UNUSED(ignored))
{}

static PyGetSetDef async_gen_getsetlist[] =;

static PyMemberDef async_gen_memberlist[] =;

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,
"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.");

static PyMethodDef async_gen_methods[] =;


static PyAsyncMethods async_gen_as_async =;


PyTypeObject PyAsyncGen_Type =;


PyObject *
PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
{}

static PyObject *
async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
{}


/* ---------- Async Generator ASend Awaitable ------------ */


static void
async_gen_asend_dealloc(PyAsyncGenASend *o)
{}

static int
async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg)
{}


static PyObject *
async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg)
{}


static PyObject *
async_gen_asend_iternext(PyAsyncGenASend *o)
{}


static PyObject *
async_gen_asend_throw(PyAsyncGenASend *o, PyObject *const *args, Py_ssize_t nargs)
{}


static PyObject *
async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
{}

static void
async_gen_asend_finalize(PyAsyncGenASend *o)
{}

static PyMethodDef async_gen_asend_methods[] =;


static PyAsyncMethods async_gen_asend_as_async =;


PyTypeObject _PyAsyncGenASend_Type =;


static PyObject *
async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
{}


/* ---------- Async Generator Value Wrapper ------------ */


static void
async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
{}


static int
async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o,
                               visitproc visit, void *arg)
{}


PyTypeObject _PyAsyncGenWrappedValue_Type =;


PyObject *
_PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val)
{}


/* ---------- Async Generator AThrow awaitable ------------ */


static void
async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
{}


static int
async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg)
{}


static PyObject *
async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
{}


static PyObject *
async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t nargs)
{}


static PyObject *
async_gen_athrow_iternext(PyAsyncGenAThrow *o)
{}


static PyObject *
async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
{}


static void
async_gen_athrow_finalize(PyAsyncGenAThrow *o)
{}

static PyMethodDef async_gen_athrow_methods[] =;


static PyAsyncMethods async_gen_athrow_as_async =;


PyTypeObject _PyAsyncGenAThrow_Type =;


static PyObject *
async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
{}