blob: 4be739ab8288d5c6f06b41aab6e6fd93cf41c360 [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{
Antoine Pitrou667f5452014-07-08 18:43:23 -0400380 return gen_send_ex(gen, NULL, 0);
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);
399 Py_XDECREF(et);
400 Py_XDECREF(tb);
401 if (ev) {
402 value = ((PyStopIterationObject *)ev)->value;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100403 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000404 Py_DECREF(ev);
405 }
406 } else if (PyErr_Occurred()) {
407 return -1;
408 }
409 if (value == NULL) {
410 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100411 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000412 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000413 *pvalue = value;
414 return 0;
415}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000416
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000417static PyObject *
418gen_repr(PyGenObject *gen)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return PyUnicode_FromFormat("<generator object %S at %p>",
Victor Stinner40ee3012014-06-16 15:59:28 +0200421 gen->gi_qualname, gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000422}
423
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000424static PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200425gen_get_name(PyGenObject *op)
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000426{
Victor Stinner40ee3012014-06-16 15:59:28 +0200427 Py_INCREF(op->gi_name);
428 return op->gi_name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000429}
430
Victor Stinner40ee3012014-06-16 15:59:28 +0200431static int
432gen_set_name(PyGenObject *op, PyObject *value)
433{
434 PyObject *tmp;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000435
Victor Stinner40ee3012014-06-16 15:59:28 +0200436 /* Not legal to del gen.gi_name or to set it to anything
437 * other than a string object. */
438 if (value == NULL || !PyUnicode_Check(value)) {
439 PyErr_SetString(PyExc_TypeError,
440 "__name__ must be set to a string object");
441 return -1;
442 }
443 tmp = op->gi_name;
444 Py_INCREF(value);
445 op->gi_name = value;
446 Py_DECREF(tmp);
447 return 0;
448}
449
450static PyObject *
451gen_get_qualname(PyGenObject *op)
452{
453 Py_INCREF(op->gi_qualname);
454 return op->gi_qualname;
455}
456
457static int
458gen_set_qualname(PyGenObject *op, PyObject *value)
459{
460 PyObject *tmp;
461
462 /* Not legal to del gen.__qualname__ or to set it to anything
463 * other than a string object. */
464 if (value == NULL || !PyUnicode_Check(value)) {
465 PyErr_SetString(PyExc_TypeError,
466 "__qualname__ must be set to a string object");
467 return -1;
468 }
469 tmp = op->gi_qualname;
470 Py_INCREF(value);
471 op->gi_qualname = value;
472 Py_DECREF(tmp);
473 return 0;
474}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000475
476static PyGetSetDef gen_getsetlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200477 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
478 PyDoc_STR("name of the generator")},
479 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
480 PyDoc_STR("qualified name of the generator")},
481 {NULL} /* Sentinel */
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000482};
483
Martin v. Löwise440e472004-06-01 15:22:42 +0000484static PyMemberDef gen_memberlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200485 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
486 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
487 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000489};
490
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000491static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500492 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
494 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
495 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000496};
497
Martin v. Löwise440e472004-06-01 15:22:42 +0000498PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyVarObject_HEAD_INIT(&PyType_Type, 0)
500 "generator", /* tp_name */
501 sizeof(PyGenObject), /* tp_basicsize */
502 0, /* tp_itemsize */
503 /* methods */
504 (destructor)gen_dealloc, /* tp_dealloc */
505 0, /* tp_print */
506 0, /* tp_getattr */
507 0, /* tp_setattr */
508 0, /* tp_reserved */
509 (reprfunc)gen_repr, /* tp_repr */
510 0, /* tp_as_number */
511 0, /* tp_as_sequence */
512 0, /* tp_as_mapping */
513 0, /* tp_hash */
514 0, /* tp_call */
515 0, /* tp_str */
516 PyObject_GenericGetAttr, /* tp_getattro */
517 0, /* tp_setattro */
518 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200519 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
520 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 0, /* tp_doc */
522 (traverseproc)gen_traverse, /* tp_traverse */
523 0, /* tp_clear */
524 0, /* tp_richcompare */
525 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
526 PyObject_SelfIter, /* tp_iter */
527 (iternextfunc)gen_iternext, /* tp_iternext */
528 gen_methods, /* tp_methods */
529 gen_memberlist, /* tp_members */
530 gen_getsetlist, /* tp_getset */
531 0, /* tp_base */
532 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 0, /* tp_descr_get */
535 0, /* tp_descr_set */
536 0, /* tp_dictoffset */
537 0, /* tp_init */
538 0, /* tp_alloc */
539 0, /* tp_new */
540 0, /* tp_free */
541 0, /* tp_is_gc */
542 0, /* tp_bases */
543 0, /* tp_mro */
544 0, /* tp_cache */
545 0, /* tp_subclasses */
546 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200547 0, /* tp_del */
548 0, /* tp_version_tag */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200549 _PyGen_Finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000550};
551
552PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200553PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
Martin v. Löwise440e472004-06-01 15:22:42 +0000554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
556 if (gen == NULL) {
557 Py_DECREF(f);
558 return NULL;
559 }
560 gen->gi_frame = f;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200561 f->f_gen = (PyObject *) gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 Py_INCREF(f->f_code);
563 gen->gi_code = (PyObject *)(f->f_code);
564 gen->gi_running = 0;
565 gen->gi_weakreflist = NULL;
Victor Stinner40ee3012014-06-16 15:59:28 +0200566 if (name != NULL)
567 gen->gi_name = name;
568 else
569 gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
570 Py_INCREF(gen->gi_name);
571 if (qualname != NULL)
572 gen->gi_qualname = qualname;
573 else
574 gen->gi_qualname = gen->gi_name;
575 Py_INCREF(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 _PyObject_GC_TRACK(gen);
577 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000578}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579
Victor Stinner40ee3012014-06-16 15:59:28 +0200580PyObject *
581PyGen_New(PyFrameObject *f)
582{
583 return PyGen_NewWithQualName(f, NULL, NULL);
584}
585
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586int
587PyGen_NeedsFinalizing(PyGenObject *gen)
588{
Antoine Pitrou93963562013-05-14 20:37:52 +0200589 int i;
590 PyFrameObject *f = gen->gi_frame;
591
592 if (f == NULL || f->f_stacktop == NULL)
593 return 0; /* no frame or empty blockstack == no finalization */
594
595 /* Any block type besides a loop requires cleanup. */
596 for (i = 0; i < f->f_iblock; i++)
597 if (f->f_blockstack[i].b_type != SETUP_LOOP)
598 return 1;
599
600 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602}