annotate Objects/genobject.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents fb2ea8bb3a33
children fd25030223b0
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);
44976
51bb7b1ae765 #1473257: add generator.gi_code attribute that refers to
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
14 Py_VISIT(gen->gi_code);
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
15 return 0;
32175
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
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
18 static void
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
19 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
20 {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
21 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
22
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
23 _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
24
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
25 if (gen->gi_weakreflist != NULL)
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
26 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
27
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
28 _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
29
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
30 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
31 /* Generator is paused, so we need to close */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43335
diff changeset
32 Py_TYPE(gen)->tp_del(self);
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
33 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
34 return; /* resurrected. :( */
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
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
37 _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
38 Py_CLEAR(gen->gi_frame);
44976
51bb7b1ae765 #1473257: add generator.gi_code attribute that refers to
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
39 Py_CLEAR(gen->gi_code);
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
40 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
41 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
42
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
43
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
44 static PyObject *
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
45 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
46 {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
47 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
48 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
49 PyObject *result;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
50
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
51 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
52 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
53 "generator already executing");
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
54 return NULL;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
55 }
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
56 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
57 /* Only set exception if called from send() */
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
58 if (arg && !exc)
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
59 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
60 return NULL;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
61 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
62
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
63 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
64 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
65 PyErr_SetString(PyExc_TypeError,
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
66 "can't send non-None value to a "
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
67 "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
68 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
69 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
70 } else {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
71 /* 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
72 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
73 Py_INCREF(result);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
74 *(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
75 }
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
76
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
77 /* 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
78 * necessarily their creator. */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
79 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
80 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
81 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
82
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 = 1;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
84 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
85 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
86
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
87 /* 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
88 * 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
89 * cycle. */
35338
d6575cd1ab67 Fix a too-aggressive assert (see SF#1257960). Previously, gen_iternext
Phillip J. Eby <pje@telecommunity.com>
parents: 35298
diff changeset
90 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
91 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
92
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
93 /* 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
94 * 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
95 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
96 Py_DECREF(result);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
97 result = NULL;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
98 /* Set exception if not called by gen_iternext() */
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
99 if (arg)
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
100 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
101 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
102
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
103 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
104 /* 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
105 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
106 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
107 }
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 return result;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
110 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
111
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
112 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
113 "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
114 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
115
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
116 static PyObject *
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
117 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
118 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
119 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
120 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
121
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
122 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
123 "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
124
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
125 static PyObject *
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
126 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
127 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
128 PyObject *retval;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
129 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
130 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
131 if (retval) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
132 Py_DECREF(retval);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
133 PyErr_SetString(PyExc_RuntimeError,
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
134 "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
135 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
136 }
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
137 if (PyErr_ExceptionMatches(PyExc_StopIteration)
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
138 || 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
139 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
140 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
141 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
142 return Py_None;
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 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
145 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
146
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
147 static void
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
148 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
149 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
150 PyObject *res;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
151 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
152 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
153
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
154 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
155 /* 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
156 return;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
157
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
158 /* 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
159 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
160 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
161
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
162 /* 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
163 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
164
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
165 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
166
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
167 if (res == NULL)
37755
b7373bc3b09f - Whitespace normalization
Thomas Wouters <thomas@python.org>
parents: 37724
diff changeset
168 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
169 else
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
170 Py_DECREF(res);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
171
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
172 /* 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
173 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
174
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
175 /* 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
176 * 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
177 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
178 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
179 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
180 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
181
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
182 /* 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
183 * never happened.
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
184 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
185 {
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36751
diff changeset
186 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
187 _Py_NewReference(self);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
188 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
189 }
37760
9eb4dc1cc83e gen_del(): Looks like much this was copy/pasted from
Tim Peters <tim.peters@gmail.com>
parents: 37755
diff changeset
190 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
191 _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
192
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
193 /* 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
194 * 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
195 _Py_DEC_REFTOTAL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
196 /* 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
197 * 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
198 * 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
199 * _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
200 * undone.
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
201 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
202 #ifdef COUNT_ALLOCS
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
203 --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
204 --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
205 #endif
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
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
209
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
210 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
211 "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
212 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
213
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
214 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
215 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
216 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
217 PyObject *typ;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
218 PyObject *tb = NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
219 PyObject *val = NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
220
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 37760
diff changeset
221 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
222 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
223
36492
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
224 /* 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
225 NULL. */
37625
c8a09fd4f10d gen_throw(): The caller doesn't own PyArg_ParseTuple()
Tim Peters <tim.peters@gmail.com>
parents: 37564
diff changeset
226 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
227 tb = NULL;
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
228 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
229 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
230 "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
231 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
232 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
233
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
234 Py_INCREF(typ);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
235 Py_XINCREF(val);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
236 Py_XINCREF(tb);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
237
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36492
diff changeset
238 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
239 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
240 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
241
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36492
diff changeset
242 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
243 /* 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
244 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
245 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
246 "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
247 goto failed_throw;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
248 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
249 else {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
250 /* 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
251 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
252 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
253 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
254 Py_INCREF(typ);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
255 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
256 }
43335
c5b6ae9440b5 Generators had their throw() method allowing string exceptions. That's a
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
257 else {
36492
acd702e306fb * Refcount leak. It was just a reference to Py_None, but still.
Armin Rigo <arigo@tunes.org>
parents: 35338
diff changeset
258 /* 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
259 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
260 "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
261 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
262 goto failed_throw;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
263 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
264
37626
c26f5e206de7 wrap docstrings so they are less than 80 columns. add spaces after commas.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37625
diff changeset
265 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
266 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
267
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
268 failed_throw:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
269 /* 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
270 Py_DECREF(typ);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
271 Py_XDECREF(val);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
272 Py_XDECREF(tb);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
273 return NULL;
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
274 }
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 static PyObject *
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
278 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
279 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
280 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
281 }
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
47056
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
284 static PyObject *
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
285 gen_repr(PyGenObject *gen)
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
286 {
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
287 char *code_name;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
288 code_name = PyString_AsString(((PyCodeObject *)gen->gi_code)->co_name);
47056
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
289 if (code_name == NULL)
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
290 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
291 return PyString_FromFormat("<generator object %.200s at %p>",
47056
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
292 code_name, gen);
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
293 }
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
294
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
295
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
296 static PyObject *
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
297 gen_get_name(PyGenObject *gen)
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
298 {
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
299 PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
300 Py_INCREF(name);
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
301 return name;
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
302 }
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
303
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
304
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
305 PyDoc_STRVAR(gen__name__doc__,
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
306 "Return the name of the generator's associated code object.");
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
307
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
308 static PyGetSetDef gen_getsetlist[] = {
52898
fb2ea8bb3a33 Merged revisions 70642,70648,70656,70661,70765,70773,70789,70824-70825,70828,70830,70832,70836,70838,70842,70851,70855,70857-70858 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47527
diff changeset
309 {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
47056
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
310 {NULL}
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
311 };
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
312
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
313
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
314 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
315 {"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
316 {"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO},
44976
51bb7b1ae765 #1473257: add generator.gi_code attribute that refers to
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
317 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), RO},
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
318 {NULL} /* Sentinel */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
319 };
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
320
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
321 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
322 {"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
323 {"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
324 {"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
325 {NULL, NULL} /* Sentinel */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
326 };
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
327
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
328 PyTypeObject PyGen_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
329 PyVarObject_HEAD_INIT(&PyType_Type, 0)
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
330 "generator", /* tp_name */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
331 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
332 0, /* tp_itemsize */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
333 /* methods */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
334 (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
335 0, /* tp_print */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
336 0, /* tp_getattr */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
337 0, /* tp_setattr */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
338 0, /* tp_compare */
47056
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
339 (reprfunc)gen_repr, /* tp_repr */
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
340 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
341 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
342 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
343 0, /* tp_hash */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
344 0, /* tp_call */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
345 0, /* tp_str */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
346 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
347 0, /* tp_setattro */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
348 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
349 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
350 0, /* tp_doc */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
351 (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
352 0, /* tp_clear */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
353 0, /* tp_richcompare */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
354 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
32329
6fead5ea4bba * Factor out PyObject_SelfIter().
Raymond Hettinger <python@rcn.com>
parents: 32175
diff changeset
355 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
356 (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
357 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
358 gen_memberlist, /* tp_members */
47056
1573c776df82 #2863: add gen.__name__ and add this name to generator repr().
Georg Brandl <georg@python.org>
parents: 44976
diff changeset
359 gen_getsetlist, /* tp_getset */
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
360 0, /* tp_base */
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
361 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
362
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
363 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
364 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
365 0, /* tp_dictoffset */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
366 0, /* tp_init */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
367 0, /* tp_alloc */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
368 0, /* tp_new */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
369 0, /* tp_free */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
370 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
371 0, /* tp_bases */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
372 0, /* tp_mro */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
373 0, /* tp_cache */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
374 0, /* tp_subclasses */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
375 0, /* tp_weaklist */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33386
diff changeset
376 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
377 };
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
378
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
379 PyObject *
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
380 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
381 {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
382 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
383 if (gen == NULL) {
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
384 Py_DECREF(f);
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
385 return NULL;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
386 }
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
387 gen->gi_frame = f;
44976
51bb7b1ae765 #1473257: add generator.gi_code attribute that refers to
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
388 Py_INCREF(f->f_code);
51bb7b1ae765 #1473257: add generator.gi_code attribute that refers to
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
389 gen->gi_code = (PyObject *)(f->f_code);
32175
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
390 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
391 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
392 _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
393 return (PyObject *)gen;
8ee5a56b8031 Patch #957398: Add public API for Generator Object/Type.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
394 }
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
395
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
396 int
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
397 PyGen_NeedsFinalizing(PyGenObject *gen)
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
398 {
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
399 int i;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
400 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
401
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
402 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
403 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
404
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
405 /* 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
406 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
407 while (--i >= 0) {
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
408 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
409 return 1;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
410 }
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
411
37724
31929da0b05a Fix SF#1470508: crash in generator cycle finalization. There were two
Phillip J. Eby <pje@telecommunity.com>
parents: 37649
diff changeset
412 /* 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
413 return 0;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37202
diff changeset
414 }