Simplified psyco_wait() function interface.

This commit is contained in:
Daniele Varrazzo 2010-04-25 23:01:53 +01:00
parent 8efa1fa6af
commit 2596cf7125
3 changed files with 16 additions and 25 deletions

View File

@ -346,7 +346,6 @@ static int
_conn_sync_connect(connectionObject *self) _conn_sync_connect(connectionObject *self)
{ {
PGconn *pgconn; PGconn *pgconn;
PyObject *wait_rv;
int green; int green;
/* store this value to prevent inconsistencies due to a change /* store this value to prevent inconsistencies due to a change
@ -382,10 +381,7 @@ _conn_sync_connect(connectionObject *self)
/* if the connection is green, wait to finish connection */ /* if the connection is green, wait to finish connection */
if (green) { if (green) {
wait_rv = psyco_wait(self); if (0 != psyco_wait(self)) {
if (wait_rv) {
Py_DECREF(wait_rv);
} else {
return -1; return -1;
} }
} }

View File

@ -114,9 +114,9 @@ have_wait_callback()
* raise `InterfaceError` if it is not. Use `psyco_green()` to check if * raise `InterfaceError` if it is not. Use `psyco_green()` to check if
* the function is to be called. * the function is to be called.
* *
* The function returns the return value of the called function. * Return 0 on success, else nonzero and set a Python exception.
*/ */
PyObject * int
psyco_wait(connectionObject *conn) psyco_wait(connectionObject *conn)
{ {
PyObject *rv; PyObject *rv;
@ -124,13 +124,19 @@ psyco_wait(connectionObject *conn)
Dprintf("psyco_wait"); Dprintf("psyco_wait");
if (!(cb = have_wait_callback())) { if (!(cb = have_wait_callback())) {
return NULL; return -1;
} }
rv = PyObject_CallFunctionObjArgs(cb, conn, NULL); rv = PyObject_CallFunctionObjArgs(cb, conn, NULL);
Py_DECREF(cb); Py_DECREF(cb);
return rv; if (NULL != rv) {
Py_DECREF(rv);
return 0;
} else {
Dprintf("psyco_wait: error in wait callback");
return -1;
}
} }
/* Replacement for PQexec using the user-provided wait function. /* Replacement for PQexec using the user-provided wait function.
@ -145,16 +151,10 @@ PGresult *
psyco_exec_green(connectionObject *conn, const char *command) psyco_exec_green(connectionObject *conn, const char *command)
{ {
PGresult *result = NULL; PGresult *result = NULL;
PyObject *cb, *pyrv;
Dprintf("psyco_exec_green: executing query async");
if (!(cb = have_wait_callback())) {
goto end;
}
/* Send the query asynchronously */ /* Send the query asynchronously */
if (0 == pq_send_query(conn, command)) { if (0 == pq_send_query(conn, command)) {
goto clear; goto end;
} }
/* Enter the poll loop with a write. When writing is finished the poll /* Enter the poll loop with a write. When writing is finished the poll
@ -163,21 +163,16 @@ psyco_exec_green(connectionObject *conn, const char *command)
*/ */
conn->async_status = ASYNC_WRITE; conn->async_status = ASYNC_WRITE;
pyrv = PyObject_CallFunctionObjArgs(cb, conn, NULL); if (0 != psyco_wait(conn)) {
if (!pyrv) {
Dprintf("psyco_exec_green: error in wait callback");
psyco_clear_result_blocking(conn); psyco_clear_result_blocking(conn);
goto clear; goto end;
} }
Py_DECREF(pyrv);
/* Now we can read the data without fear of blocking. */ /* Now we can read the data without fear of blocking. */
result = pq_get_last_result(conn); result = pq_get_last_result(conn);
clear:
conn->async_status = ASYNC_DONE;
Py_DECREF(cb);
end: end:
conn->async_status = ASYNC_DONE;
return result; return result;
} }

View File

@ -59,7 +59,7 @@ HIDDEN PyObject *psyco_set_wait_callback(PyObject *self, PyObject *obj);
HIDDEN PyObject *psyco_get_wait_callback(PyObject *self, PyObject *obj); HIDDEN PyObject *psyco_get_wait_callback(PyObject *self, PyObject *obj);
HIDDEN int psyco_green(void); HIDDEN int psyco_green(void);
HIDDEN PyObject *psyco_wait(connectionObject *conn); HIDDEN int psyco_wait(connectionObject *conn);
HIDDEN PGresult *psyco_exec_green(connectionObject *conn, const char *command); HIDDEN PGresult *psyco_exec_green(connectionObject *conn, const char *command);
#ifdef __cplusplus #ifdef __cplusplus