From b5b5cc71a7ff5d4392eb9278bab747c71b850cc4 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Mon, 9 May 2005 06:52:30 +0000 Subject: [PATCH] Applied .executemany() iterator patch. --- ChangeLog | 5 +++++ psycopg/cursor_type.c | 17 ++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index b7dd37ac..ed78c2fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-05-09 Federico Di Gregorio + + * psycopg/cursor_type.c (psyco_curs_executemany): applied slightly + fixed patch from wrobell to allow iterators in .executemany(). + 2005-04-18 Federico Di Gregorio * MANIFEST.in: included debian directory. diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index cf73ccf2..83466d0e 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -395,9 +395,8 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs) static PyObject * psyco_curs_executemany(cursorObject *self, PyObject *args, PyObject *kwargs) { - int i; PyObject *operation = NULL, *vars = NULL; - + PyObject *v, *iter = NULL; static char *kwlist[] = {"query", "vars", NULL}; @@ -407,23 +406,27 @@ psyco_curs_executemany(cursorObject *self, PyObject *args, PyObject *kwargs) } EXC_IF_CURS_CLOSED(self); + + if (!PyIter_Check(vars)) { + vars = iter = PyObject_GetIter(vars); + if (iter == NULL) return NULL; + } - for (i = 0; i < PySequence_Size(vars); i++) { - PyObject *v = PySequence_GetItem(vars, i); - if (!v || _psyco_curs_execute(self, operation, v, 0) == 0) { - Py_XDECREF(v); + while ((v = PyIter_Next(vars)) != NULL) { + if (_psyco_curs_execute(self, operation, v, 0) == 0) { + Py_DECREF(v); return NULL; } else { Py_DECREF(v); } } + Py_XDECREF(iter); Py_INCREF(Py_None); return Py_None; } - #ifdef PSYCOPG_EXTENSIONS #define psyco_curs_mogrify_doc \