blob: 67e6ef9e001e50a1c7b6a3a79941dde964b20842 [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
Antoine Pitrou58720d62013-08-05 23:26:40 +020018void
19_PyGen_Finalize(PyObject *self)
Antoine Pitrou796564c2013-07-30 19:59:21 +020020{
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);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200143 gen->gi_frame->f_gen = NULL;
Antoine Pitrou93963562013-05-14 20:37:52 +0200144 gen->gi_frame = NULL;
145 Py_DECREF(f);
146 }
147
148 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000149}
150
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000151PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152"send(arg) -> send 'arg' into generator,\n\
153return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000154
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500155PyObject *
156_PyGen_Send(PyGenObject *gen, PyObject *arg)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000157{
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500158 return gen_send_ex(gen, arg, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000159}
160
161PyDoc_STRVAR(close_doc,
Benjamin Petersonab3da292012-05-03 18:44:09 -0400162"close() -> raise GeneratorExit inside generator.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000163
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000164/*
165 * This helper function is used by gen_close and gen_throw to
166 * close a subiterator being delegated to by yield-from.
167 */
168
Antoine Pitrou93963562013-05-14 20:37:52 +0200169static int
170gen_close_iter(PyObject *yf)
171{
172 PyObject *retval = NULL;
173 _Py_IDENTIFIER(close);
174
175 if (PyGen_CheckExact(yf)) {
176 retval = gen_close((PyGenObject *)yf, NULL);
177 if (retval == NULL)
178 return -1;
179 } else {
180 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
181 if (meth == NULL) {
182 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
183 PyErr_WriteUnraisable(yf);
184 PyErr_Clear();
185 } else {
186 retval = PyObject_CallFunction(meth, "");
187 Py_DECREF(meth);
188 if (retval == NULL)
189 return -1;
190 }
191 }
192 Py_XDECREF(retval);
193 return 0;
194}
195
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500196static PyObject *
197gen_yf(PyGenObject *gen)
198{
Antoine Pitrou93963562013-05-14 20:37:52 +0200199 PyObject *yf = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500200 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200201
202 if (f && f->f_stacktop) {
203 PyObject *bytecode = f->f_code->co_code;
204 unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
205
206 if (code[f->f_lasti + 1] != YIELD_FROM)
207 return NULL;
208 yf = f->f_stacktop[-1];
209 Py_INCREF(yf);
210 }
211
212 return yf;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500213}
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000214
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000215static PyObject *
216gen_close(PyGenObject *gen, PyObject *args)
217{
Antoine Pitrou93963562013-05-14 20:37:52 +0200218 PyObject *retval;
219 PyObject *yf = gen_yf(gen);
220 int err = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000221
Antoine Pitrou93963562013-05-14 20:37:52 +0200222 if (yf) {
223 gen->gi_running = 1;
224 err = gen_close_iter(yf);
225 gen->gi_running = 0;
226 Py_DECREF(yf);
227 }
228 if (err == 0)
229 PyErr_SetNone(PyExc_GeneratorExit);
230 retval = gen_send_ex(gen, Py_None, 1);
231 if (retval) {
232 Py_DECREF(retval);
233 PyErr_SetString(PyExc_RuntimeError,
234 "generator ignored GeneratorExit");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return NULL;
236 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200237 if (PyErr_ExceptionMatches(PyExc_StopIteration)
238 || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
239 PyErr_Clear(); /* ignore these errors */
240 Py_INCREF(Py_None);
241 return Py_None;
242 }
243 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000244}
245
Antoine Pitrou93963562013-05-14 20:37:52 +0200246
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000247PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
249return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000250
251static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252gen_throw(PyGenObject *gen, PyObject *args)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyObject *typ;
255 PyObject *tb = NULL;
256 PyObject *val = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500257 PyObject *yf = gen_yf(gen);
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000258 _Py_IDENTIFIER(throw);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
261 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000262
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000263 if (yf) {
264 PyObject *ret;
265 int err;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000266 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500267 gen->gi_running = 1;
Antoine Pitrou93963562013-05-14 20:37:52 +0200268 err = gen_close_iter(yf);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500269 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000270 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000271 if (err < 0)
272 return gen_send_ex(gen, Py_None, 1);
273 goto throw_here;
274 }
275 if (PyGen_CheckExact(yf)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500276 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000277 ret = gen_throw((PyGenObject *)yf, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500278 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000279 } else {
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000280 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000281 if (meth == NULL) {
282 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
283 Py_DECREF(yf);
284 return NULL;
285 }
286 PyErr_Clear();
287 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000288 goto throw_here;
289 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500290 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000291 ret = PyObject_CallObject(meth, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500292 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000293 Py_DECREF(meth);
294 }
295 Py_DECREF(yf);
296 if (!ret) {
297 PyObject *val;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500298 /* Pop subiterator from stack */
299 ret = *(--gen->gi_frame->f_stacktop);
300 assert(ret == yf);
301 Py_DECREF(ret);
302 /* Termination repetition of YIELD_FROM */
303 gen->gi_frame->f_lasti++;
Nick Coghlanc40bc092012-06-17 15:15:49 +1000304 if (_PyGen_FetchStopIterationValue(&val) == 0) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000305 ret = gen_send_ex(gen, val, 0);
306 Py_DECREF(val);
307 } else {
308 ret = gen_send_ex(gen, Py_None, 1);
309 }
310 }
311 return ret;
312 }
313
314throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 /* First, check the traceback argument, replacing None with
316 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400317 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 else if (tb != NULL && !PyTraceBack_Check(tb)) {
321 PyErr_SetString(PyExc_TypeError,
322 "throw() third argument must be a traceback object");
323 return NULL;
324 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_INCREF(typ);
327 Py_XINCREF(val);
328 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000329
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400330 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 else if (PyExceptionInstance_Check(typ)) {
334 /* Raising an instance. The value should be a dummy. */
335 if (val && val != Py_None) {
336 PyErr_SetString(PyExc_TypeError,
337 "instance exception may not have a separate value");
338 goto failed_throw;
339 }
340 else {
341 /* Normalize to raise <class>, <instance> */
342 Py_XDECREF(val);
343 val = typ;
344 typ = PyExceptionInstance_Class(typ);
345 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200346
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400347 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200348 /* Returns NULL if there's no traceback */
349 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 }
351 }
352 else {
353 /* Not something you can raise. throw() fails. */
354 PyErr_Format(PyExc_TypeError,
355 "exceptions must be classes or instances "
356 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000357 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 goto failed_throw;
359 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyErr_Restore(typ, val, tb);
362 return gen_send_ex(gen, Py_None, 1);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000363
364failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* Didn't use our arguments, so restore their original refcounts */
366 Py_DECREF(typ);
367 Py_XDECREF(val);
368 Py_XDECREF(tb);
369 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000370}
371
372
373static PyObject *
374gen_iternext(PyGenObject *gen)
375{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000376 PyObject *val = NULL;
377 PyObject *ret;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500378 ret = gen_send_ex(gen, val, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000379 Py_XDECREF(val);
380 return ret;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000381}
382
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000383/*
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000384 * If StopIteration exception is set, fetches its 'value'
385 * attribute if any, otherwise sets pvalue to None.
386 *
387 * Returns 0 if no exception or StopIteration is set.
388 * If any other exception is set, returns -1 and leaves
389 * pvalue unchanged.
390 */
391
392int
Nick Coghlanc40bc092012-06-17 15:15:49 +1000393_PyGen_FetchStopIterationValue(PyObject **pvalue) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000394 PyObject *et, *ev, *tb;
395 PyObject *value = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500396
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000397 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
398 PyErr_Fetch(&et, &ev, &tb);
Antoine Pitrou7403e912015-04-26 18:46:40 +0200399 if (ev) {
400 /* exception will usually be normalised already */
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300401 if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200402 value = ((PyStopIterationObject *)ev)->value;
403 Py_INCREF(value);
404 Py_DECREF(ev);
405 } else if (et == PyExc_StopIteration) {
406 /* avoid normalisation and take ev as value */
407 value = ev;
408 } else {
409 /* normalisation required */
410 PyErr_NormalizeException(&et, &ev, &tb);
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300411 if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200412 PyErr_Restore(et, ev, tb);
413 return -1;
414 }
415 value = ((PyStopIterationObject *)ev)->value;
416 Py_INCREF(value);
417 Py_DECREF(ev);
418 }
419 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000420 Py_XDECREF(et);
421 Py_XDECREF(tb);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000422 } else if (PyErr_Occurred()) {
423 return -1;
424 }
425 if (value == NULL) {
426 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100427 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000428 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000429 *pvalue = value;
430 return 0;
431}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000432
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000433static PyObject *
434gen_repr(PyGenObject *gen)
435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 return PyUnicode_FromFormat("<generator object %S at %p>",
437 ((PyCodeObject *)gen->gi_code)->co_name,
438 gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000439}
440
441
442static PyObject *
443gen_get_name(PyGenObject *gen)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
446 Py_INCREF(name);
447 return name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000448}
449
450
451PyDoc_STRVAR(gen__name__doc__,
452"Return the name of the generator's associated code object.");
453
454static PyGetSetDef gen_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
456 {NULL}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000457};
458
459
Martin v. Löwise440e472004-06-01 15:22:42 +0000460static PyMemberDef gen_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
Benjamin Peterson657e9eb2012-03-07 18:17:03 -0600462 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
464 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000465};
466
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000467static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500468 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
470 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
471 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000472};
473
Martin v. Löwise440e472004-06-01 15:22:42 +0000474PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyVarObject_HEAD_INIT(&PyType_Type, 0)
476 "generator", /* tp_name */
477 sizeof(PyGenObject), /* tp_basicsize */
478 0, /* tp_itemsize */
479 /* methods */
480 (destructor)gen_dealloc, /* tp_dealloc */
481 0, /* tp_print */
482 0, /* tp_getattr */
483 0, /* tp_setattr */
484 0, /* tp_reserved */
485 (reprfunc)gen_repr, /* tp_repr */
486 0, /* tp_as_number */
487 0, /* tp_as_sequence */
488 0, /* tp_as_mapping */
489 0, /* tp_hash */
490 0, /* tp_call */
491 0, /* tp_str */
492 PyObject_GenericGetAttr, /* tp_getattro */
493 0, /* tp_setattro */
494 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200495 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
496 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 0, /* tp_doc */
498 (traverseproc)gen_traverse, /* tp_traverse */
499 0, /* tp_clear */
500 0, /* tp_richcompare */
501 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
502 PyObject_SelfIter, /* tp_iter */
503 (iternextfunc)gen_iternext, /* tp_iternext */
504 gen_methods, /* tp_methods */
505 gen_memberlist, /* tp_members */
506 gen_getsetlist, /* tp_getset */
507 0, /* tp_base */
508 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 0, /* tp_descr_get */
511 0, /* tp_descr_set */
512 0, /* tp_dictoffset */
513 0, /* tp_init */
514 0, /* tp_alloc */
515 0, /* tp_new */
516 0, /* tp_free */
517 0, /* tp_is_gc */
518 0, /* tp_bases */
519 0, /* tp_mro */
520 0, /* tp_cache */
521 0, /* tp_subclasses */
522 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200523 0, /* tp_del */
524 0, /* tp_version_tag */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200525 _PyGen_Finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000526};
527
528PyObject *
529PyGen_New(PyFrameObject *f)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
532 if (gen == NULL) {
533 Py_DECREF(f);
534 return NULL;
535 }
536 gen->gi_frame = f;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200537 f->f_gen = (PyObject *) gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 Py_INCREF(f->f_code);
539 gen->gi_code = (PyObject *)(f->f_code);
540 gen->gi_running = 0;
541 gen->gi_weakreflist = NULL;
542 _PyObject_GC_TRACK(gen);
543 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000544}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545
546int
547PyGen_NeedsFinalizing(PyGenObject *gen)
548{
Antoine Pitrou93963562013-05-14 20:37:52 +0200549 int i;
550 PyFrameObject *f = gen->gi_frame;
551
552 if (f == NULL || f->f_stacktop == NULL)
553 return 0; /* no frame or empty blockstack == no finalization */
554
555 /* Any block type besides a loop requires cleanup. */
556 for (i = 0; i < f->f_iblock; i++)
557 if (f->f_blockstack[i].b_type != SETUP_LOOP)
558 return 1;
559
560 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562}