From eb280c1da4d2d1c372c200e5b1f42eed1ddf6bf7 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 20 Mar 2013 22:15:32 +0000 Subject: [PATCH] Better use of Py_CLEAR and implicit zeroed-out structures --- psycopg/cursor_int.c | 12 ++------ psycopg/cursor_type.c | 19 +------------ psycopg/notify_type.c | 4 +-- psycopg/xid_type.c | 66 ++++++++++--------------------------------- 4 files changed, 19 insertions(+), 82 deletions(-) diff --git a/psycopg/cursor_int.c b/psycopg/cursor_int.c index 1ac3f550..dd4c0d7d 100644 --- a/psycopg/cursor_int.c +++ b/psycopg/cursor_int.c @@ -72,19 +72,11 @@ curs_get_cast(cursorObject *self, PyObject *oid) void curs_reset(cursorObject *self) { - PyObject *tmp; - /* initialize some variables to default values */ self->notuples = 1; self->rowcount = -1; self->row = 0; - tmp = self->description; - Py_INCREF(Py_None); - self->description = Py_None; - Py_XDECREF(tmp); - - tmp = self->casts; - self->casts = NULL; - Py_XDECREF(tmp); + Py_CLEAR(self->description); + Py_CLEAR(self->casts); } diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 4b221902..dd1780d7 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -474,7 +474,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs) } if (self->name != NULL) { - if (self->query != Py_None) { + if (self->query) { psyco_set_error(ProgrammingError, self, "can't call .execute() on named cursors more than once"); return NULL; @@ -1843,32 +1843,15 @@ cursor_setup(cursorObject *self, connectionObject *conn, const char *name) Py_INCREF(conn); self->conn = conn; - self->closed = 0; - self->withhold = 0; - self->scrollable = 0; self->mark = conn->mark; - self->pgres = NULL; self->notuples = 1; self->arraysize = 1; self->itersize = 2000; self->rowcount = -1; 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); self->tuple_factory = Py_None; - Py_INCREF(Py_None); - self->query = Py_None; /* default tzinfo factory */ Py_INCREF(pyPsycopgTzFixedOffsetTimezone); diff --git a/psycopg/notify_type.c b/psycopg/notify_type.c index beec933c..17620c4d 100644 --- a/psycopg/notify_type.c +++ b/psycopg/notify_type.c @@ -61,9 +61,7 @@ static PyMemberDef notify_members[] = { static PyObject * notify_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { - notifyObject *self = (notifyObject *)type->tp_alloc(type, 0); - - return (PyObject *)self; + return type->tp_alloc(type, 0); } static int diff --git a/psycopg/xid_type.c b/psycopg/xid_type.c index 70cbb048..bd4f0e4c 100644 --- a/psycopg/xid_type.c +++ b/psycopg/xid_type.c @@ -79,24 +79,7 @@ static PyMemberDef xid_members[] = { static PyObject * xid_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { - xidObject *self; - - 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; + return type->tp_alloc(type, 0); } static int @@ -106,7 +89,6 @@ xid_init(xidObject *self, PyObject *args, PyObject *kwargs) int format_id; size_t i, gtrid_len, bqual_len; const char *gtrid, *bqual; - PyObject *tmp; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iss", kwlist, &format_id, >rid, &bqual)) @@ -149,17 +131,12 @@ xid_init(xidObject *self, PyObject *args, PyObject *kwargs) } } - tmp = self->format_id; self->format_id = PyInt_FromLong(format_id); - Py_XDECREF(tmp); - - tmp = self->gtrid; self->gtrid = Text_FromUTF8(gtrid); - Py_XDECREF(tmp); - - tmp = self->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; } @@ -555,7 +532,6 @@ static xidObject * _xid_unparsed_from_string(PyObject *str) { xidObject *xid = NULL; xidObject *rv = NULL; - PyObject *tmp; /* fake args to work around the checks performed by the xid init */ if (!(xid = (xidObject *)PyObject_CallFunction((PyObject *)&xidType, @@ -564,22 +540,19 @@ _xid_unparsed_from_string(PyObject *str) { } /* set xid.gtrid = str */ - tmp = xid->gtrid; + Py_CLEAR(xid->gtrid); Py_INCREF(str); xid->gtrid = str; - Py_DECREF(tmp); /* set xid.format_id = None */ - tmp = xid->format_id; + Py_CLEAR(xid->format_id); Py_INCREF(Py_None); xid->format_id = Py_None; - Py_DECREF(tmp); /* set xid.bqual = None */ - tmp = xid->bqual; + Py_CLEAR(xid->bqual); Py_INCREF(Py_None); xid->bqual = Py_None; - Py_DECREF(tmp); /* return the finished object */ rv = xid; @@ -665,34 +638,25 @@ xid_recover(PyObject *conn) /* Get the xid with the XA triple set */ if (!(item = PySequence_GetItem(rec, 0))) { goto exit; } if (!(xid = xid_from_string(item))) { goto exit; } - Py_DECREF(item); item = NULL; + Py_CLEAR(item); /* set xid.prepared */ - if (!(item = PySequence_GetItem(rec, 1))) { goto exit; } - tmp = xid->prepared; - xid->prepared = item; - Py_DECREF(tmp); - item = NULL; + Py_CLEAR(xid->prepared); + if (!(xid->prepared = PySequence_GetItem(rec, 1))) { goto exit; } /* set xid.owner */ - if (!(item = PySequence_GetItem(rec, 2))) { goto exit; } - tmp = xid->owner; - xid->owner = item; - Py_DECREF(tmp); - item = NULL; + Py_CLEAR(xid->owner); + if (!(xid->owner = PySequence_GetItem(rec, 2))) { goto exit; } /* set xid.database */ - if (!(item = PySequence_GetItem(rec, 3))) { goto exit; } - tmp = xid->database; - xid->database = item; - Py_DECREF(tmp); - item = NULL; + Py_CLEAR(xid->database); + if (!(xid->database = PySequence_GetItem(rec, 3))) { goto exit; } /* xid finished: add it to the returned list */ PyList_SET_ITEM(xids, i, (PyObject *)xid); xid = NULL; /* ref stolen */ - Py_DECREF(rec); rec = NULL; + Py_CLEAR(rec); } /* set the return value. */