From 0830deb7902e1f0931a1e2396237bba604d8d670 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 20 Mar 2013 00:47:20 +0000 Subject: [PATCH] Dropped IFCLEARPGRES macro PQclear already guards against NULL, so the extra check is redundant --- psycopg/connection_int.c | 2 +- psycopg/cursor_type.c | 12 ++++++------ psycopg/error_type.c | 2 +- psycopg/pqpath.c | 24 ++++++++++++------------ psycopg/pqpath.h | 5 ++--- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 6cca1ce3..f36a0db4 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -890,7 +890,7 @@ conn_poll(connectionObject *self) } curs = (cursorObject *)py_curs; - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); curs->pgres = pq_get_last_result(self); /* fetch the tuples (if there are any) and build the result. We diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 75a953cc..9d3b4aaa 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -379,7 +379,7 @@ _psyco_curs_execute(cursorObject *self, if (operation == NULL) { goto exit; } - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); Py_CLEAR(self->query); Dprintf("psyco_curs_execute: starting execution of new query"); @@ -790,7 +790,7 @@ psyco_curs_fetchone(cursorObject *self) if (self->row >= self->rowcount && self->conn->async_cursor && PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self) - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); return res; } @@ -837,7 +837,7 @@ psyco_curs_next_named(cursorObject *self) if (self->row >= self->rowcount && self->conn->async_cursor && PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self) - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); return res; } @@ -922,7 +922,7 @@ psyco_curs_fetchmany(cursorObject *self, PyObject *args, PyObject *kwords) if (self->row >= self->rowcount && self->conn->async_cursor && PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self) - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); /* success */ rv = list; @@ -991,7 +991,7 @@ psyco_curs_fetchall(cursorObject *self) if (self->row >= self->rowcount && self->conn->async_cursor && PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self) - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); /* success */ rv = list; @@ -1904,7 +1904,7 @@ cursor_dealloc(PyObject* obj) Py_CLEAR(self->string_types); Py_CLEAR(self->binary_types); - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); Dprintf("cursor_dealloc: deleted cursor object at %p, refcnt = " FORMAT_CODE_PY_SSIZE_T, diff --git a/psycopg/error_type.c b/psycopg/error_type.c index cb632e0d..ac62c4e9 100644 --- a/psycopg/error_type.c +++ b/psycopg/error_type.c @@ -102,7 +102,7 @@ error_clear(errorObject *self) Py_CLEAR(self->pgcode); Py_CLEAR(self->cursor); PyMem_Free(self->codec); - IFCLEARPGRES(self->pgres); + CLEARPGRES(self->pgres); return ((PyTypeObject *)PyExc_StandardError)->tp_clear((PyObject *)self); } diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 13f64af1..34541402 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -230,7 +230,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) Py_CLEAR(perr->pgcode); perr->pgcode = error_text_from_chars(perr, code); - IFCLEARPGRES(perr->pgres); + CLEARPGRES(perr->pgres); if (pgres && *pgres) { perr->pgres = *pgres; *pgres = NULL; @@ -394,7 +394,7 @@ pq_execute_command_locked(connectionObject *conn, const char *query, } retvalue = 0; - IFCLEARPGRES(*pgres); + CLEARPGRES(*pgres); cleanup: return retvalue; @@ -902,7 +902,7 @@ pq_execute(cursorObject *curs, const char *query, int async, int no_result) } if (async == 0) { - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); Dprintf("pq_execute: executing SYNC query: pgconn = %p", curs->conn->pgconn); Dprintf(" %-.200s", query); if (!psyco_green()) { @@ -941,7 +941,7 @@ pq_execute(cursorObject *curs, const char *query, int async, int no_result) Dprintf("pq_execute: executing ASYNC query: pgconn = %p", curs->conn->pgconn); Dprintf(" %-.200s", query); - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); if (PQsendQuery(curs->conn->pgconn, query) == 0) { pthread_mutex_unlock(&(curs->conn->lock)); Py_BLOCK_THREADS; @@ -1338,7 +1338,7 @@ _pq_copy_in_v3(cursorObject *curs) /* XXX would be nice to propagate the exeption */ res = PQputCopyEnd(curs->conn->pgconn, "error in .read() call"); - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); Dprintf("_pq_copy_in_v3: copy ended; res = %d", res); @@ -1362,7 +1362,7 @@ _pq_copy_in_v3(cursorObject *curs) break; if (PQresultStatus(curs->pgres) == PGRES_FATAL_ERROR) pq_raise(curs->conn, curs, NULL); - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); } } @@ -1428,7 +1428,7 @@ _pq_copy_out_v3(cursorObject *curs) } /* and finally we grab the operation result from the backend */ - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); for (;;) { Py_BEGIN_ALLOW_THREADS; curs->pgres = PQgetResult(curs->conn->pgconn); @@ -1438,7 +1438,7 @@ _pq_copy_out_v3(cursorObject *curs) break; if (PQresultStatus(curs->pgres) == PGRES_FATAL_ERROR) pq_raise(curs->conn, curs, NULL); - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); } ret = 1; @@ -1499,7 +1499,7 @@ pq_fetch(cursorObject *curs, int no_result) curs->rowcount = -1; /* error caught by out glorious notice handler */ if (PyErr_Occurred()) ex = -1; - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); break; case PGRES_COPY_IN: @@ -1508,7 +1508,7 @@ pq_fetch(cursorObject *curs, int no_result) curs->rowcount = -1; /* error caught by out glorious notice handler */ if (PyErr_Occurred()) ex = -1; - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); break; case PGRES_TUPLES_OK: @@ -1520,7 +1520,7 @@ pq_fetch(cursorObject *curs, int no_result) } else { Dprintf("pq_fetch: got tuples, discarding them"); - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); curs->rowcount = -1; ex = 0; } @@ -1529,7 +1529,7 @@ pq_fetch(cursorObject *curs, int no_result) case PGRES_EMPTY_QUERY: PyErr_SetString(ProgrammingError, "can't execute an empty query"); - IFCLEARPGRES(curs->pgres); + CLEARPGRES(curs->pgres); ex = -1; break; diff --git a/psycopg/pqpath.h b/psycopg/pqpath.h index d697e48f..40beea19 100644 --- a/psycopg/pqpath.h +++ b/psycopg/pqpath.h @@ -29,9 +29,8 @@ #include "psycopg/cursor.h" #include "psycopg/connection.h" -/* macros to clean the pg result */ -#define IFCLEARPGRES(pgres) if (pgres) {PQclear(pgres); pgres = NULL;} -#define CLEARPGRES(pgres) PQclear(pgres); pgres = NULL +/* macro to clean the pg result */ +#define CLEARPGRES(pgres) do { PQclear(pgres); pgres = NULL; } while (0) /* exported functions */ HIDDEN PGresult *pq_get_last_result(connectionObject *conn);