Applied .executemany() iterator patch.

This commit is contained in:
Federico Di Gregorio 2005-05-09 06:52:30 +00:00
parent 431a2aec6c
commit b5b5cc71a7
2 changed files with 15 additions and 7 deletions

View File

@ -1,3 +1,8 @@
2005-05-09 Federico Di Gregorio <fog@debian.org>
* psycopg/cursor_type.c (psyco_curs_executemany): applied slightly
fixed patch from wrobell to allow iterators in .executemany().
2005-04-18 Federico Di Gregorio <fog@debian.org> 2005-04-18 Federico Di Gregorio <fog@debian.org>
* MANIFEST.in: included debian directory. * MANIFEST.in: included debian directory.

View File

@ -395,9 +395,8 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
static PyObject * static PyObject *
psyco_curs_executemany(cursorObject *self, PyObject *args, PyObject *kwargs) psyco_curs_executemany(cursorObject *self, PyObject *args, PyObject *kwargs)
{ {
int i;
PyObject *operation = NULL, *vars = NULL; PyObject *operation = NULL, *vars = NULL;
PyObject *v, *iter = NULL;
static char *kwlist[] = {"query", "vars", 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); 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++) { while ((v = PyIter_Next(vars)) != NULL) {
PyObject *v = PySequence_GetItem(vars, i); if (_psyco_curs_execute(self, operation, v, 0) == 0) {
if (!v || _psyco_curs_execute(self, operation, v, 0) == 0) { Py_DECREF(v);
Py_XDECREF(v);
return NULL; return NULL;
} }
else { else {
Py_DECREF(v); Py_DECREF(v);
} }
} }
Py_XDECREF(iter);
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
#ifdef PSYCOPG_EXTENSIONS #ifdef PSYCOPG_EXTENSIONS
#define psyco_curs_mogrify_doc \ #define psyco_curs_mogrify_doc \