From 01799e9137077e56220425faa245fdf5ac3a30e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Urba=C5=84ski?= Date: Wed, 31 Mar 2010 01:43:07 +0200 Subject: [PATCH] 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. --- lib/extras.py | 16 ++++++------- psycopg/cursor_type.c | 53 ++++++++----------------------------------- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/lib/extras.py b/lib/extras.py index 87c9f7ce..027c587b 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -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() diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 8e264b3e..358eb232 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -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, ¶meters, &async + if (!PyArg_ParseTuple(args, "s#|O", + &procname, &procname_len, ¶meters )) { 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; }