blob: b32d9b68fc3cd939f916d4333a2d5515335f8d5a [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);
9static void gen_undelegate(PyGenObject *gen);
10
Martin v. Löwise440e472004-06-01 15:22:42 +000011static int
12gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
13{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 Py_VISIT((PyObject *)gen->gi_frame);
15 Py_VISIT(gen->gi_code);
16 return 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000017}
18
19static void
20gen_dealloc(PyGenObject *gen)
21{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 PyObject *self = (PyObject *) gen;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 _PyObject_GC_UNTRACK(gen);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (gen->gi_weakreflist != NULL)
27 PyObject_ClearWeakRefs(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 _PyObject_GC_TRACK(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
32 /* Generator is paused, so we need to close */
33 Py_TYPE(gen)->tp_del(self);
34 if (self->ob_refcnt > 0)
35 return; /* resurrected. :( */
36 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 _PyObject_GC_UNTRACK(self);
39 Py_CLEAR(gen->gi_frame);
40 Py_CLEAR(gen->gi_code);
41 PyObject_GC_Del(gen);
Martin v. Löwise440e472004-06-01 15:22:42 +000042}
43
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000044
Martin v. Löwise440e472004-06-01 15:22:42 +000045static PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000046gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
Martin v. Löwise440e472004-06-01 15:22:42 +000047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 PyThreadState *tstate = PyThreadState_GET();
49 PyFrameObject *f = gen->gi_frame;
50 PyObject *result;
Martin v. Löwise440e472004-06-01 15:22:42 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (gen->gi_running) {
53 PyErr_SetString(PyExc_ValueError,
54 "generator already executing");
55 return NULL;
56 }
57 if (f==NULL || f->f_stacktop == NULL) {
58 /* Only set exception if called from send() */
59 if (arg && !exc)
60 PyErr_SetNone(PyExc_StopIteration);
61 return NULL;
62 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if (f->f_lasti == -1) {
65 if (arg && arg != Py_None) {
66 PyErr_SetString(PyExc_TypeError,
67 "can't send non-None value to a "
68 "just-started generator");
69 return NULL;
70 }
71 } else {
72 /* Push arg onto the frame's value stack */
73 result = arg ? arg : Py_None;
74 Py_INCREF(result);
75 *(f->f_stacktop++) = result;
76 }
Martin v. Löwise440e472004-06-01 15:22:42 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 /* Generators always return to their most recent caller, not
79 * necessarily their creator. */
80 Py_XINCREF(tstate->frame);
81 assert(f->f_back == NULL);
82 f->f_back = tstate->frame;
Martin v. Löwise440e472004-06-01 15:22:42 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 gen->gi_running = 1;
85 result = PyEval_EvalFrameEx(f, exc);
86 gen->gi_running = 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 /* Don't keep the reference to f_back any longer than necessary. It
89 * may keep a chain of frames alive or it could create a reference
90 * cycle. */
91 assert(f->f_back == tstate->frame);
92 Py_CLEAR(f->f_back);
Martin v. Löwise440e472004-06-01 15:22:42 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 /* If the generator just returned (as opposed to yielding), signal
95 * that the generator is exhausted. */
Nick Coghlan1f7ce622012-01-13 21:43:40 +100096 if (result && f->f_stacktop == NULL) {
97 if (result == Py_None) {
98 /* Delay exception instantiation if we can */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 PyErr_SetNone(PyExc_StopIteration);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000100 } else {
101 PyObject *e = PyStopIteration_Create(result);
102 if (e != NULL) {
103 PyErr_SetObject(PyExc_StopIteration, e);
104 Py_DECREF(e);
105 }
106 }
107 Py_CLEAR(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 if (!result || f->f_stacktop == NULL) {
111 /* generator can't be rerun, so release the frame */
Antoine Pitroua370fcf2011-08-20 14:15:03 +0200112 /* first clean reference cycle through stored exception traceback */
113 PyObject *t, *v, *tb;
114 t = f->f_exc_type;
115 v = f->f_exc_value;
116 tb = f->f_exc_traceback;
117 f->f_exc_type = NULL;
118 f->f_exc_value = NULL;
119 f->f_exc_traceback = NULL;
120 Py_XDECREF(t);
121 Py_XDECREF(v);
122 Py_XDECREF(tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 gen->gi_frame = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000124 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 }
Martin v. Löwise440e472004-06-01 15:22:42 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000128}
129
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000130PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131"send(arg) -> send 'arg' into generator,\n\
132return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000133
134static PyObject *
135gen_send(PyGenObject *gen, PyObject *arg)
136{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000137 int exc = 0;
138 PyObject *ret;
139 PyObject *yf = gen->gi_frame ? gen->gi_frame->f_yieldfrom : NULL;
140 /* XXX (ncoghlan): Are the incref/decref on arg and yf strictly needed?
141 * Or would it be valid to rely on borrowed references?
142 */
143 Py_INCREF(arg);
144 if (yf) {
145 Py_INCREF(yf);
146 if (PyGen_CheckExact(yf)) {
147 ret = gen_send((PyGenObject *)yf, arg);
148 } else {
149 if (arg == Py_None)
150 ret = PyIter_Next(yf);
151 else
152 ret = PyObject_CallMethod(yf, "send", "O", arg);
153 }
154 if (ret) {
155 Py_DECREF(yf);
156 goto done;
157 }
158 gen_undelegate(gen);
159 Py_CLEAR(arg);
160 if (PyGen_FetchStopIterationValue(&arg) < 0) {
161 exc = 1;
162 }
163 Py_DECREF(yf);
164 }
165 ret = gen_send_ex(gen, arg, exc);
166done:
167 Py_XDECREF(arg);
168 return ret;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000169}
170
171PyDoc_STRVAR(close_doc,
172"close(arg) -> raise GeneratorExit inside generator.");
173
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000174/*
175 * This helper function is used by gen_close and gen_throw to
176 * close a subiterator being delegated to by yield-from.
177 */
178
179static int
180gen_close_iter(PyObject *yf)
181{
182 PyObject *retval = NULL;
183
184 if (PyGen_CheckExact(yf)) {
185 retval = gen_close((PyGenObject *)yf, NULL);
186 if (retval == NULL) {
187 return -1;
188 }
189 } else {
190 PyObject *meth = PyObject_GetAttrString(yf, "close");
191 if (meth == NULL) {
192 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
193 PyErr_WriteUnraisable(yf);
194 }
195 PyErr_Clear();
196 } else {
197 retval = PyObject_CallFunction(meth, "");
198 Py_DECREF(meth);
199 if (!retval)
200 return -1;
201 }
202 }
203 Py_XDECREF(retval);
204 return 0;
205}
206
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000207static PyObject *
208gen_close(PyGenObject *gen, PyObject *args)
209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 PyObject *retval;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000211 PyObject *yf = gen->gi_frame ? gen->gi_frame->f_yieldfrom : NULL;
212 int err = 0;
213
214 if (yf) {
215 Py_INCREF(yf);
216 err = gen_close_iter(yf);
217 gen_undelegate(gen);
218 Py_DECREF(yf);
219 }
220 if (err == 0)
221 PyErr_SetNone(PyExc_GeneratorExit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 retval = gen_send_ex(gen, Py_None, 1);
223 if (retval) {
224 Py_DECREF(retval);
225 PyErr_SetString(PyExc_RuntimeError,
226 "generator ignored GeneratorExit");
227 return NULL;
228 }
229 if (PyErr_ExceptionMatches(PyExc_StopIteration)
230 || PyErr_ExceptionMatches(PyExc_GeneratorExit))
231 {
232 PyErr_Clear(); /* ignore these errors */
233 Py_INCREF(Py_None);
234 return Py_None;
235 }
236 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000237}
238
239static void
240gen_del(PyObject *self)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyObject *res;
243 PyObject *error_type, *error_value, *error_traceback;
244 PyGenObject *gen = (PyGenObject *)self;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
247 /* Generator isn't paused, so no need to close */
248 return;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 /* Temporarily resurrect the object. */
251 assert(self->ob_refcnt == 0);
252 self->ob_refcnt = 1;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 /* Save the current exception, if any. */
255 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 res = gen_close(gen, NULL);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (res == NULL)
260 PyErr_WriteUnraisable(self);
261 else
262 Py_DECREF(res);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Restore the saved exception. */
265 PyErr_Restore(error_type, error_value, error_traceback);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 /* Undo the temporary resurrection; can't use DECREF here, it would
268 * cause a recursive call.
269 */
270 assert(self->ob_refcnt > 0);
271 if (--self->ob_refcnt == 0)
272 return; /* this is the normal path out */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* close() resurrected it! Make it look like the original Py_DECREF
275 * never happened.
276 */
277 {
278 Py_ssize_t refcnt = self->ob_refcnt;
279 _Py_NewReference(self);
280 self->ob_refcnt = refcnt;
281 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000282 assert(PyType_IS_GC(Py_TYPE(self)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
286 * we need to undo that. */
287 _Py_DEC_REFTOTAL;
288 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
289 * chain, so no more to do there.
290 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
291 * _Py_NewReference bumped tp_allocs: both of those need to be
292 * undone.
293 */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000294#ifdef COUNT_ALLOCS
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000295 --(Py_TYPE(self)->tp_frees);
296 --(Py_TYPE(self)->tp_allocs);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000297#endif
298}
299
300
301
302PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
304return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000305
306static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307gen_throw(PyGenObject *gen, PyObject *args)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 PyObject *typ;
310 PyObject *tb = NULL;
311 PyObject *val = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000312 PyObject *yf = gen->gi_frame ? gen->gi_frame->f_yieldfrom : NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
315 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000316
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000317 if (yf) {
318 PyObject *ret;
319 int err;
320 Py_INCREF(yf);
321 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
322 err = gen_close_iter(yf);
323 Py_DECREF(yf);
324 gen_undelegate(gen);
325 if (err < 0)
326 return gen_send_ex(gen, Py_None, 1);
327 goto throw_here;
328 }
329 if (PyGen_CheckExact(yf)) {
330 ret = gen_throw((PyGenObject *)yf, args);
331 } else {
332 PyObject *meth = PyObject_GetAttrString(yf, "throw");
333 if (meth == NULL) {
334 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
335 Py_DECREF(yf);
336 return NULL;
337 }
338 PyErr_Clear();
339 Py_DECREF(yf);
340 gen_undelegate(gen);
341 goto throw_here;
342 }
343 ret = PyObject_CallObject(meth, args);
344 Py_DECREF(meth);
345 }
346 Py_DECREF(yf);
347 if (!ret) {
348 PyObject *val;
349 gen_undelegate(gen);
350 if (PyGen_FetchStopIterationValue(&val) == 0) {
351 ret = gen_send_ex(gen, val, 0);
352 Py_DECREF(val);
353 } else {
354 ret = gen_send_ex(gen, Py_None, 1);
355 }
356 }
357 return ret;
358 }
359
360throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* First, check the traceback argument, replacing None with
362 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400363 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 else if (tb != NULL && !PyTraceBack_Check(tb)) {
367 PyErr_SetString(PyExc_TypeError,
368 "throw() third argument must be a traceback object");
369 return NULL;
370 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 Py_INCREF(typ);
373 Py_XINCREF(val);
374 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000375
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400376 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 else if (PyExceptionInstance_Check(typ)) {
380 /* Raising an instance. The value should be a dummy. */
381 if (val && val != Py_None) {
382 PyErr_SetString(PyExc_TypeError,
383 "instance exception may not have a separate value");
384 goto failed_throw;
385 }
386 else {
387 /* Normalize to raise <class>, <instance> */
388 Py_XDECREF(val);
389 val = typ;
390 typ = PyExceptionInstance_Class(typ);
391 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200392
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400393 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200394 /* Returns NULL if there's no traceback */
395 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 }
397 }
398 else {
399 /* Not something you can raise. throw() fails. */
400 PyErr_Format(PyExc_TypeError,
401 "exceptions must be classes or instances "
402 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000403 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 goto failed_throw;
405 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 PyErr_Restore(typ, val, tb);
408 return gen_send_ex(gen, Py_None, 1);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000409
410failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* Didn't use our arguments, so restore their original refcounts */
412 Py_DECREF(typ);
413 Py_XDECREF(val);
414 Py_XDECREF(tb);
415 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000416}
417
418
419static PyObject *
420gen_iternext(PyGenObject *gen)
421{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000422 PyObject *val = NULL;
423 PyObject *ret;
424 int exc = 0;
425 PyObject *yf = gen->gi_frame ? gen->gi_frame->f_yieldfrom : NULL;
426 if (yf) {
427 Py_INCREF(yf);
428 /* ceval.c ensures that yf is an iterator */
429 ret = Py_TYPE(yf)->tp_iternext(yf);
430 if (ret) {
431 Py_DECREF(yf);
432 return ret;
433 }
434 gen_undelegate(gen);
435 if (PyGen_FetchStopIterationValue(&val) < 0)
436 exc = 1;
437 Py_DECREF(yf);
438 }
439 ret = gen_send_ex(gen, val, exc);
440 Py_XDECREF(val);
441 return ret;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000442}
443
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000444/*
445 * In certain recursive situations, a generator may lose its frame
446 * before we get a chance to clear f_yieldfrom, so we use this
447 * helper function.
448 */
449
450static void
451gen_undelegate(PyGenObject *gen) {
452 if (gen->gi_frame) {
453 Py_XDECREF(gen->gi_frame->f_yieldfrom);
454 gen->gi_frame->f_yieldfrom = NULL;
455 }
456}
457
458/*
459 * If StopIteration exception is set, fetches its 'value'
460 * attribute if any, otherwise sets pvalue to None.
461 *
462 * Returns 0 if no exception or StopIteration is set.
463 * If any other exception is set, returns -1 and leaves
464 * pvalue unchanged.
465 */
466
467int
468PyGen_FetchStopIterationValue(PyObject **pvalue) {
469 PyObject *et, *ev, *tb;
470 PyObject *value = NULL;
471
472 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
473 PyErr_Fetch(&et, &ev, &tb);
474 Py_XDECREF(et);
475 Py_XDECREF(tb);
476 if (ev) {
477 value = ((PyStopIterationObject *)ev)->value;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100478 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000479 Py_DECREF(ev);
480 }
481 } else if (PyErr_Occurred()) {
482 return -1;
483 }
484 if (value == NULL) {
485 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100486 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000487 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000488 *pvalue = value;
489 return 0;
490}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000491
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000492static PyObject *
493gen_repr(PyGenObject *gen)
494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 return PyUnicode_FromFormat("<generator object %S at %p>",
496 ((PyCodeObject *)gen->gi_code)->co_name,
497 gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000498}
499
500
501static PyObject *
502gen_get_name(PyGenObject *gen)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
505 Py_INCREF(name);
506 return name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000507}
508
509
510PyDoc_STRVAR(gen__name__doc__,
511"Return the name of the generator's associated code object.");
512
513static PyGetSetDef gen_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
515 {NULL}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000516};
517
518
Martin v. Löwise440e472004-06-01 15:22:42 +0000519static PyMemberDef gen_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
521 {"gi_running", T_INT, offsetof(PyGenObject, gi_running), READONLY},
522 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
523 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000524};
525
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000526static PyMethodDef gen_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 {"send",(PyCFunction)gen_send, METH_O, send_doc},
528 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
529 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
530 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000531};
532
Martin v. Löwise440e472004-06-01 15:22:42 +0000533PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyVarObject_HEAD_INIT(&PyType_Type, 0)
535 "generator", /* tp_name */
536 sizeof(PyGenObject), /* tp_basicsize */
537 0, /* tp_itemsize */
538 /* methods */
539 (destructor)gen_dealloc, /* tp_dealloc */
540 0, /* tp_print */
541 0, /* tp_getattr */
542 0, /* tp_setattr */
543 0, /* tp_reserved */
544 (reprfunc)gen_repr, /* tp_repr */
545 0, /* tp_as_number */
546 0, /* tp_as_sequence */
547 0, /* tp_as_mapping */
548 0, /* tp_hash */
549 0, /* tp_call */
550 0, /* tp_str */
551 PyObject_GenericGetAttr, /* tp_getattro */
552 0, /* tp_setattro */
553 0, /* tp_as_buffer */
554 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
555 0, /* tp_doc */
556 (traverseproc)gen_traverse, /* tp_traverse */
557 0, /* tp_clear */
558 0, /* tp_richcompare */
559 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
560 PyObject_SelfIter, /* tp_iter */
561 (iternextfunc)gen_iternext, /* tp_iternext */
562 gen_methods, /* tp_methods */
563 gen_memberlist, /* tp_members */
564 gen_getsetlist, /* tp_getset */
565 0, /* tp_base */
566 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 0, /* tp_descr_get */
569 0, /* tp_descr_set */
570 0, /* tp_dictoffset */
571 0, /* tp_init */
572 0, /* tp_alloc */
573 0, /* tp_new */
574 0, /* tp_free */
575 0, /* tp_is_gc */
576 0, /* tp_bases */
577 0, /* tp_mro */
578 0, /* tp_cache */
579 0, /* tp_subclasses */
580 0, /* tp_weaklist */
581 gen_del, /* tp_del */
Martin v. Löwise440e472004-06-01 15:22:42 +0000582};
583
584PyObject *
585PyGen_New(PyFrameObject *f)
586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
588 if (gen == NULL) {
589 Py_DECREF(f);
590 return NULL;
591 }
592 gen->gi_frame = f;
593 Py_INCREF(f->f_code);
594 gen->gi_code = (PyObject *)(f->f_code);
595 gen->gi_running = 0;
596 gen->gi_weakreflist = NULL;
597 _PyObject_GC_TRACK(gen);
598 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000599}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600
601int
602PyGen_NeedsFinalizing(PyGenObject *gen)
603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 int i;
605 PyFrameObject *f = gen->gi_frame;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606
Benjamin Petersonf07c9a12011-07-03 17:23:22 -0500607 if (f == NULL || f->f_stacktop == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 return 0; /* no frame or empty blockstack == no finalization */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 /* Any block type besides a loop requires cleanup. */
Benjamin Petersonf07c9a12011-07-03 17:23:22 -0500611 for (i = 0; i < f->f_iblock; i++)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (f->f_blockstack[i].b_type != SETUP_LOOP)
613 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* No blocks except loops, it's safe to skip finalization. */
616 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617}