From 97220eadc65c03abf2b36fbff85b4f4f531e074b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 17 Mar 2019 03:10:10 +0000 Subject: [PATCH] Added helper methods to set a result into a connection/cursor --- psycopg/connection.h | 1 + psycopg/connection_int.c | 11 +++++++++-- psycopg/cursor.h | 1 + psycopg/cursor_int.c | 8 ++++++++ psycopg/pqpath.c | 31 +++++++++++++------------------ 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/psycopg/connection.h b/psycopg/connection.h index 8018a5e6..6c7ade6e 100644 --- a/psycopg/connection.h +++ b/psycopg/connection.h @@ -174,6 +174,7 @@ RAISES_NEG HIDDEN int conn_tpc_begin(connectionObject *self, xidObject *xid); RAISES_NEG HIDDEN int conn_tpc_command(connectionObject *self, const char *cmd, xidObject *xid); HIDDEN PyObject *conn_tpc_recover(connectionObject *self); +HIDDEN void conn_set_result(connectionObject *self, PGresult *pgres); /* exception-raising macros */ #define EXC_IF_CONN_CLOSED(self) if ((self)->closed > 0) { \ diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index f7436022..7c263e83 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -1119,8 +1119,7 @@ conn_poll(connectionObject *self) break; } - PQclear(curs->pgres); - curs->pgres = self->pgres; + curs_set_result(curs, self->pgres); self->pgres = NULL; /* fetch the tuples (if there are any) and build the result. We @@ -1487,3 +1486,11 @@ exit: return rv; } + + +void +conn_set_result(connectionObject *self, PGresult *pgres) +{ + PQclear(self->pgres); + self->pgres = pgres; +} diff --git a/psycopg/cursor.h b/psycopg/cursor.h index 30d0d34e..bc91cbb0 100644 --- a/psycopg/cursor.h +++ b/psycopg/cursor.h @@ -96,6 +96,7 @@ HIDDEN void curs_reset(cursorObject *self); RAISES_NEG HIDDEN int psyco_curs_withhold_set(cursorObject *self, PyObject *pyvalue); RAISES_NEG HIDDEN int psyco_curs_scrollable_set(cursorObject *self, PyObject *pyvalue); HIDDEN PyObject *psyco_curs_validate_sql_basic(cursorObject *self, PyObject *sql); +HIDDEN void curs_set_result(cursorObject *self, PGresult *pgres); /* exception-raising macros */ #define EXC_IF_CURS_CLOSED(self) \ diff --git a/psycopg/cursor_int.c b/psycopg/cursor_int.c index bebbeaab..453bf619 100644 --- a/psycopg/cursor_int.c +++ b/psycopg/cursor_int.c @@ -160,3 +160,11 @@ exit: Py_XDECREF(comp); return rv; } + + +void +curs_set_result(cursorObject *self, PGresult *pgres) +{ + PQclear(self->pgres); + self->pgres = pgres; +} diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 722a6ae6..062c5425 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -273,9 +273,9 @@ pq_clear_async(connectionObject *conn) finalize asynchronous processing so the connection will be ready to accept another query */ - while ((pgres = PQgetResult(conn->pgconn)) != NULL) { + while ((pgres = PQgetResult(conn->pgconn))) { Dprintf("pq_clear_async: clearing PGresult at %p", pgres); - CLEARPGRES(pgres); + PQclear(pgres); } Py_CLEAR(conn->async_cursor); } @@ -321,12 +321,11 @@ pq_execute_command_locked(connectionObject *conn, const char *query, conn->pgconn, query); *error = NULL; - CLEARPGRES(conn->pgres); if (!psyco_green()) { - conn->pgres = PQexec(conn->pgconn, query); + conn_set_result(conn, PQexec(conn->pgconn, query)); } else { PyEval_RestoreThread(*tstate); - conn->pgres = psyco_exec_green(conn, query); + conn_set_result(conn, psyco_exec_green(conn, query)); *tstate = PyEval_SaveThread(); } if (conn->pgres == NULL) { @@ -647,13 +646,12 @@ pq_get_guc_locked( Dprintf("pq_get_guc_locked: pgconn = %p, query = %s", conn->pgconn, query); *error = NULL; - CLEARPGRES(conn->pgres); if (!psyco_green()) { - conn->pgres = PQexec(conn->pgconn, query); + conn_set_result(conn, PQexec(conn->pgconn, query)); } else { PyEval_RestoreThread(*tstate); - conn->pgres = psyco_exec_green(conn, query); + conn_set_result(conn, psyco_exec_green(conn, query)); *tstate = PyEval_SaveThread(); } @@ -826,8 +824,7 @@ pq_get_result_async(connectionObject *conn) PQclear(res); } else { - PQclear(conn->pgres); - conn->pgres = res; + conn_set_result(conn, res); } switch (status) { @@ -903,15 +900,14 @@ _pq_execute_sync(cursorObject *curs, const char *query, int no_result, int no_be return -1; } - CLEARPGRES(conn->pgres); Dprintf("pq_execute: executing SYNC query: pgconn = %p", conn->pgconn); Dprintf(" %-.200s", query); if (!psyco_green()) { - conn->pgres = PQexec(conn->pgconn, query); + conn_set_result(conn, PQexec(conn->pgconn, query)); } else { Py_BLOCK_THREADS; - conn->pgres = psyco_exec_green(conn, query); + conn_set_result(conn, psyco_exec_green(conn, query)); Py_UNBLOCK_THREADS; } @@ -932,7 +928,7 @@ _pq_execute_sync(cursorObject *curs, const char *query, int no_result, int no_be Py_BLOCK_THREADS; /* assign the result back to the cursor now that we have the GIL */ - curs->pgres = conn->pgres; + curs_set_result(curs, conn->pgres); conn->pgres = NULL; /* Process notifies here instead of when fetching the tuple as we are @@ -1422,7 +1418,7 @@ _pq_copy_in_v3(cursorObject *curs) /* and finally we grab the operation result from the backend */ for (;;) { Py_BEGIN_ALLOW_THREADS; - curs->pgres = PQgetResult(curs->conn->pgconn); + curs_set_result(curs, PQgetResult(curs->conn->pgconn)); Py_END_ALLOW_THREADS; if (NULL == curs->pgres) @@ -1503,10 +1499,9 @@ _pq_copy_out_v3(cursorObject *curs) } /* and finally we grab the operation result from the backend */ - CLEARPGRES(curs->pgres); for (;;) { Py_BEGIN_ALLOW_THREADS; - curs->pgres = PQgetResult(curs->conn->pgconn); + curs_set_result(curs, PQgetResult(curs->conn->pgconn)); Py_END_ALLOW_THREADS; if (NULL == curs->pgres) @@ -1585,7 +1580,7 @@ retry: } if (len == -1) { /* EOF */ - curs->pgres = PQgetResult(pgconn); + curs_set_result(curs, PQgetResult(pgconn)); if (curs->pgres && PQresultStatus(curs->pgres) == PGRES_FATAL_ERROR) { pq_raise(conn, curs, NULL);