From 8920bf24f8eeaf6e8683fd5461b2c1baad325e63 Mon Sep 17 00:00:00 2001 From: "Phillip J. Eby" Date: Wed, 12 Apr 2006 19:07:15 +0000 Subject: [PATCH] Don't set gi_frame to Py_None, use NULL instead, eliminating some insane pointer dereferences. --- Include/genobject.h | 1 + Objects/genobject.c | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Include/genobject.h b/Include/genobject.h index 1ecd7ad773c..ca8443203c7 100644 --- a/Include/genobject.h +++ b/Include/genobject.h @@ -13,6 +13,7 @@ typedef struct { PyObject_HEAD /* The gi_ prefix is intended to remind of generator-iterator. */ + /* Note: gi_frame can be NULL if the generator is "finished" */ struct _frame *gi_frame; /* True if generator is being executed. */ diff --git a/Objects/genobject.c b/Objects/genobject.c index f5d0a5e7b36..367b4de0272 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -10,7 +10,8 @@ static int gen_traverse(PyGenObject *gen, visitproc visit, void *arg) { - return visit((PyObject *)gen->gi_frame, arg); + Py_VISIT(gen->gi_frame); + return 0; } static void @@ -26,7 +27,7 @@ gen_dealloc(PyGenObject *gen) _PyObject_GC_TRACK(self); - if (gen->gi_frame->f_stacktop!=NULL) { + if (gen->gi_frame!=NULL && gen->gi_frame->f_stacktop!=NULL) { /* Generator is paused, so we need to close */ gen->ob_type->tp_del(self); if (self->ob_refcnt > 0) @@ -51,7 +52,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) "generator already executing"); return NULL; } - if ((PyObject *)f == Py_None || f->f_stacktop == NULL) { + if (f==NULL || f->f_stacktop == NULL) { /* Only set exception if called from send() */ if (arg && !exc) PyErr_SetNone(PyExc_StopIteration); return NULL; @@ -98,8 +99,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) if (!result || f->f_stacktop == NULL) { /* generator can't be rerun, so release the frame */ Py_DECREF(f); - gen->gi_frame = (PyFrameObject *)Py_None; - Py_INCREF(Py_None); + gen->gi_frame = NULL; } return result; @@ -147,7 +147,7 @@ gen_del(PyObject *self) PyObject *error_type, *error_value, *error_traceback; PyGenObject *gen = (PyGenObject *)self; - if ((PyObject *)gen->gi_frame == Py_None || gen->gi_frame->f_stacktop==NULL) + if (!gen->gi_frame || gen->gi_frame->f_stacktop==NULL) /* Generator isn't paused, so no need to close */ return; @@ -366,7 +366,7 @@ PyGen_NeedsFinalizing(PyGenObject *gen) int i; PyFrameObject *f = gen->gi_frame; - if ((PyObject *)f == Py_None || f->f_stacktop==NULL || f->f_iblock<=0) + if (f == NULL || f->f_stacktop==NULL || f->f_iblock<=0) return 0; /* no frame or no blockstack == no finalization */ for (i=f->f_iblock; i>=0; i--) {