From 9b259a8a5489928f9abf12cf4c7459696ef18a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Urba=C5=84ski?= Date: Fri, 26 Mar 2010 04:00:47 +0100 Subject: [PATCH] Disallow some methods depending on the connection's sync/async mode --- psycopg/cursor_type.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 0609ca31..1fc88f93 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -442,16 +442,32 @@ _psyco_curs_execute(cursorObject *self, static PyObject * psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs) { - long int async = 0; + long int async; PyObject *vars = NULL, *operation = NULL; static char *kwlist[] = {"query", "vars", "async", 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); + return NULL; + } + if (self->name != NULL) { if (self->query != Py_None) { psyco_set_error(ProgrammingError, (PyObject*)self, @@ -510,6 +526,12 @@ psyco_curs_executemany(cursorObject *self, PyObject *args, PyObject *kwargs) return NULL; } + if (self->conn->async == 1) { + psyco_set_error(ProgrammingError, (PyObject*)self, + "can't call .executemany() on async cursors", NULL, NULL); + return NULL; + } + if (!PyIter_Check(vars)) { vars = iter = PyObject_GetIter(vars); if (iter == NULL) return NULL; @@ -943,17 +965,34 @@ psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs) { const char *procname = NULL; char *sql = NULL; - long int async = 0; + 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 )) { 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) {