Better use of Py_CLEAR and implicit zeroed-out structures

This commit is contained in:
Daniele Varrazzo 2013-03-20 22:15:32 +00:00
parent 546927d44a
commit eb280c1da4
4 changed files with 19 additions and 82 deletions

View File

@ -72,19 +72,11 @@ curs_get_cast(cursorObject *self, PyObject *oid)
void void
curs_reset(cursorObject *self) curs_reset(cursorObject *self)
{ {
PyObject *tmp;
/* initialize some variables to default values */ /* initialize some variables to default values */
self->notuples = 1; self->notuples = 1;
self->rowcount = -1; self->rowcount = -1;
self->row = 0; self->row = 0;
tmp = self->description; Py_CLEAR(self->description);
Py_INCREF(Py_None); Py_CLEAR(self->casts);
self->description = Py_None;
Py_XDECREF(tmp);
tmp = self->casts;
self->casts = NULL;
Py_XDECREF(tmp);
} }

View File

@ -474,7 +474,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
} }
if (self->name != NULL) { if (self->name != NULL) {
if (self->query != Py_None) { if (self->query) {
psyco_set_error(ProgrammingError, self, psyco_set_error(ProgrammingError, self,
"can't call .execute() on named cursors more than once"); "can't call .execute() on named cursors more than once");
return NULL; return NULL;
@ -1843,32 +1843,15 @@ cursor_setup(cursorObject *self, connectionObject *conn, const char *name)
Py_INCREF(conn); Py_INCREF(conn);
self->conn = conn; self->conn = conn;
self->closed = 0;
self->withhold = 0;
self->scrollable = 0;
self->mark = conn->mark; self->mark = conn->mark;
self->pgres = NULL;
self->notuples = 1; self->notuples = 1;
self->arraysize = 1; self->arraysize = 1;
self->itersize = 2000; self->itersize = 2000;
self->rowcount = -1; self->rowcount = -1;
self->lastoid = InvalidOid; self->lastoid = InvalidOid;
self->casts = NULL;
self->notice = NULL;
self->string_types = NULL;
self->binary_types = NULL;
self->weakreflist = NULL;
Py_INCREF(Py_None);
self->description = Py_None;
Py_INCREF(Py_None);
self->pgstatus = Py_None;
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->tuple_factory = Py_None; self->tuple_factory = Py_None;
Py_INCREF(Py_None);
self->query = Py_None;
/* default tzinfo factory */ /* default tzinfo factory */
Py_INCREF(pyPsycopgTzFixedOffsetTimezone); Py_INCREF(pyPsycopgTzFixedOffsetTimezone);

View File

@ -61,9 +61,7 @@ static PyMemberDef notify_members[] = {
static PyObject * static PyObject *
notify_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) notify_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ {
notifyObject *self = (notifyObject *)type->tp_alloc(type, 0); return type->tp_alloc(type, 0);
return (PyObject *)self;
} }
static int static int

View File

@ -79,24 +79,7 @@ static PyMemberDef xid_members[] = {
static PyObject * static PyObject *
xid_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) xid_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ {
xidObject *self; return type->tp_alloc(type, 0);
if (!(self = (xidObject *)type->tp_alloc(type, 0))) { return NULL; }
Py_INCREF(Py_None);
self->format_id = Py_None;
Py_INCREF(Py_None);
self->gtrid = Py_None;
Py_INCREF(Py_None);
self->bqual = Py_None;
Py_INCREF(Py_None);
self->prepared = Py_None;
Py_INCREF(Py_None);
self->owner = Py_None;
Py_INCREF(Py_None);
self->database = Py_None;
return (PyObject *)self;
} }
static int static int
@ -106,7 +89,6 @@ xid_init(xidObject *self, PyObject *args, PyObject *kwargs)
int format_id; int format_id;
size_t i, gtrid_len, bqual_len; size_t i, gtrid_len, bqual_len;
const char *gtrid, *bqual; const char *gtrid, *bqual;
PyObject *tmp;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iss", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iss", kwlist,
&format_id, &gtrid, &bqual)) &format_id, &gtrid, &bqual))
@ -149,17 +131,12 @@ xid_init(xidObject *self, PyObject *args, PyObject *kwargs)
} }
} }
tmp = self->format_id;
self->format_id = PyInt_FromLong(format_id); self->format_id = PyInt_FromLong(format_id);
Py_XDECREF(tmp);
tmp = self->gtrid;
self->gtrid = Text_FromUTF8(gtrid); self->gtrid = Text_FromUTF8(gtrid);
Py_XDECREF(tmp);
tmp = self->bqual;
self->bqual = Text_FromUTF8(bqual); self->bqual = Text_FromUTF8(bqual);
Py_XDECREF(tmp); Py_INCREF(Py_None); self->prepared = Py_None;
Py_INCREF(Py_None); self->owner = Py_None;
Py_INCREF(Py_None); self->database = Py_None;
return 0; return 0;
} }
@ -555,7 +532,6 @@ static xidObject *
_xid_unparsed_from_string(PyObject *str) { _xid_unparsed_from_string(PyObject *str) {
xidObject *xid = NULL; xidObject *xid = NULL;
xidObject *rv = NULL; xidObject *rv = NULL;
PyObject *tmp;
/* fake args to work around the checks performed by the xid init */ /* fake args to work around the checks performed by the xid init */
if (!(xid = (xidObject *)PyObject_CallFunction((PyObject *)&xidType, if (!(xid = (xidObject *)PyObject_CallFunction((PyObject *)&xidType,
@ -564,22 +540,19 @@ _xid_unparsed_from_string(PyObject *str) {
} }
/* set xid.gtrid = str */ /* set xid.gtrid = str */
tmp = xid->gtrid; Py_CLEAR(xid->gtrid);
Py_INCREF(str); Py_INCREF(str);
xid->gtrid = str; xid->gtrid = str;
Py_DECREF(tmp);
/* set xid.format_id = None */ /* set xid.format_id = None */
tmp = xid->format_id; Py_CLEAR(xid->format_id);
Py_INCREF(Py_None); Py_INCREF(Py_None);
xid->format_id = Py_None; xid->format_id = Py_None;
Py_DECREF(tmp);
/* set xid.bqual = None */ /* set xid.bqual = None */
tmp = xid->bqual; Py_CLEAR(xid->bqual);
Py_INCREF(Py_None); Py_INCREF(Py_None);
xid->bqual = Py_None; xid->bqual = Py_None;
Py_DECREF(tmp);
/* return the finished object */ /* return the finished object */
rv = xid; rv = xid;
@ -665,34 +638,25 @@ xid_recover(PyObject *conn)
/* Get the xid with the XA triple set */ /* Get the xid with the XA triple set */
if (!(item = PySequence_GetItem(rec, 0))) { goto exit; } if (!(item = PySequence_GetItem(rec, 0))) { goto exit; }
if (!(xid = xid_from_string(item))) { goto exit; } if (!(xid = xid_from_string(item))) { goto exit; }
Py_DECREF(item); item = NULL; Py_CLEAR(item);
/* set xid.prepared */ /* set xid.prepared */
if (!(item = PySequence_GetItem(rec, 1))) { goto exit; } Py_CLEAR(xid->prepared);
tmp = xid->prepared; if (!(xid->prepared = PySequence_GetItem(rec, 1))) { goto exit; }
xid->prepared = item;
Py_DECREF(tmp);
item = NULL;
/* set xid.owner */ /* set xid.owner */
if (!(item = PySequence_GetItem(rec, 2))) { goto exit; } Py_CLEAR(xid->owner);
tmp = xid->owner; if (!(xid->owner = PySequence_GetItem(rec, 2))) { goto exit; }
xid->owner = item;
Py_DECREF(tmp);
item = NULL;
/* set xid.database */ /* set xid.database */
if (!(item = PySequence_GetItem(rec, 3))) { goto exit; } Py_CLEAR(xid->database);
tmp = xid->database; if (!(xid->database = PySequence_GetItem(rec, 3))) { goto exit; }
xid->database = item;
Py_DECREF(tmp);
item = NULL;
/* xid finished: add it to the returned list */ /* xid finished: add it to the returned list */
PyList_SET_ITEM(xids, i, (PyObject *)xid); PyList_SET_ITEM(xids, i, (PyObject *)xid);
xid = NULL; /* ref stolen */ xid = NULL; /* ref stolen */
Py_DECREF(rec); rec = NULL; Py_CLEAR(rec);
} }
/* set the return value. */ /* set the return value. */