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"

// Forward declarations
static PyObject* gen_close(PyObject *, PyObject *);
static PyObject* async_gen_asend_new(PyAsyncGenObject *, PyObject *);
static PyObject* async_gen_athrow_new(PyAsyncGenObject *, PyObject *);


#define _PyGen_CAST(op)
#define _PyCoroObject_CAST(op)
#define _PyAsyncGenObject_CAST(op)


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(PyObject *self, visitproc visit, void *arg)
{}

void
_PyGen_Finalize(PyObject *self)
{}

static void
gen_dealloc(PyObject *self)
{}

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

static PySendResult
PyGen_am_send(PyObject *self, 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(PyObject *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(PyObject *self, 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(PyObject *self)
{}

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

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

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

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

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

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


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

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

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

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

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

static PyObject *
gen_getcode(PyObject *self, 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;

#define _PyCoroWrapper_CAST(op)


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(PyObject *self)
{}

static PyObject *
coro_await(PyObject *coro)
{}

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

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

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

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

static PyObject *
cr_getcode(PyObject *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(PyObject *self)
{}

static PyObject *
coro_wrapper_iternext(PyObject *self)
{}

static PyObject *
coro_wrapper_send(PyObject *self, PyObject *arg)
{}

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

static PyObject *
coro_wrapper_close(PyObject *self, PyObject *args)
{}

static int
coro_wrapper_traverse(PyObject *self, 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;

#define _PyAsyncGenASend_CAST(op)


PyAsyncGenAThrow;


_PyAsyncGenWrappedValue;


#define _PyAsyncGenWrappedValue_CheckExact(o)
#define _PyAsyncGenWrappedValue_CAST(op)


static int
async_gen_traverse(PyObject *self, visitproc visit, void *arg)
{}


static PyObject *
async_gen_repr(PyObject *self)
{}


static int
async_gen_init_hooks(PyAsyncGenObject *o)
{}


static PyObject *
async_gen_anext(PyObject *self)
{}


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(PyObject *ag, void *Py_UNUSED(ignored))
{}

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

static PyObject *
ag_getsuspended(PyObject *self, 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(PyObject *self)
{}

static int
async_gen_asend_traverse(PyObject *self, visitproc visit, void *arg)
{}


static PyObject *
async_gen_asend_send(PyObject *self, PyObject *arg)
{}


static PyObject *
async_gen_asend_iternext(PyObject *ags)
{}


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


static PyObject *
async_gen_asend_close(PyObject *self, PyObject *args)
{}

static void
async_gen_asend_finalize(PyObject *self)
{}

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(PyObject *self)
{}


static int
async_gen_wrapped_val_traverse(PyObject *self, visitproc visit, void *arg)
{}


PyTypeObject _PyAsyncGenWrappedValue_Type =;


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


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

#define _PyAsyncGenAThrow_CAST(op)

static void
async_gen_athrow_dealloc(PyObject *self)
{}


static int
async_gen_athrow_traverse(PyObject *self, visitproc visit, void *arg)
{}


static PyObject *
async_gen_athrow_send(PyObject *self, PyObject *arg)
{}


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


static PyObject *
async_gen_athrow_iternext(PyObject *agt)
{}


static PyObject *
async_gen_athrow_close(PyObject *self, 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)
{}