annotate Objects/genobject.c @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents 3fe3ccdf0155
children ead7c3c6be1b 6620a510e9a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
1 /* Generator object implementation */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
2
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
3 #include "Python.h"
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
4 #include "frameobject.h"
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
5 #include "genobject.h"
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
6 #include "ceval.h"
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
7 #include "structmember.h"
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
8 #include "opcode.h"
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
9
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
10 static int
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
11 gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
12 {
37649
21fcc86e4820 Add a cast to make code compile with a C++ compiler.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37642
diff changeset
13 Py_VISIT((PyObject *)gen->gi_frame);
37642
22f2c92a1da7 Don't set gi_frame to Py_None, use NULL instead, eliminating some insane
Phillip J. Eby <pje@telecommunity.com>
parents: 37626
diff changeset
14 return 0;
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
15 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
16
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
17 static void
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
18 gen_dealloc(PyGenObject *gen)
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
19 {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
20 PyObject *self = (PyObject *) gen;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
21
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
22 _PyObject_GC_UNTRACK(gen);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
23
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
24 if (gen->gi_weakreflist != NULL)
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
25 PyObject_ClearWeakRefs(self);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
26
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
27 _PyObject_GC_TRACK(self);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
28
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
29 if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
30 /* Generator is paused, so we need to close */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
31 gen->ob_type->tp_del(self);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
32 if (self->ob_refcnt > 0)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
33 return; /* resurrected. :( */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
34 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
35
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
36 _PyObject_GC_UNTRACK(self);
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
37 Py_CLEAR(gen->gi_frame);
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
38 PyObject_GC_Del(gen);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
39 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
40
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
41
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
42 static PyObject *
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
43 gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
44 {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
45 PyThreadState *tstate = PyThreadState_GET();
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
46 PyFrameObject *f = gen->gi_frame;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
47 PyObject *result;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
48
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
49 if (gen->gi_running) {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
50 PyErr_SetString(PyExc_ValueError,
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
51 "generator already executing");
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
52 return NULL;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
53 }
37642
22f2c92a1da7 Don't set gi_frame to Py_None, use NULL instead, eliminating some insane
Phillip J. Eby <pje@telecommunity.com>
parents: 37626
diff changeset
54 if (f==NULL || f->f_stacktop == NULL) {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
55 /* Only set exception if called from send() */
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
56 if (arg && !exc)
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
57 PyErr_SetNone(PyExc_StopIteration);
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
58 return NULL;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
59 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
60
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
61 if (f->f_lasti == -1) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
62 if (arg && arg != Py_None) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
63 PyErr_SetString(PyExc_TypeError,
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
64 "can't send non-None value to a "
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
65 "just-started generator");
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
66 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
67 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
68 } else {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
69 /* Push arg onto the frame's value stack */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
70 result = arg ? arg : Py_None;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
71 Py_INCREF(result);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
72 *(f->f_stacktop++) = result;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
73 }
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
74
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
75 /* Generators always return to their most recent caller, not
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
76 * necessarily their creator. */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
77 Py_XINCREF(tstate->frame);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
78 assert(f->f_back == NULL);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
79 f->f_back = tstate->frame;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
80
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
81 gen->gi_running = 1;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
82 result = PyEval_EvalFrameEx(f, exc);
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
83 gen->gi_running = 0;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
84
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
85 /* Don't keep the reference to f_back any longer than necessary. It
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
86 * may keep a chain of frames alive or it could create a reference
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
87 * cycle. */
35338
d6575cd1ab67 Fix a too-aggressive assert (see SF#1257960). Previously, gen_iternext
Phillip J. Eby <pje@telecommunity.com>
parents: 35298
diff changeset
88 assert(f->f_back == tstate->frame);
33386
c2c49ee727d0 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents: 32421
diff changeset
89 Py_CLEAR(f->f_back);
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
90
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
91 /* If the generator just returned (as opposed to yielding), signal
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
92 * that the generator is exhausted. */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
93 if (result == Py_None && f->f_stacktop == NULL) {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
94 Py_DECREF(result);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
95 result = NULL;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
96 /* Set exception if not called by gen_iternext() */
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
97 if (arg)
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
98 PyErr_SetNone(PyExc_StopIteration);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
99 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
100
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
101 if (!result || f->f_stacktop == NULL) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
102 /* generator can't be rerun, so release the frame */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
103 Py_DECREF(f);
37642
22f2c92a1da7 Don't set gi_frame to Py_None, use NULL instead, eliminating some insane
Phillip J. Eby <pje@telecommunity.com>
parents: 37626
diff changeset
104 gen->gi_frame = NULL;
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
105 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
106
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
107 return result;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
108 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
109
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
110 PyDoc_STRVAR(send_doc,
37626
c26f5e206de7 wrap docstrings so they are less than 80 columns. add spaces after commas.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37625
diff changeset
111 "send(arg) -> send 'arg' into generator,\n\
c26f5e206de7 wrap docstrings so they are less than 80 columns. add spaces after commas.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37625
diff changeset
112 return next yielded value or raise StopIteration.");
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
113
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
114 static PyObject *
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
115 gen_send(PyGenObject *gen, PyObject *arg)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
116 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
117 return gen_send_ex(gen, arg, 0);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
118 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
119
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
120 PyDoc_STRVAR(close_doc,
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
121 "close(arg) -> raise GeneratorExit inside generator.");
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
122
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
123 static PyObject *
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
124 gen_close(PyGenObject *gen, PyObject *args)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
125 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
126 PyObject *retval;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
127 PyErr_SetNone(PyExc_GeneratorExit);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
128 retval = gen_send_ex(gen, Py_None, 1);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
129 if (retval) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
130 Py_DECREF(retval);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
131 PyErr_SetString(PyExc_RuntimeError,
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
132 "generator ignored GeneratorExit");
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
133 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
134 }
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
135 if (PyErr_ExceptionMatches(PyExc_StopIteration)
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
136 || PyErr_ExceptionMatches(PyExc_GeneratorExit))
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
137 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
138 PyErr_Clear(); /* ignore these errors */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
139 Py_INCREF(Py_None);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
140 return Py_None;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
141 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
142 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
143 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
144
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
145 static void
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
146 gen_del(PyObject *self)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
147 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
148 PyObject *res;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
149 PyObject *error_type, *error_value, *error_traceback;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
150 PyGenObject *gen = (PyGenObject *)self;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
151
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
152 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
153 /* Generator isn't paused, so no need to close */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
154 return;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
155
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
156 /* Temporarily resurrect the object. */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
157 assert(self->ob_refcnt == 0);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
158 self->ob_refcnt = 1;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
159
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
160 /* Save the current exception, if any. */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
161 PyErr_Fetch(&error_type, &error_value, &error_traceback);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
162
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
163 res = gen_close(gen, NULL);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
164
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
165 if (res == NULL)
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
166 PyErr_WriteUnraisable(self);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
167 else
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
168 Py_DECREF(res);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
169
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
170 /* Restore the saved exception. */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
171 PyErr_Restore(error_type, error_value, error_traceback);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
172
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
173 /* Undo the temporary resurrection; can't use DECREF here, it would
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
174 * cause a recursive call.
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
175 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
176 assert(self->ob_refcnt > 0);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
177 if (--self->ob_refcnt == 0)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
178 return; /* this is the normal path out */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
179
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
180 /* close() resurrected it! Make it look like the original Py_DECREF
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
181 * never happened.
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
182 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
183 {
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36751
diff changeset
184 Py_ssize_t refcnt = self->ob_refcnt;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
185 _Py_NewReference(self);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
186 self->ob_refcnt = refcnt;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
187 }
37760
9eb4dc1cc83e gen_del(): Looks like much this was copy/pasted from
Tim Peters <tim.peters@gmail.com>
parents: 37755
diff changeset
188 assert(PyType_IS_GC(self->ob_type) &&
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
189 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
190
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
191 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
192 * we need to undo that. */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
193 _Py_DEC_REFTOTAL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
194 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
195 * chain, so no more to do there.
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
196 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
197 * _Py_NewReference bumped tp_allocs: both of those need to be
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
198 * undone.
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
199 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
200 #ifdef COUNT_ALLOCS
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
201 --self->ob_type->tp_frees;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
202 --self->ob_type->tp_allocs;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
203 #endif
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
204 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
205
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
206
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
207
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
208 PyDoc_STRVAR(throw_doc,
37626
c26f5e206de7 wrap docstrings so they are less than 80 columns. add spaces after commas.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37625
diff changeset
209 "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
c26f5e206de7 wrap docstrings so they are less than 80 columns. add spaces after commas.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37625
diff changeset
210 return next yielded value or raise StopIteration.");
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
211
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
212 static PyObject *
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
213 gen_throw(PyGenObject *gen, PyObject *args)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
214 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
215 PyObject *typ;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
216 PyObject *tb = NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
217 PyObject *val = NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
218
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 37760
diff changeset
219 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
220 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
221
36492
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
222 /* First, check the traceback argument, replacing None with
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
223 NULL. */
37625
c8a09fd4f10d gen_throw(): The caller doesn't own PyArg_ParseTuple()
Tim Peters <tim.peters@gmail.com>
parents: 37564
diff changeset
224 if (tb == Py_None)
36492
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
225 tb = NULL;
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
226 else if (tb != NULL && !PyTraceBack_Check(tb)) {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
227 PyErr_SetString(PyExc_TypeError,
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
228 "throw() third argument must be a traceback object");
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
229 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
230 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
231
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
232 Py_INCREF(typ);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
233 Py_XINCREF(val);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
234 Py_XINCREF(tb);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
235
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36492
diff changeset
236 if (PyExceptionClass_Check(typ)) {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
237 PyErr_NormalizeException(&typ, &val, &tb);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
238 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
239
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36492
diff changeset
240 else if (PyExceptionInstance_Check(typ)) {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
241 /* Raising an instance. The value should be a dummy. */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
242 if (val && val != Py_None) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
243 PyErr_SetString(PyExc_TypeError,
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
244 "instance exception may not have a separate value");
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
245 goto failed_throw;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
246 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
247 else {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
248 /* Normalize to raise <class>, <instance> */
36492
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
249 Py_XDECREF(val);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
250 val = typ;
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36492
diff changeset
251 typ = PyExceptionInstance_Class(typ);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
252 Py_INCREF(typ);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
253 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
254 }
37202
f5cd3091d99f Support throw() of string exceptions.
Phillip J. Eby <pje@telecommunity.com>
parents: 36866
diff changeset
255
f5cd3091d99f Support throw() of string exceptions.
Phillip J. Eby <pje@telecommunity.com>
parents: 36866
diff changeset
256 /* Allow raising builtin string exceptions */
f5cd3091d99f Support throw() of string exceptions.
Phillip J. Eby <pje@telecommunity.com>
parents: 36866
diff changeset
257
f5cd3091d99f Support throw() of string exceptions.
Phillip J. Eby <pje@telecommunity.com>
parents: 36866
diff changeset
258 else if (!PyString_CheckExact(typ)) {
36492
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
259 /* Not something you can raise. throw() fails. */
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
260 PyErr_Format(PyExc_TypeError,
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
261 "exceptions must be classes, or instances, not %s",
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
262 typ->ob_type->tp_name);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
263 goto failed_throw;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
264 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
265
37626
c26f5e206de7 wrap docstrings so they are less than 80 columns. add spaces after commas.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37625
diff changeset
266 PyErr_Restore(typ, val, tb);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
267 return gen_send_ex(gen, Py_None, 1);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
268
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
269 failed_throw:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
270 /* Didn't use our arguments, so restore their original refcounts */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
271 Py_DECREF(typ);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
272 Py_XDECREF(val);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
273 Py_XDECREF(tb);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
274 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
275 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
276
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
277
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
278 static PyObject *
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
279 gen_iternext(PyGenObject *gen)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
280 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
281 return gen_send_ex(gen, NULL, 0);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
282 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
283
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
284
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
285 static PyMemberDef gen_memberlist[] = {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
286 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), RO},
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
287 {"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO},
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
288 {NULL} /* Sentinel */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
289 };
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
290
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
291 static PyMethodDef gen_methods[] = {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
292 {"send",(PyCFunction)gen_send, METH_O, send_doc},
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
293 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
294 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
295 {NULL, NULL} /* Sentinel */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
296 };
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
297
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
298 PyTypeObject PyGen_Type = {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
299 PyObject_HEAD_INIT(&PyType_Type)
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
300 0, /* ob_size */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
301 "generator", /* tp_name */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
302 sizeof(PyGenObject), /* tp_basicsize */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
303 0, /* tp_itemsize */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
304 /* methods */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
305 (destructor)gen_dealloc, /* tp_dealloc */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
306 0, /* tp_print */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
307 0, /* tp_getattr */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
308 0, /* tp_setattr */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
309 0, /* tp_compare */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
310 0, /* tp_repr */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
311 0, /* tp_as_number */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
312 0, /* tp_as_sequence */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
313 0, /* tp_as_mapping */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
314 0, /* tp_hash */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
315 0, /* tp_call */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
316 0, /* tp_str */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
317 PyObject_GenericGetAttr, /* tp_getattro */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
318 0, /* tp_setattro */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
319 0, /* tp_as_buffer */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
320 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
321 0, /* tp_doc */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
322 (traverseproc)gen_traverse, /* tp_traverse */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
323 0, /* tp_clear */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
324 0, /* tp_richcompare */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
325 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
32329
6fead5ea4bba * Factor out PyObject_SelfIter().
Raymond Hettinger <python@rcn.com>
parents: 32175
diff changeset
326 PyObject_SelfIter, /* tp_iter */
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
327 (iternextfunc)gen_iternext, /* tp_iternext */
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
328 gen_methods, /* tp_methods */
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
329 gen_memberlist, /* tp_members */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
330 0, /* tp_getset */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
331 0, /* tp_base */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
332 0, /* tp_dict */
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
333
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
334 0, /* tp_descr_get */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
335 0, /* tp_descr_set */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
336 0, /* tp_dictoffset */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
337 0, /* tp_init */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
338 0, /* tp_alloc */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
339 0, /* tp_new */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
340 0, /* tp_free */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
341 0, /* tp_is_gc */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
342 0, /* tp_bases */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
343 0, /* tp_mro */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
344 0, /* tp_cache */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
345 0, /* tp_subclasses */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
346 0, /* tp_weaklist */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
347 gen_del, /* tp_del */
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
348 };
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
349
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
350 PyObject *
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
351 PyGen_New(PyFrameObject *f)
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
352 {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
353 PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
354 if (gen == NULL) {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
355 Py_DECREF(f);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
356 return NULL;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
357 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
358 gen->gi_frame = f;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
359 gen->gi_running = 0;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
360 gen->gi_weakreflist = NULL;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
361 _PyObject_GC_TRACK(gen);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
362 return (PyObject *)gen;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
363 }
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
364
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
365 int
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
366 PyGen_NeedsFinalizing(PyGenObject *gen)
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
367 {
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
368 int i;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
369 PyFrameObject *f = gen->gi_frame;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
370
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
371 if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
372 return 0; /* no frame or empty blockstack == no finalization */
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
373
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
374 /* Any block type besides a loop requires cleanup. */
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
375 i = f->f_iblock;
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
376 while (--i >= 0) {
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
377 if (f->f_blockstack[i].b_type != SETUP_LOOP)
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
378 return 1;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
379 }
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
380
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
381 /* No blocks except loops, it's safe to skip finalization. */
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
382 return 0;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
383 }