blob: dfd90aa4b1be9dc4a84c5f682f4b8fc68884ce1b [file] [log] [blame]
Martin v. Löwise440e472004-06-01 15:22:42 +00001/* Generator object implementation */
2
3#include "Python.h"
4#include "frameobject.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00005#include "structmember.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006#include "opcode.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00007
Nick Coghlan1f7ce622012-01-13 21:43:40 +10008static PyObject *gen_close(PyGenObject *gen, PyObject *args);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10009
Martin v. Löwise440e472004-06-01 15:22:42 +000010static int
11gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
12{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 Py_VISIT((PyObject *)gen->gi_frame);
14 Py_VISIT(gen->gi_code);
15 return 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000016}
17
18static void
Antoine Pitrou796564c2013-07-30 19:59:21 +020019gen_finalize(PyObject *self)
20{
21 PyGenObject *gen = (PyGenObject *)self;
22 PyObject *res;
23 PyObject *error_type, *error_value, *error_traceback;
24
25 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
26 /* Generator isn't paused, so no need to close */
27 return;
28
29 /* Save the current exception, if any. */
30 PyErr_Fetch(&error_type, &error_value, &error_traceback);
31
32 res = gen_close(gen, NULL);
33
34 if (res == NULL)
35 PyErr_WriteUnraisable(self);
36 else
37 Py_DECREF(res);
38
39 /* Restore the saved exception. */
40 PyErr_Restore(error_type, error_value, error_traceback);
41}
42
43static void
Martin v. Löwise440e472004-06-01 15:22:42 +000044gen_dealloc(PyGenObject *gen)
45{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 PyObject *self = (PyObject *) gen;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 _PyObject_GC_UNTRACK(gen);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 if (gen->gi_weakreflist != NULL)
51 PyObject_ClearWeakRefs(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000052
Antoine Pitrou93963562013-05-14 20:37:52 +020053 _PyObject_GC_TRACK(self);
54
Antoine Pitrou796564c2013-07-30 19:59:21 +020055 if (PyObject_CallFinalizerFromDealloc(self))
56 return; /* resurrected. :( */
Antoine Pitrou93963562013-05-14 20:37:52 +020057
58 _PyObject_GC_UNTRACK(self);
59 Py_CLEAR(gen->gi_frame);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 Py_CLEAR(gen->gi_code);
61 PyObject_GC_Del(gen);
Martin v. Löwise440e472004-06-01 15:22:42 +000062}
63
64static PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000065gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
Martin v. Löwise440e472004-06-01 15:22:42 +000066{
Antoine Pitrou93963562013-05-14 20:37:52 +020067 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +020069 PyObject *result;
Martin v. Löwise440e472004-06-01 15:22:42 +000070
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -050071 if (gen->gi_running) {
72 PyErr_SetString(PyExc_ValueError,
73 "generator already executing");
74 return NULL;
75 }
Antoine Pitrou93963562013-05-14 20:37:52 +020076 if (f == NULL || f->f_stacktop == NULL) {
77 /* Only set exception if called from send() */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (arg && !exc)
79 PyErr_SetNone(PyExc_StopIteration);
80 return NULL;
81 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000082
Antoine Pitrou93963562013-05-14 20:37:52 +020083 if (f->f_lasti == -1) {
84 if (arg && arg != Py_None) {
85 PyErr_SetString(PyExc_TypeError,
86 "can't send non-None value to a "
87 "just-started generator");
88 return NULL;
89 }
90 } else {
91 /* Push arg onto the frame's value stack */
92 result = arg ? arg : Py_None;
93 Py_INCREF(result);
94 *(f->f_stacktop++) = result;
95 }
96
97 /* Generators always return to their most recent caller, not
98 * necessarily their creator. */
99 Py_XINCREF(tstate->frame);
100 assert(f->f_back == NULL);
101 f->f_back = tstate->frame;
102
103 gen->gi_running = 1;
104 result = PyEval_EvalFrameEx(f, exc);
105 gen->gi_running = 0;
106
107 /* Don't keep the reference to f_back any longer than necessary. It
108 * may keep a chain of frames alive or it could create a reference
109 * cycle. */
110 assert(f->f_back == tstate->frame);
111 Py_CLEAR(f->f_back);
112
113 /* If the generator just returned (as opposed to yielding), signal
114 * that the generator is exhausted. */
115 if (result && f->f_stacktop == NULL) {
116 if (result == Py_None) {
117 /* Delay exception instantiation if we can */
118 PyErr_SetNone(PyExc_StopIteration);
119 } else {
120 PyObject *e = PyObject_CallFunctionObjArgs(
121 PyExc_StopIteration, result, NULL);
122 if (e != NULL) {
123 PyErr_SetObject(PyExc_StopIteration, e);
124 Py_DECREF(e);
125 }
126 }
127 Py_CLEAR(result);
128 }
129
130 if (!result || f->f_stacktop == NULL) {
131 /* generator can't be rerun, so release the frame */
132 /* first clean reference cycle through stored exception traceback */
133 PyObject *t, *v, *tb;
134 t = f->f_exc_type;
135 v = f->f_exc_value;
136 tb = f->f_exc_traceback;
137 f->f_exc_type = NULL;
138 f->f_exc_value = NULL;
139 f->f_exc_traceback = NULL;
140 Py_XDECREF(t);
141 Py_XDECREF(v);
142 Py_XDECREF(tb);
143 gen->gi_frame = NULL;
144 Py_DECREF(f);
145 }
146
147 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000148}
149
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000150PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151"send(arg) -> send 'arg' into generator,\n\
152return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000153
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500154PyObject *
155_PyGen_Send(PyGenObject *gen, PyObject *arg)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000156{
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500157 return gen_send_ex(gen, arg, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000158}
159
160PyDoc_STRVAR(close_doc,
Benjamin Petersonab3da292012-05-03 18:44:09 -0400161"close() -> raise GeneratorExit inside generator.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000162
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000163/*
164 * This helper function is used by gen_close and gen_throw to
165 * close a subiterator being delegated to by yield-from.
166 */
167
Antoine Pitrou93963562013-05-14 20:37:52 +0200168static int
169gen_close_iter(PyObject *yf)
170{
171 PyObject *retval = NULL;
172 _Py_IDENTIFIER(close);
173
174 if (PyGen_CheckExact(yf)) {
175 retval = gen_close((PyGenObject *)yf, NULL);
176 if (retval == NULL)
177 return -1;
178 } else {
179 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
180 if (meth == NULL) {
181 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
182 PyErr_WriteUnraisable(yf);
183 PyErr_Clear();
184 } else {
185 retval = PyObject_CallFunction(meth, "");
186 Py_DECREF(meth);
187 if (retval == NULL)
188 return -1;
189 }
190 }
191 Py_XDECREF(retval);
192 return 0;
193}
194
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500195static PyObject *
196gen_yf(PyGenObject *gen)
197{
Antoine Pitrou93963562013-05-14 20:37:52 +0200198 PyObject *yf = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500199 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200200
201 if (f && f->f_stacktop) {
202 PyObject *bytecode = f->f_code->co_code;
203 unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
204
205 if (code[f->f_lasti + 1] != YIELD_FROM)
206 return NULL;
207 yf = f->f_stacktop[-1];
208 Py_INCREF(yf);
209 }
210
211 return yf;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500212}
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000213
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000214static PyObject *
215gen_close(PyGenObject *gen, PyObject *args)
216{
Antoine Pitrou93963562013-05-14 20:37:52 +0200217 PyObject *retval;
218 PyObject *yf = gen_yf(gen);
219 int err = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000220
Antoine Pitrou93963562013-05-14 20:37:52 +0200221 if (yf) {
222 gen->gi_running = 1;
223 err = gen_close_iter(yf);
224 gen->gi_running = 0;
225 Py_DECREF(yf);
226 }
227 if (err == 0)
228 PyErr_SetNone(PyExc_GeneratorExit);
229 retval = gen_send_ex(gen, Py_None, 1);
230 if (retval) {
231 Py_DECREF(retval);
232 PyErr_SetString(PyExc_RuntimeError,
233 "generator ignored GeneratorExit");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 return NULL;
235 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200236 if (PyErr_ExceptionMatches(PyExc_StopIteration)
237 || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
238 PyErr_Clear(); /* ignore these errors */
239 Py_INCREF(Py_None);
240 return Py_None;
241 }
242 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000243}
244
Antoine Pitrou93963562013-05-14 20:37:52 +0200245
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000246PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
248return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000249
250static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251gen_throw(PyGenObject *gen, PyObject *args)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 PyObject *typ;
254 PyObject *tb = NULL;
255 PyObject *val = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500256 PyObject *yf = gen_yf(gen);
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000257 _Py_IDENTIFIER(throw);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
260 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000261
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000262 if (yf) {
263 PyObject *ret;
264 int err;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000265 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500266 gen->gi_running = 1;
Antoine Pitrou93963562013-05-14 20:37:52 +0200267 err = gen_close_iter(yf);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500268 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000269 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000270 if (err < 0)
271 return gen_send_ex(gen, Py_None, 1);
272 goto throw_here;
273 }
274 if (PyGen_CheckExact(yf)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500275 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000276 ret = gen_throw((PyGenObject *)yf, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500277 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000278 } else {
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000279 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000280 if (meth == NULL) {
281 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
282 Py_DECREF(yf);
283 return NULL;
284 }
285 PyErr_Clear();
286 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000287 goto throw_here;
288 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500289 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000290 ret = PyObject_CallObject(meth, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500291 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000292 Py_DECREF(meth);
293 }
294 Py_DECREF(yf);
295 if (!ret) {
296 PyObject *val;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500297 /* Pop subiterator from stack */
298 ret = *(--gen->gi_frame->f_stacktop);
299 assert(ret == yf);
300 Py_DECREF(ret);
301 /* Termination repetition of YIELD_FROM */
302 gen->gi_frame->f_lasti++;
Nick Coghlanc40bc092012-06-17 15:15:49 +1000303 if (_PyGen_FetchStopIterationValue(&val) == 0) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000304 ret = gen_send_ex(gen, val, 0);
305 Py_DECREF(val);
306 } else {
307 ret = gen_send_ex(gen, Py_None, 1);
308 }
309 }
310 return ret;
311 }
312
313throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* First, check the traceback argument, replacing None with
315 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400316 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 else if (tb != NULL && !PyTraceBack_Check(tb)) {
320 PyErr_SetString(PyExc_TypeError,
321 "throw() third argument must be a traceback object");
322 return NULL;
323 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 Py_INCREF(typ);
326 Py_XINCREF(val);
327 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000328
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400329 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 else if (PyExceptionInstance_Check(typ)) {
333 /* Raising an instance. The value should be a dummy. */
334 if (val && val != Py_None) {
335 PyErr_SetString(PyExc_TypeError,
336 "instance exception may not have a separate value");
337 goto failed_throw;
338 }
339 else {
340 /* Normalize to raise <class>, <instance> */
341 Py_XDECREF(val);
342 val = typ;
343 typ = PyExceptionInstance_Class(typ);
344 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200345
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400346 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200347 /* Returns NULL if there's no traceback */
348 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 }
350 }
351 else {
352 /* Not something you can raise. throw() fails. */
353 PyErr_Format(PyExc_TypeError,
354 "exceptions must be classes or instances "
355 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000356 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 goto failed_throw;
358 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 PyErr_Restore(typ, val, tb);
361 return gen_send_ex(gen, Py_None, 1);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000362
363failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 /* Didn't use our arguments, so restore their original refcounts */
365 Py_DECREF(typ);
366 Py_XDECREF(val);
367 Py_XDECREF(tb);
368 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000369}
370
371
372static PyObject *
373gen_iternext(PyGenObject *gen)
374{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000375 PyObject *val = NULL;
376 PyObject *ret;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500377 ret = gen_send_ex(gen, val, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000378 Py_XDECREF(val);
379 return ret;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000380}
381
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000382/*
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000383 * If StopIteration exception is set, fetches its 'value'
384 * attribute if any, otherwise sets pvalue to None.
385 *
386 * Returns 0 if no exception or StopIteration is set.
387 * If any other exception is set, returns -1 and leaves
388 * pvalue unchanged.
389 */
390
391int
Nick Coghlanc40bc092012-06-17 15:15:49 +1000392_PyGen_FetchStopIterationValue(PyObject **pvalue) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000393 PyObject *et, *ev, *tb;
394 PyObject *value = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500395
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000396 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
397 PyErr_Fetch(&et, &ev, &tb);
398 Py_XDECREF(et);
399 Py_XDECREF(tb);
400 if (ev) {
401 value = ((PyStopIterationObject *)ev)->value;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100402 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000403 Py_DECREF(ev);
404 }
405 } else if (PyErr_Occurred()) {
406 return -1;
407 }
408 if (value == NULL) {
409 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100410 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000411 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000412 *pvalue = value;
413 return 0;
414}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000415
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000416static PyObject *
417gen_repr(PyGenObject *gen)
418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 return PyUnicode_FromFormat("<generator object %S at %p>",
420 ((PyCodeObject *)gen->gi_code)->co_name,
421 gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000422}
423
424
425static PyObject *
426gen_get_name(PyGenObject *gen)
427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
429 Py_INCREF(name);
430 return name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000431}
432
433
434PyDoc_STRVAR(gen__name__doc__,
435"Return the name of the generator's associated code object.");
436
437static PyGetSetDef gen_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
439 {NULL}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000440};
441
442
Martin v. Löwise440e472004-06-01 15:22:42 +0000443static PyMemberDef gen_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
Benjamin Peterson657e9eb2012-03-07 18:17:03 -0600445 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
447 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000448};
449
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000450static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500451 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
453 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
454 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000455};
456
Martin v. Löwise440e472004-06-01 15:22:42 +0000457PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyVarObject_HEAD_INIT(&PyType_Type, 0)
459 "generator", /* tp_name */
460 sizeof(PyGenObject), /* tp_basicsize */
461 0, /* tp_itemsize */
462 /* methods */
463 (destructor)gen_dealloc, /* tp_dealloc */
464 0, /* tp_print */
465 0, /* tp_getattr */
466 0, /* tp_setattr */
467 0, /* tp_reserved */
468 (reprfunc)gen_repr, /* tp_repr */
469 0, /* tp_as_number */
470 0, /* tp_as_sequence */
471 0, /* tp_as_mapping */
472 0, /* tp_hash */
473 0, /* tp_call */
474 0, /* tp_str */
475 PyObject_GenericGetAttr, /* tp_getattro */
476 0, /* tp_setattro */
477 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200478 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
479 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 0, /* tp_doc */
481 (traverseproc)gen_traverse, /* tp_traverse */
482 0, /* tp_clear */
483 0, /* tp_richcompare */
484 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
485 PyObject_SelfIter, /* tp_iter */
486 (iternextfunc)gen_iternext, /* tp_iternext */
487 gen_methods, /* tp_methods */
488 gen_memberlist, /* tp_members */
489 gen_getsetlist, /* tp_getset */
490 0, /* tp_base */
491 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 0, /* tp_descr_get */
494 0, /* tp_descr_set */
495 0, /* tp_dictoffset */
496 0, /* tp_init */
497 0, /* tp_alloc */
498 0, /* tp_new */
499 0, /* tp_free */
500 0, /* tp_is_gc */
501 0, /* tp_bases */
502 0, /* tp_mro */
503 0, /* tp_cache */
504 0, /* tp_subclasses */
505 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200506 0, /* tp_del */
507 0, /* tp_version_tag */
508 gen_finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000509};
510
511PyObject *
512PyGen_New(PyFrameObject *f)
513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
515 if (gen == NULL) {
516 Py_DECREF(f);
517 return NULL;
518 }
519 gen->gi_frame = f;
520 Py_INCREF(f->f_code);
521 gen->gi_code = (PyObject *)(f->f_code);
522 gen->gi_running = 0;
523 gen->gi_weakreflist = NULL;
524 _PyObject_GC_TRACK(gen);
525 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000526}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527
528int
529PyGen_NeedsFinalizing(PyGenObject *gen)
530{
Antoine Pitrou93963562013-05-14 20:37:52 +0200531 int i;
532 PyFrameObject *f = gen->gi_frame;
533
534 if (f == NULL || f->f_stacktop == NULL)
535 return 0; /* no frame or empty blockstack == no finalization */
536
537 /* Any block type besides a loop requires cleanup. */
538 for (i = 0; i < f->f_iblock; i++)
539 if (f->f_blockstack[i].b_type != SETUP_LOOP)
540 return 1;
541
542 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544}