Skip to content

Commit b4df670

Browse files
author
phillip.eby
committed
Don't set gi_frame to Py_None, use NULL instead, eliminating some insane
pointer dereferences. git-svn-id: http://svn.python.org/projects/python/trunk@45316 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 65c1b46 commit b4df670

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

Include/genobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typedef struct {
1313
PyObject_HEAD
1414
/* The gi_ prefix is intended to remind of generator-iterator. */
1515

16+
/* Note: gi_frame can be NULL if the generator is "finished" */
1617
struct _frame *gi_frame;
1718

1819
/* True if generator is being executed. */

Objects/genobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
static int
1111
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
1212
{
13-
return visit((PyObject *)gen->gi_frame, arg);
13+
Py_VISIT(gen->gi_frame);
14+
return 0;
1415
}
1516

1617
static void
@@ -26,7 +27,7 @@ gen_dealloc(PyGenObject *gen)
2627

2728
_PyObject_GC_TRACK(self);
2829

29-
if (gen->gi_frame->f_stacktop!=NULL) {
30+
if (gen->gi_frame!=NULL && gen->gi_frame->f_stacktop!=NULL) {
3031
/* Generator is paused, so we need to close */
3132
gen->ob_type->tp_del(self);
3233
if (self->ob_refcnt > 0)
@@ -51,7 +52,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
5152
"generator already executing");
5253
return NULL;
5354
}
54-
if ((PyObject *)f == Py_None || f->f_stacktop == NULL) {
55+
if (f==NULL || f->f_stacktop == NULL) {
5556
/* Only set exception if called from send() */
5657
if (arg && !exc) PyErr_SetNone(PyExc_StopIteration);
5758
return NULL;
@@ -98,8 +99,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
9899
if (!result || f->f_stacktop == NULL) {
99100
/* generator can't be rerun, so release the frame */
100101
Py_DECREF(f);
101-
gen->gi_frame = (PyFrameObject *)Py_None;
102-
Py_INCREF(Py_None);
102+
gen->gi_frame = NULL;
103103
}
104104

105105
return result;
@@ -147,7 +147,7 @@ gen_del(PyObject *self)
147147
PyObject *error_type, *error_value, *error_traceback;
148148
PyGenObject *gen = (PyGenObject *)self;
149149

150-
if ((PyObject *)gen->gi_frame == Py_None || gen->gi_frame->f_stacktop==NULL)
150+
if (!gen->gi_frame || gen->gi_frame->f_stacktop==NULL)
151151
/* Generator isn't paused, so no need to close */
152152
return;
153153

@@ -366,7 +366,7 @@ PyGen_NeedsFinalizing(PyGenObject *gen)
366366
int i;
367367
PyFrameObject *f = gen->gi_frame;
368368

369-
if ((PyObject *)f == Py_None || f->f_stacktop==NULL || f->f_iblock<=0)
369+
if (f == NULL || f->f_stacktop==NULL || f->f_iblock<=0)
370370
return 0; /* no frame or no blockstack == no finalization */
371371

372372
for (i=f->f_iblock; i>=0; i--) {

0 commit comments

Comments
 (0)