Back to index

python3.2  3.2.2
Functions | Variables
genobject.c File Reference
#include "Python.h"
#include "frameobject.h"
#include "structmember.h"
#include "opcode.h"

Go to the source code of this file.

Functions

static int gen_traverse (PyGenObject *gen, visitproc visit, void *arg)
static void gen_dealloc (PyGenObject *gen)
static PyObjectgen_send_ex (PyGenObject *gen, PyObject *arg, int exc)
 PyDoc_STRVAR (send_doc,"send(arg) -> send 'arg' into generator,\n\ return next yielded value or raise StopIteration.")
static PyObjectgen_send (PyGenObject *gen, PyObject *arg)
 PyDoc_STRVAR (close_doc,"close(arg) -> raise GeneratorExit inside generator.")
static PyObjectgen_close (PyGenObject *gen, PyObject *args)
static void gen_del (PyObject *self)
 PyDoc_STRVAR (throw_doc,"throw(typ[,val[,tb]]) -> raise exception in generator,\n\ return next yielded value or raise StopIteration.")
static PyObjectgen_throw (PyGenObject *gen, PyObject *args)
static PyObjectgen_iternext (PyGenObject *gen)
static PyObjectgen_repr (PyGenObject *gen)
static PyObjectgen_get_name (PyGenObject *gen)
 PyDoc_STRVAR (gen__name__doc__,"Return the name of the generator's associated code object.")
PyObjectPyGen_New (PyFrameObject *f)
int PyGen_NeedsFinalizing (PyGenObject *gen)

Variables

static PyGetSetDef gen_getsetlist []
static PyMemberDef gen_memberlist []
static PyMethodDef gen_methods []
PyTypeObject PyGen_Type

Function Documentation

static PyObject* gen_close ( PyGenObject gen,
PyObject args 
) [static]

Definition at line 124 of file genobject.c.

{
    PyObject *retval;
    PyErr_SetNone(PyExc_GeneratorExit);
    retval = gen_send_ex(gen, Py_None, 1);
    if (retval) {
        Py_DECREF(retval);
        PyErr_SetString(PyExc_RuntimeError,
                        "generator ignored GeneratorExit");
        return NULL;
    }
    if (PyErr_ExceptionMatches(PyExc_StopIteration)
        || PyErr_ExceptionMatches(PyExc_GeneratorExit))
    {
        PyErr_Clear();          /* ignore these errors */
        Py_INCREF(Py_None);
        return Py_None;
    }
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void gen_dealloc ( PyGenObject gen) [static]

Definition at line 17 of file genobject.c.

{
    PyObject *self = (PyObject *) gen;

    _PyObject_GC_UNTRACK(gen);

    if (gen->gi_weakreflist != NULL)
        PyObject_ClearWeakRefs(self);

    _PyObject_GC_TRACK(self);

    if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
        /* Generator is paused, so we need to close */
        Py_TYPE(gen)->tp_del(self);
        if (self->ob_refcnt > 0)
            return;                     /* resurrected.  :( */
    }

    _PyObject_GC_UNTRACK(self);
    Py_CLEAR(gen->gi_frame);
    Py_CLEAR(gen->gi_code);
    PyObject_GC_Del(gen);
}

Here is the call graph for this function:

static void gen_del ( PyObject self) [static]

Definition at line 146 of file genobject.c.

{
    PyObject *res;
    PyObject *error_type, *error_value, *error_traceback;
    PyGenObject *gen = (PyGenObject *)self;

    if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
        /* Generator isn't paused, so no need to close */
        return;

    /* Temporarily resurrect the object. */
    assert(self->ob_refcnt == 0);
    self->ob_refcnt = 1;

    /* Save the current exception, if any. */
    PyErr_Fetch(&error_type, &error_value, &error_traceback);

    res = gen_close(gen, NULL);

    if (res == NULL)
        PyErr_WriteUnraisable(self);
    else
        Py_DECREF(res);

    /* Restore the saved exception. */
    PyErr_Restore(error_type, error_value, error_traceback);

    /* Undo the temporary resurrection; can't use DECREF here, it would
     * cause a recursive call.
     */
    assert(self->ob_refcnt > 0);
    if (--self->ob_refcnt == 0)
        return; /* this is the normal path out */

    /* close() resurrected it!  Make it look like the original Py_DECREF
     * never happened.
     */
    {
        Py_ssize_t refcnt = self->ob_refcnt;
        _Py_NewReference(self);
        self->ob_refcnt = refcnt;
    }
    assert(PyType_IS_GC(self->ob_type) &&
           _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);

    /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
     * we need to undo that. */
    _Py_DEC_REFTOTAL;
    /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
     * chain, so no more to do there.
     * If COUNT_ALLOCS, the original decref bumped tp_frees, and
     * _Py_NewReference bumped tp_allocs:  both of those need to be
     * undone.
     */
#ifdef COUNT_ALLOCS
    --self->ob_type->tp_frees;
    --self->ob_type->tp_allocs;
#endif
}

Here is the call graph for this function:

static PyObject* gen_get_name ( PyGenObject gen) [static]

Definition at line 293 of file genobject.c.

{
    PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
    Py_INCREF(name);
    return name;
}
static PyObject* gen_iternext ( PyGenObject gen) [static]

Definition at line 277 of file genobject.c.

{
    return gen_send_ex(gen, NULL, 0);
}

Here is the call graph for this function:

static PyObject* gen_repr ( PyGenObject gen) [static]

Definition at line 284 of file genobject.c.

{
    return PyUnicode_FromFormat("<generator object %S at %p>",
                                ((PyCodeObject *)gen->gi_code)->co_name,
                                gen);
}
static PyObject* gen_send ( PyGenObject gen,
PyObject arg 
) [static]

Definition at line 115 of file genobject.c.

{
    return gen_send_ex(gen, arg, 0);
}

Here is the call graph for this function:

static PyObject* gen_send_ex ( PyGenObject gen,
PyObject arg,
int  exc 
) [static]

Definition at line 43 of file genobject.c.

{
    PyThreadState *tstate = PyThreadState_GET();
    PyFrameObject *f = gen->gi_frame;
    PyObject *result;

    if (gen->gi_running) {
        PyErr_SetString(PyExc_ValueError,
                        "generator already executing");
        return NULL;
    }
    if (f==NULL || f->f_stacktop == NULL) {
        /* Only set exception if called from send() */
        if (arg && !exc)
            PyErr_SetNone(PyExc_StopIteration);
        return NULL;
    }

    if (f->f_lasti == -1) {
        if (arg && arg != Py_None) {
            PyErr_SetString(PyExc_TypeError,
                            "can't send non-None value to a "
                            "just-started generator");
            return NULL;
        }
    } else {
        /* Push arg onto the frame's value stack */
        result = arg ? arg : Py_None;
        Py_INCREF(result);
        *(f->f_stacktop++) = result;
    }

    /* Generators always return to their most recent caller, not
     * necessarily their creator. */
    Py_XINCREF(tstate->frame);
    assert(f->f_back == NULL);
    f->f_back = tstate->frame;

    gen->gi_running = 1;
    result = PyEval_EvalFrameEx(f, exc);
    gen->gi_running = 0;

    /* Don't keep the reference to f_back any longer than necessary.  It
     * may keep a chain of frames alive or it could create a reference
     * cycle. */
    assert(f->f_back == tstate->frame);
    Py_CLEAR(f->f_back);

    /* If the generator just returned (as opposed to yielding), signal
     * that the generator is exhausted. */
    if (result == Py_None && f->f_stacktop == NULL) {
        Py_DECREF(result);
        result = NULL;
        /* Set exception if not called by gen_iternext() */
        if (arg)
            PyErr_SetNone(PyExc_StopIteration);
    }

    if (!result || f->f_stacktop == NULL) {
        /* generator can't be rerun, so release the frame */
        Py_DECREF(f);
        gen->gi_frame = NULL;
    }

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* gen_throw ( PyGenObject gen,
PyObject args 
) [static]

Definition at line 213 of file genobject.c.

{
    PyObject *typ;
    PyObject *tb = NULL;
    PyObject *val = NULL;

    if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
        return NULL;

    /* First, check the traceback argument, replacing None with
       NULL. */
    if (tb == Py_None)
        tb = NULL;
    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);

    if (PyExceptionClass_Check(typ)) {
        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> */
            Py_XDECREF(val);
            val = typ;
            typ = PyExceptionInstance_Class(typ);
            Py_INCREF(typ);
        }
    }
    else {
        /* Not something you can raise.  throw() fails. */
        PyErr_Format(PyExc_TypeError,
                     "exceptions must be classes or instances "
                     "deriving from BaseException, not %s",
                     typ->ob_type->tp_name);
            goto failed_throw;
    }

    PyErr_Restore(typ, val, tb);
    return gen_send_ex(gen, Py_None, 1);

failed_throw:
    /* Didn't use our arguments, so restore their original refcounts */
    Py_DECREF(typ);
    Py_XDECREF(val);
    Py_XDECREF(tb);
    return NULL;
}

Here is the call graph for this function:

static int gen_traverse ( PyGenObject gen,
visitproc  visit,
void arg 
) [static]

Definition at line 9 of file genobject.c.

{
    Py_VISIT((PyObject *)gen->gi_frame);
    Py_VISIT(gen->gi_code);
    return 0;
}
PyDoc_STRVAR ( send_doc  ,
"send(arg) -> send 'arg' into  generator,
\n\return next yielded value or raise StopIteration."   
)
PyDoc_STRVAR ( close_doc  ,
"close(arg) -> raise GeneratorExit inside generator."   
)
PyDoc_STRVAR ( throw_doc  ,
"throw(typ[,val[,tb]]) -> raise exception in  generator,
\n\return next yielded value or raise StopIteration."   
)
PyDoc_STRVAR ( gen__name__doc__  ,
"Return the name of the generator's associated code object."   
)

Definition at line 393 of file genobject.c.

{
    int i;
    PyFrameObject *f = gen->gi_frame;

    if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
        return 0; /* no frame or empty blockstack == no finalization */

    /* Any block type besides a loop requires cleanup. */
    i = f->f_iblock;
    while (--i >= 0) {
        if (f->f_blockstack[i].b_type != SETUP_LOOP)
            return 1;
    }

    /* No blocks except loops, it's safe to skip finalization. */
    return 0;
}

Here is the caller graph for this function:

Definition at line 376 of file genobject.c.

{
    PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
    if (gen == NULL) {
        Py_DECREF(f);
        return NULL;
    }
    gen->gi_frame = f;
    Py_INCREF(f->f_code);
    gen->gi_code = (PyObject *)(f->f_code);
    gen->gi_running = 0;
    gen->gi_weakreflist = NULL;
    _PyObject_GC_TRACK(gen);
    return (PyObject *)gen;
}

Here is the caller graph for this function:


Variable Documentation

Initial value:
 {
    {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
    {NULL}
}

Definition at line 304 of file genobject.c.

Initial value:
 {
    {"gi_frame",        T_OBJECT, offsetof(PyGenObject, gi_frame),      READONLY},
    {"gi_running",      T_INT,    offsetof(PyGenObject, gi_running),    READONLY},
    {"gi_code",     T_OBJECT, offsetof(PyGenObject, gi_code),  READONLY},
    {NULL}      
}

Definition at line 310 of file genobject.c.

Initial value:
 {
    {"send",(PyCFunction)gen_send, METH_O, send_doc},
    {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
    {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
    {NULL, NULL}        
}

Definition at line 317 of file genobject.c.

Definition at line 324 of file genobject.c.