summaryrefslogtreecommitdiff
path: root/psycopg/pqpath.c
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-03-05 02:48:11 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-03-05 02:48:11 +0000
commitb8c75d9de0874481ebbe6bd39ba16399ef7b3995 (patch)
tree4b1e7ee931329dace42df4a46ab6d98a21969f6f /psycopg/pqpath.c
parent37aa62ca52149360331e54740a2f70567343710e (diff)
parent2c309dfdb46cce51ff17688c97b391e39f2a392e (diff)
downloadpsycopg2-b8c75d9de0874481ebbe6bd39ba16399ef7b3995.tar.gz
Merge branch 'gcc-python-plugin' into devel
Diffstat (limited to 'psycopg/pqpath.c')
-rw-r--r--psycopg/pqpath.c64
1 files changed, 29 insertions, 35 deletions
diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c
index adbaa74..6ba4eca 100644
--- a/psycopg/pqpath.c
+++ b/psycopg/pqpath.c
@@ -64,7 +64,7 @@ strip_severity(const char *msg)
code. A list of error codes can be found at:
http://www.postgresql.org/docs/current/static/errcodes-appendix.html */
-static PyObject *
+BORROWED static PyObject *
exception_from_sqlstate(const char *sqlstate)
{
switch (sqlstate[0]) {
@@ -151,7 +151,7 @@ exception_from_sqlstate(const char *sqlstate)
This function should be called while holding the GIL. */
-static void
+RAISES static void
pq_raise(connectionObject *conn, cursorObject *curs, PGresult *pgres)
{
PyObject *exc = NULL;
@@ -164,7 +164,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult *pgres)
"psycopg went psycotic and raised a null error");
return;
}
-
+
/* if the connection has somehow beed broken, we mark the connection
object as closed but requiring cleanup */
if (conn->pgconn != NULL && PQstatus(conn->pgconn) == CONNECTION_BAD)
@@ -249,7 +249,9 @@ pq_clear_critical(connectionObject *conn)
}
}
-static PyObject *
+/* return -1 if the exception is set (i.e. if conn->critical is set),
+ * else 0 */
+RAISES_NEG static int
pq_resolve_critical(connectionObject *conn, int close)
{
Dprintf("pq_resolve_critical: resolving %s", conn->critical);
@@ -264,11 +266,13 @@ pq_resolve_critical(connectionObject *conn, int close)
/* we don't want to destroy this connection but just close it */
if (close == 1) conn_close(conn);
-
+
/* remember to clear the critical! */
- pq_clear_critical(conn);
+ pq_clear_critical(conn);
+
+ return -1;
}
- return NULL;
+ return 0;
}
/* pq_clear_async - clear the effects of a previous async query
@@ -300,19 +304,16 @@ pq_clear_async(connectionObject *conn)
Accepted arg values are 1 (nonblocking) and 0 (blocking).
- Return 0 if everything ok, else nonzero.
-
- In case of error, if pyerr is nonzero, set a Python exception.
+ Return 0 if everything ok, else < 0 and set an exception.
*/
-int
-pq_set_non_blocking(connectionObject *conn, int arg, int pyerr)
+RAISES_NEG int
+pq_set_non_blocking(connectionObject *conn, int arg)
{
int ret = PQsetnonblocking(conn->pgconn, arg);
if (0 != ret) {
Dprintf("PQsetnonblocking(%d) FAILED", arg);
- if (pyerr) {
- PyErr_SetString(OperationalError, "PQsetnonblocking() failed");
- }
+ PyErr_SetString(OperationalError, "PQsetnonblocking() failed");
+ ret = -1;
}
return ret;
}
@@ -382,7 +383,7 @@ cleanup:
This function should be called while holding the global interpreter
lock.
*/
-void
+RAISES void
pq_complete_error(connectionObject *conn, PGresult **pgres, char **error)
{
Dprintf("pq_complete_error: pgconn = %p, pgres = %p, error = %s",
@@ -476,7 +477,7 @@ pq_commit(connectionObject *conn)
return retvalue;
}
-int
+RAISES_NEG int
pq_abort_locked(connectionObject *conn, PGresult **pgres, char **error,
PyThreadState **tstate)
{
@@ -503,7 +504,7 @@ pq_abort_locked(connectionObject *conn, PGresult **pgres, char **error,
This function should be called while holding the global interpreter
lock. */
-int
+RAISES_NEG int
pq_abort(connectionObject *conn)
{
int retvalue = -1;
@@ -540,7 +541,7 @@ pq_abort(connectionObject *conn)
connection without holding the global interpreter lock.
*/
-int
+RAISES_NEG int
pq_reset_locked(connectionObject *conn, PGresult **pgres, char **error,
PyThreadState **tstate)
{
@@ -832,7 +833,7 @@ pq_flush(connectionObject *conn)
this fucntion locks the connection object
this function call Py_*_ALLOW_THREADS macros */
-int
+RAISES_NEG int
pq_execute(cursorObject *curs, const char *query, int async)
{
PGresult *pgres = NULL;
@@ -842,8 +843,7 @@ pq_execute(cursorObject *curs, const char *query, int async)
/* if the status of the connection is critical raise an exception and
definitely close the connection */
if (curs->conn->critical) {
- pq_resolve_critical(curs->conn, 1);
- return -1;
+ return pq_resolve_critical(curs->conn, 1);
}
/* check status of connection, raise error if not OK */
@@ -938,12 +938,13 @@ pq_execute(cursorObject *curs, const char *query, int async)
to respect the old DBAPI-2.0 compatible behaviour */
if (async == 0) {
Dprintf("pq_execute: entering syncronous DBAPI compatibility mode");
- if (pq_fetch(curs) == -1) return -1;
+ if (pq_fetch(curs) < 0) return -1;
}
else {
+ PyObject *tmp;
curs->conn->async_status = async_status;
- curs->conn->async_cursor = PyWeakref_NewRef((PyObject *)curs, NULL);
- if (!curs->conn->async_cursor) {
+ curs->conn->async_cursor = tmp = PyWeakref_NewRef((PyObject *)curs, NULL);
+ if (!tmp) {
/* weakref creation failed */
return -1;
}
@@ -1012,7 +1013,7 @@ pq_get_last_result(connectionObject *conn)
1 - result from backend (possibly data is ready)
*/
-static int
+RAISES_NEG static int
_pq_fetch_tuples(cursorObject *curs)
{
int i, *dsize = NULL;
@@ -1412,8 +1413,7 @@ pq_fetch(cursorObject *curs)
Dprintf("pq_fetch: got a NULL pgres, checking for critical");
pq_set_critical(curs->conn);
if (curs->conn->critical) {
- pq_resolve_critical(curs->conn);
- return -1;
+ return pq_resolve_critical(curs->conn);
}
else {
return 0;
@@ -1489,13 +1489,7 @@ pq_fetch(cursorObject *curs)
raise the exception but we avoid to close the connection) */
Dprintf("pq_fetch: fetching done; check for critical errors");
if (curs->conn->critical) {
- if (ex == -1) {
- pq_resolve_critical(curs->conn, 1);
- }
- else {
- pq_resolve_critical(curs->conn, 0);
- }
- return -1;
+ return pq_resolve_critical(curs->conn, ex == -1 ? 1 : 0);
}
return ex;