Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 1 | /* Generator object implementation */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | #include "frameobject.h" |
| 5 | #include "genobject.h" |
| 6 | #include "ceval.h" |
| 7 | #include "structmember.h" |
| 8 | |
| 9 | static int |
| 10 | gen_traverse(PyGenObject *gen, visitproc visit, void *arg) |
| 11 | { |
| 12 | return visit((PyObject *)gen->gi_frame, arg); |
| 13 | } |
| 14 | |
| 15 | static void |
| 16 | gen_dealloc(PyGenObject *gen) |
| 17 | { |
| 18 | _PyObject_GC_UNTRACK(gen); |
| 19 | if (gen->gi_weakreflist != NULL) |
| 20 | PyObject_ClearWeakRefs((PyObject *) gen); |
| 21 | Py_DECREF(gen->gi_frame); |
| 22 | PyObject_GC_Del(gen); |
| 23 | } |
| 24 | |
| 25 | static PyObject * |
| 26 | gen_iternext(PyGenObject *gen) |
| 27 | { |
| 28 | PyThreadState *tstate = PyThreadState_GET(); |
| 29 | PyFrameObject *f = gen->gi_frame; |
| 30 | PyObject *result; |
| 31 | |
| 32 | if (gen->gi_running) { |
| 33 | PyErr_SetString(PyExc_ValueError, |
| 34 | "generator already executing"); |
| 35 | return NULL; |
| 36 | } |
| 37 | if (f->f_stacktop == NULL) |
| 38 | return NULL; |
| 39 | |
| 40 | /* Generators always return to their most recent caller, not |
| 41 | * necessarily their creator. */ |
| 42 | Py_XINCREF(tstate->frame); |
| 43 | assert(f->f_back == NULL); |
| 44 | f->f_back = tstate->frame; |
| 45 | |
| 46 | gen->gi_running = 1; |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 47 | result = PyEval_EvalFrame(f); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 48 | gen->gi_running = 0; |
| 49 | |
| 50 | /* Don't keep the reference to f_back any longer than necessary. It |
| 51 | * may keep a chain of frames alive or it could create a reference |
| 52 | * cycle. */ |
Raymond Hettinger | 6c7a00f | 2004-06-12 05:17:55 +0000 | [diff] [blame] | 53 | assert(f->f_back != NULL); |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 54 | Py_CLEAR(f->f_back); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 55 | |
| 56 | /* If the generator just returned (as opposed to yielding), signal |
| 57 | * that the generator is exhausted. */ |
| 58 | if (result == Py_None && f->f_stacktop == NULL) { |
| 59 | Py_DECREF(result); |
| 60 | result = NULL; |
| 61 | } |
| 62 | |
| 63 | return result; |
| 64 | } |
| 65 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 66 | static PyMemberDef gen_memberlist[] = { |
| 67 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), RO}, |
| 68 | {"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO}, |
| 69 | {NULL} /* Sentinel */ |
| 70 | }; |
| 71 | |
| 72 | PyTypeObject PyGen_Type = { |
| 73 | PyObject_HEAD_INIT(&PyType_Type) |
| 74 | 0, /* ob_size */ |
| 75 | "generator", /* tp_name */ |
| 76 | sizeof(PyGenObject), /* tp_basicsize */ |
| 77 | 0, /* tp_itemsize */ |
| 78 | /* methods */ |
| 79 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 80 | 0, /* tp_print */ |
| 81 | 0, /* tp_getattr */ |
| 82 | 0, /* tp_setattr */ |
| 83 | 0, /* tp_compare */ |
| 84 | 0, /* tp_repr */ |
| 85 | 0, /* tp_as_number */ |
| 86 | 0, /* tp_as_sequence */ |
| 87 | 0, /* tp_as_mapping */ |
| 88 | 0, /* tp_hash */ |
| 89 | 0, /* tp_call */ |
| 90 | 0, /* tp_str */ |
| 91 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 92 | 0, /* tp_setattro */ |
| 93 | 0, /* tp_as_buffer */ |
| 94 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 95 | 0, /* tp_doc */ |
| 96 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 97 | 0, /* tp_clear */ |
| 98 | 0, /* tp_richcompare */ |
| 99 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 6c7a00f | 2004-06-12 05:17:55 +0000 | [diff] [blame] | 100 | PyObject_SelfIter, /* tp_iter */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 101 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 102 | 0, /* tp_methods */ |
| 103 | gen_memberlist, /* tp_members */ |
| 104 | 0, /* tp_getset */ |
| 105 | 0, /* tp_base */ |
| 106 | 0, /* tp_dict */ |
| 107 | }; |
| 108 | |
| 109 | PyObject * |
| 110 | PyGen_New(PyFrameObject *f) |
| 111 | { |
| 112 | PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type); |
| 113 | if (gen == NULL) { |
| 114 | Py_DECREF(f); |
| 115 | return NULL; |
| 116 | } |
| 117 | gen->gi_frame = f; |
| 118 | gen->gi_running = 0; |
| 119 | gen->gi_weakreflist = NULL; |
| 120 | _PyObject_GC_TRACK(gen); |
| 121 | return (PyObject *)gen; |
| 122 | } |