Make asynchronous connections produce asynchronous cursors by default

Drop the async kwarg from cursor.execute(), cursors created by
asynchronous connections will be asynchronous by default, ones created
by synchronous connections will be synchronous.

Mind that this might break third party subclasses of
psycopg2.extensions.cursor, if they try to chain to the superclass in
their execute() implementation and are passing the async kwarg. The
example cursors in psycopg2.extras have been fixed no to do that.
This commit is contained in:
Jan Urbański 2010-03-31 01:43:07 +02:00 committed by Federico Di Gregorio
parent d8ab5ac8a1
commit 01799e9137
2 changed files with 17 additions and 52 deletions

View File

@ -112,10 +112,10 @@ class DictCursor(DictCursorBase):
DictCursorBase.__init__(self, *args, **kwargs)
self._prefetch = 1
def execute(self, query, vars=None, async=0):
def execute(self, query, vars=None):
self.index = {}
self._query_executed = 1
return _cursor.execute(self, query, vars, async)
return _cursor.execute(self, query, vars)
def callproc(self, procname, vars=None):
self.index = {}
@ -201,10 +201,10 @@ class RealDictCursor(DictCursorBase):
DictCursorBase.__init__(self, *args, **kwargs)
self._prefetch = 0
def execute(self, query, vars=None, async=0):
def execute(self, query, vars=None):
self.column_mapping = []
self._query_executed = 1
return _cursor.execute(self, query, vars, async)
return _cursor.execute(self, query, vars)
def callproc(self, procname, vars=None):
self.column_mapping = []
@ -282,9 +282,9 @@ class LoggingConnection(_connection):
class LoggingCursor(_cursor):
"""A cursor that logs queries using its connection logging facilities."""
def execute(self, query, vars=None, async=0):
def execute(self, query, vars=None):
try:
return _cursor.execute(self, query, vars, async)
return _cursor.execute(self, query, vars)
finally:
self.connection.log(self.query, self)
@ -325,9 +325,9 @@ class MinTimeLoggingConnection(LoggingConnection):
class MinTimeLoggingCursor(LoggingCursor):
"""The cursor sub-class companion to `MinTimeLoggingConnection`."""
def execute(self, query, vars=None, async=0):
def execute(self, query, vars=None):
self.timestamp = time.time()
return LoggingCursor.execute(self, query, vars, async)
return LoggingCursor.execute(self, query, vars)
def callproc(self, procname, vars=None):
self.timestamp = time.time()

View File

@ -305,7 +305,7 @@ static PyObject *_psyco_curs_validate_sql_basic(
}
#define psyco_curs_execute_doc \
"execute(query, vars=None, async=0) -- Execute query with bound vars."
"execute(query, vars=None) -- Execute query with bound vars."
static int
_psyco_curs_execute(cursorObject *self,
@ -442,29 +442,12 @@ _psyco_curs_execute(cursorObject *self,
static PyObject *
psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
{
long int async;
PyObject *vars = NULL, *operation = NULL;
static char *kwlist[] = {"query", "vars", "async", NULL};
static char *kwlist[] = {"query", "vars", NULL};
async = self->conn->async;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Ol", kwlist,
&operation, &vars, &async)) {
return NULL;
}
if (async != self->conn->async) {
if (async == 0)
psyco_set_error(ProgrammingError, (PyObject*)self,
"can't execute a synchronous query "
"from an asynchronous cursor",
NULL, NULL);
else
psyco_set_error(ProgrammingError, (PyObject*)self,
"can't execute an asynchronous query "
"from a synchronous cursor",
NULL, NULL);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist,
&operation, &vars)) {
return NULL;
}
@ -489,7 +472,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
EXC_IF_CURS_CLOSED(self);
if (_psyco_curs_execute(self, operation, vars, async)) {
if (_psyco_curs_execute(self, operation, vars, self->conn->async)) {
Py_INCREF(Py_None);
return Py_None;
}
@ -958,41 +941,23 @@ psyco_curs_fetchall(cursorObject *self, PyObject *args)
/* callproc method - execute a stored procedure */
#define psyco_curs_callproc_doc \
"callproc(procname, parameters=None, async=0) -- Execute stored procedure."
"callproc(procname, parameters=None) -- Execute stored procedure."
static PyObject *
psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
{
const char *procname = NULL;
char *sql = NULL;
long int async;
Py_ssize_t procname_len, i, nparameters = 0, sl = 0;
PyObject *parameters = Py_None;
PyObject *operation = NULL;
PyObject *res = NULL;
async = self->conn->async;
if (!PyArg_ParseTuple(args, "s#|Ol",
&procname, &procname_len, &parameters, &async
if (!PyArg_ParseTuple(args, "s#|O",
&procname, &procname_len, &parameters
))
{ return NULL; }
if (async != self->conn->async) {
if (async == 0)
psyco_set_error(ProgrammingError, (PyObject*)self,
"can't do a synchronous function call "
"from an asynchronous cursor",
NULL, NULL);
else
psyco_set_error(ProgrammingError, (PyObject*)self,
"can't do an asynchronous function call "
"from a synchronous cursor",
NULL, NULL);
return NULL;
}
EXC_IF_CURS_CLOSED(self);
if (self->name != NULL) {
@ -1021,7 +986,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
operation = PyString_FromString(sql);
PyMem_Free((void*)sql);
if (_psyco_curs_execute(self, operation, parameters, async)) {
if (_psyco_curs_execute(self, operation, parameters, self->conn->async)) {
Py_INCREF(parameters);
res = parameters;
}