blob: 831b15df11b3d85235ed14caea89cefdf27f475e [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);
Victor Stinner40ee3012014-06-16 15:59:28 +020015 Py_VISIT(gen->gi_name);
16 Py_VISIT(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 return 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000018}
19
Antoine Pitrou58720d62013-08-05 23:26:40 +020020void
21_PyGen_Finalize(PyObject *self)
Antoine Pitrou796564c2013-07-30 19:59:21 +020022{
23 PyGenObject *gen = (PyGenObject *)self;
24 PyObject *res;
25 PyObject *error_type, *error_value, *error_traceback;
26
27 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
28 /* Generator isn't paused, so no need to close */
29 return;
30
31 /* Save the current exception, if any. */
32 PyErr_Fetch(&error_type, &error_value, &error_traceback);
33
34 res = gen_close(gen, NULL);
35
36 if (res == NULL)
37 PyErr_WriteUnraisable(self);
38 else
39 Py_DECREF(res);
40
41 /* Restore the saved exception. */
42 PyErr_Restore(error_type, error_value, error_traceback);
43}
44
45static void
Martin v. Löwise440e472004-06-01 15:22:42 +000046gen_dealloc(PyGenObject *gen)
47{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 PyObject *self = (PyObject *) gen;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 _PyObject_GC_UNTRACK(gen);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (gen->gi_weakreflist != NULL)
53 PyObject_ClearWeakRefs(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000054
Antoine Pitrou93963562013-05-14 20:37:52 +020055 _PyObject_GC_TRACK(self);
56
Antoine Pitrou796564c2013-07-30 19:59:21 +020057 if (PyObject_CallFinalizerFromDealloc(self))
58 return; /* resurrected. :( */
Antoine Pitrou93963562013-05-14 20:37:52 +020059
60 _PyObject_GC_UNTRACK(self);
61 Py_CLEAR(gen->gi_frame);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 Py_CLEAR(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +020063 Py_CLEAR(gen->gi_name);
64 Py_CLEAR(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyObject_GC_Del(gen);
Martin v. Löwise440e472004-06-01 15:22:42 +000066}
67
68static PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000069gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
Martin v. Löwise440e472004-06-01 15:22:42 +000070{
Antoine Pitrou93963562013-05-14 20:37:52 +020071 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +020073 PyObject *result;
Martin v. Löwise440e472004-06-01 15:22:42 +000074
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -050075 if (gen->gi_running) {
76 PyErr_SetString(PyExc_ValueError,
77 "generator already executing");
78 return NULL;
79 }
Antoine Pitrou93963562013-05-14 20:37:52 +020080 if (f == NULL || f->f_stacktop == NULL) {
81 /* Only set exception if called from send() */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (arg && !exc)
83 PyErr_SetNone(PyExc_StopIteration);
84 return NULL;
85 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000086
Antoine Pitrou93963562013-05-14 20:37:52 +020087 if (f->f_lasti == -1) {
88 if (arg && arg != Py_None) {
89 PyErr_SetString(PyExc_TypeError,
90 "can't send non-None value to a "
91 "just-started generator");
92 return NULL;
93 }
94 } else {
95 /* Push arg onto the frame's value stack */
96 result = arg ? arg : Py_None;
97 Py_INCREF(result);
98 *(f->f_stacktop++) = result;
99 }
100
101 /* Generators always return to their most recent caller, not
102 * necessarily their creator. */
103 Py_XINCREF(tstate->frame);
104 assert(f->f_back == NULL);
105 f->f_back = tstate->frame;
106
107 gen->gi_running = 1;
108 result = PyEval_EvalFrameEx(f, exc);
109 gen->gi_running = 0;
110
111 /* Don't keep the reference to f_back any longer than necessary. It
112 * may keep a chain of frames alive or it could create a reference
113 * cycle. */
114 assert(f->f_back == tstate->frame);
115 Py_CLEAR(f->f_back);
116
117 /* If the generator just returned (as opposed to yielding), signal
118 * that the generator is exhausted. */
119 if (result && f->f_stacktop == NULL) {
120 if (result == Py_None) {
121 /* Delay exception instantiation if we can */
122 PyErr_SetNone(PyExc_StopIteration);
123 } else {
124 PyObject *e = PyObject_CallFunctionObjArgs(
125 PyExc_StopIteration, result, NULL);
126 if (e != NULL) {
127 PyErr_SetObject(PyExc_StopIteration, e);
128 Py_DECREF(e);
129 }
130 }
131 Py_CLEAR(result);
132 }
133
134 if (!result || f->f_stacktop == NULL) {
135 /* generator can't be rerun, so release the frame */
136 /* first clean reference cycle through stored exception traceback */
137 PyObject *t, *v, *tb;
138 t = f->f_exc_type;
139 v = f->f_exc_value;
140 tb = f->f_exc_traceback;
141 f->f_exc_type = NULL;
142 f->f_exc_value = NULL;
143 f->f_exc_traceback = NULL;
144 Py_XDECREF(t);
145 Py_XDECREF(v);
146 Py_XDECREF(tb);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200147 gen->gi_frame->f_gen = NULL;
Antoine Pitrou93963562013-05-14 20:37:52 +0200148 gen->gi_frame = NULL;
149 Py_DECREF(f);
150 }
151
152 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000153}
154
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000155PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156"send(arg) -> send 'arg' into generator,\n\
157return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000158
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500159PyObject *
160_PyGen_Send(PyGenObject *gen, PyObject *arg)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000161{
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500162 return gen_send_ex(gen, arg, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000163}
164
165PyDoc_STRVAR(close_doc,
Benjamin Petersonab3da292012-05-03 18:44:09 -0400166"close() -> raise GeneratorExit inside generator.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000167
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000168/*
169 * This helper function is used by gen_close and gen_throw to
170 * close a subiterator being delegated to by yield-from.
171 */
172
Antoine Pitrou93963562013-05-14 20:37:52 +0200173static int
174gen_close_iter(PyObject *yf)
175{
176 PyObject *retval = NULL;
177 _Py_IDENTIFIER(close);
178
179 if (PyGen_CheckExact(yf)) {
180 retval = gen_close((PyGenObject *)yf, NULL);
181 if (retval == NULL)
182 return -1;
183 } else {
184 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
185 if (meth == NULL) {
186 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
187 PyErr_WriteUnraisable(yf);
188 PyErr_Clear();
189 } else {
190 retval = PyObject_CallFunction(meth, "");
191 Py_DECREF(meth);
192 if (retval == NULL)
193 return -1;
194 }
195 }
196 Py_XDECREF(retval);
197 return 0;
198}
199
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500200static PyObject *
201gen_yf(PyGenObject *gen)
202{
Antoine Pitrou93963562013-05-14 20:37:52 +0200203 PyObject *yf = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500204 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200205
206 if (f && f->f_stacktop) {
207 PyObject *bytecode = f->f_code->co_code;
208 unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
209
210 if (code[f->f_lasti + 1] != YIELD_FROM)
211 return NULL;
212 yf = f->f_stacktop[-1];
213 Py_INCREF(yf);
214 }
215
216 return yf;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500217}
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000218
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000219static PyObject *
220gen_close(PyGenObject *gen, PyObject *args)
221{
Antoine Pitrou93963562013-05-14 20:37:52 +0200222 PyObject *retval;
223 PyObject *yf = gen_yf(gen);
224 int err = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000225
Antoine Pitrou93963562013-05-14 20:37:52 +0200226 if (yf) {
227 gen->gi_running = 1;
228 err = gen_close_iter(yf);
229 gen->gi_running = 0;
230 Py_DECREF(yf);
231 }
232 if (err == 0)
233 PyErr_SetNone(PyExc_GeneratorExit);
234 retval = gen_send_ex(gen, Py_None, 1);
235 if (retval) {
236 Py_DECREF(retval);
237 PyErr_SetString(PyExc_RuntimeError,
238 "generator ignored GeneratorExit");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return NULL;
240 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200241 if (PyErr_ExceptionMatches(PyExc_StopIteration)
242 || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
243 PyErr_Clear(); /* ignore these errors */
244 Py_INCREF(Py_None);
245 return Py_None;
246 }
247 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000248}
249
Antoine Pitrou93963562013-05-14 20:37:52 +0200250
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000251PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
253return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000254
255static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256gen_throw(PyGenObject *gen, PyObject *args)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyObject *typ;
259 PyObject *tb = NULL;
260 PyObject *val = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500261 PyObject *yf = gen_yf(gen);
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000262 _Py_IDENTIFIER(throw);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
265 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000266
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000267 if (yf) {
268 PyObject *ret;
269 int err;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000270 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500271 gen->gi_running = 1;
Antoine Pitrou93963562013-05-14 20:37:52 +0200272 err = gen_close_iter(yf);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500273 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000274 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000275 if (err < 0)
276 return gen_send_ex(gen, Py_None, 1);
277 goto throw_here;
278 }
279 if (PyGen_CheckExact(yf)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500280 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000281 ret = gen_throw((PyGenObject *)yf, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500282 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000283 } else {
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000284 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000285 if (meth == NULL) {
286 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
287 Py_DECREF(yf);
288 return NULL;
289 }
290 PyErr_Clear();
291 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000292 goto throw_here;
293 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500294 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000295 ret = PyObject_CallObject(meth, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500296 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000297 Py_DECREF(meth);
298 }
299 Py_DECREF(yf);
300 if (!ret) {
301 PyObject *val;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500302 /* Pop subiterator from stack */
303 ret = *(--gen->gi_frame->f_stacktop);
304 assert(ret == yf);
305 Py_DECREF(ret);
306 /* Termination repetition of YIELD_FROM */
307 gen->gi_frame->f_lasti++;
Nick Coghlanc40bc092012-06-17 15:15:49 +1000308 if (_PyGen_FetchStopIterationValue(&val) == 0) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000309 ret = gen_send_ex(gen, val, 0);
310 Py_DECREF(val);
311 } else {
312 ret = gen_send_ex(gen, Py_None, 1);
313 }
314 }
315 return ret;
316 }
317
318throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 /* First, check the traceback argument, replacing None with
320 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400321 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 else if (tb != NULL && !PyTraceBack_Check(tb)) {
325 PyErr_SetString(PyExc_TypeError,
326 "throw() third argument must be a traceback object");
327 return NULL;
328 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_INCREF(typ);
331 Py_XINCREF(val);
332 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000333
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400334 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 else if (PyExceptionInstance_Check(typ)) {
338 /* Raising an instance. The value should be a dummy. */
339 if (val && val != Py_None) {
340 PyErr_SetString(PyExc_TypeError,
341 "instance exception may not have a separate value");
342 goto failed_throw;
343 }
344 else {
345 /* Normalize to raise <class>, <instance> */
346 Py_XDECREF(val);
347 val = typ;
348 typ = PyExceptionInstance_Class(typ);
349 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200350
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400351 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200352 /* Returns NULL if there's no traceback */
353 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
355 }
356 else {
357 /* Not something you can raise. throw() fails. */
358 PyErr_Format(PyExc_TypeError,
359 "exceptions must be classes or instances "
360 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000361 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 goto failed_throw;
363 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyErr_Restore(typ, val, tb);
366 return gen_send_ex(gen, Py_None, 1);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000367
368failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* Didn't use our arguments, so restore their original refcounts */
370 Py_DECREF(typ);
371 Py_XDECREF(val);
372 Py_XDECREF(tb);
373 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000374}
375
376
377static PyObject *
378gen_iternext(PyGenObject *gen)
379{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000380 PyObject *val = NULL;
381 PyObject *ret;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500382 ret = gen_send_ex(gen, val, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000383 Py_XDECREF(val);
384 return ret;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000385}
386
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000387/*
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000388 * If StopIteration exception is set, fetches its 'value'
389 * attribute if any, otherwise sets pvalue to None.
390 *
391 * Returns 0 if no exception or StopIteration is set.
392 * If any other exception is set, returns -1 and leaves
393 * pvalue unchanged.
394 */
395
396int
Nick Coghlanc40bc092012-06-17 15:15:49 +1000397_PyGen_FetchStopIterationValue(PyObject **pvalue) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000398 PyObject *et, *ev, *tb;
399 PyObject *value = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500400
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000401 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
402 PyErr_Fetch(&et, &ev, &tb);
403 Py_XDECREF(et);
404 Py_XDECREF(tb);
405 if (ev) {
406 value = ((PyStopIterationObject *)ev)->value;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100407 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000408 Py_DECREF(ev);
409 }
410 } else if (PyErr_Occurred()) {
411 return -1;
412 }
413 if (value == NULL) {
414 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100415 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000416 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000417 *pvalue = value;
418 return 0;
419}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000420
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000421static PyObject *
422gen_repr(PyGenObject *gen)
423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return PyUnicode_FromFormat("<generator object %S at %p>",
Victor Stinner40ee3012014-06-16 15:59:28 +0200425 gen->gi_qualname, gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000426}
427
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000428static PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200429gen_get_name(PyGenObject *op)
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000430{
Victor Stinner40ee3012014-06-16 15:59:28 +0200431 Py_INCREF(op->gi_name);
432 return op->gi_name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000433}
434
Victor Stinner40ee3012014-06-16 15:59:28 +0200435static int
436gen_set_name(PyGenObject *op, PyObject *value)
437{
438 PyObject *tmp;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000439
Victor Stinner40ee3012014-06-16 15:59:28 +0200440 /* Not legal to del gen.gi_name or to set it to anything
441 * other than a string object. */
442 if (value == NULL || !PyUnicode_Check(value)) {
443 PyErr_SetString(PyExc_TypeError,
444 "__name__ must be set to a string object");
445 return -1;
446 }
447 tmp = op->gi_name;
448 Py_INCREF(value);
449 op->gi_name = value;
450 Py_DECREF(tmp);
451 return 0;
452}
453
454static PyObject *
455gen_get_qualname(PyGenObject *op)
456{
457 Py_INCREF(op->gi_qualname);
458 return op->gi_qualname;
459}
460
461static int
462gen_set_qualname(PyGenObject *op, PyObject *value)
463{
464 PyObject *tmp;
465
466 /* Not legal to del gen.__qualname__ or to set it to anything
467 * other than a string object. */
468 if (value == NULL || !PyUnicode_Check(value)) {
469 PyErr_SetString(PyExc_TypeError,
470 "__qualname__ must be set to a string object");
471 return -1;
472 }
473 tmp = op->gi_qualname;
474 Py_INCREF(value);
475 op->gi_qualname = value;
476 Py_DECREF(tmp);
477 return 0;
478}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000479
480static PyGetSetDef gen_getsetlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200481 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
482 PyDoc_STR("name of the generator")},
483 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
484 PyDoc_STR("qualified name of the generator")},
485 {NULL} /* Sentinel */
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000486};
487
Martin v. Löwise440e472004-06-01 15:22:42 +0000488static PyMemberDef gen_memberlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200489 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
490 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
491 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000493};
494
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000495static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500496 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
498 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
499 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000500};
501
Martin v. Löwise440e472004-06-01 15:22:42 +0000502PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyVarObject_HEAD_INIT(&PyType_Type, 0)
504 "generator", /* tp_name */
505 sizeof(PyGenObject), /* tp_basicsize */
506 0, /* tp_itemsize */
507 /* methods */
508 (destructor)gen_dealloc, /* tp_dealloc */
509 0, /* tp_print */
510 0, /* tp_getattr */
511 0, /* tp_setattr */
512 0, /* tp_reserved */
513 (reprfunc)gen_repr, /* tp_repr */
514 0, /* tp_as_number */
515 0, /* tp_as_sequence */
516 0, /* tp_as_mapping */
517 0, /* tp_hash */
518 0, /* tp_call */
519 0, /* tp_str */
520 PyObject_GenericGetAttr, /* tp_getattro */
521 0, /* tp_setattro */
522 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200523 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
524 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 0, /* tp_doc */
526 (traverseproc)gen_traverse, /* tp_traverse */
527 0, /* tp_clear */
528 0, /* tp_richcompare */
529 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
530 PyObject_SelfIter, /* tp_iter */
531 (iternextfunc)gen_iternext, /* tp_iternext */
532 gen_methods, /* tp_methods */
533 gen_memberlist, /* tp_members */
534 gen_getsetlist, /* tp_getset */
535 0, /* tp_base */
536 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 0, /* tp_descr_get */
539 0, /* tp_descr_set */
540 0, /* tp_dictoffset */
541 0, /* tp_init */
542 0, /* tp_alloc */
543 0, /* tp_new */
544 0, /* tp_free */
545 0, /* tp_is_gc */
546 0, /* tp_bases */
547 0, /* tp_mro */
548 0, /* tp_cache */
549 0, /* tp_subclasses */
550 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200551 0, /* tp_del */
552 0, /* tp_version_tag */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200553 _PyGen_Finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000554};
555
556PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200557PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
Martin v. Löwise440e472004-06-01 15:22:42 +0000558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
560 if (gen == NULL) {
561 Py_DECREF(f);
562 return NULL;
563 }
564 gen->gi_frame = f;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200565 f->f_gen = (PyObject *) gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 Py_INCREF(f->f_code);
567 gen->gi_code = (PyObject *)(f->f_code);
568 gen->gi_running = 0;
569 gen->gi_weakreflist = NULL;
Victor Stinner40ee3012014-06-16 15:59:28 +0200570 if (name != NULL)
571 gen->gi_name = name;
572 else
573 gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
574 Py_INCREF(gen->gi_name);
575 if (qualname != NULL)
576 gen->gi_qualname = qualname;
577 else
578 gen->gi_qualname = gen->gi_name;
579 Py_INCREF(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 _PyObject_GC_TRACK(gen);
581 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000582}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000583
Victor Stinner40ee3012014-06-16 15:59:28 +0200584PyObject *
585PyGen_New(PyFrameObject *f)
586{
587 return PyGen_NewWithQualName(f, NULL, NULL);
588}
589
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590int
591PyGen_NeedsFinalizing(PyGenObject *gen)
592{
Antoine Pitrou93963562013-05-14 20:37:52 +0200593 int i;
594 PyFrameObject *f = gen->gi_frame;
595
596 if (f == NULL || f->f_stacktop == NULL)
597 return 0; /* no frame or empty blockstack == no finalization */
598
599 /* Any block type besides a loop requires cleanup. */
600 for (i = 0; i < f->f_iblock; i++)
601 if (f->f_blockstack[i].b_type != SETUP_LOOP)
602 return 1;
603
604 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606}