From 7e25d8218bee860e043a73ed1c9d887ed9920855 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=ABtan=20de=20Menten?= Date: Fri, 2 Apr 2010 20:29:37 +0200 Subject: [PATCH] - made the C version of RowProxy accept any sequence for the row, instead of only tuples --- lib/sqlalchemy/cextension/resultproxy.c | 33 ++++++++++++------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/sqlalchemy/cextension/resultproxy.c b/lib/sqlalchemy/cextension/resultproxy.c index b530b65f7e..45149bebd5 100644 --- a/lib/sqlalchemy/cextension/resultproxy.c +++ b/lib/sqlalchemy/cextension/resultproxy.c @@ -69,8 +69,8 @@ BaseRowProxy_init(BaseRowProxy *self, PyObject *args, PyObject *kwds) Py_INCREF(parent); self->parent = parent; - if (!PyTuple_CheckExact(row)) { - PyErr_SetString(PyExc_TypeError, "row must be a tuple"); + if (!PySequence_Check(row)) { + PyErr_SetString(PyExc_TypeError, "row must be a sequence"); return -1; } Py_INCREF(row); @@ -148,13 +148,15 @@ BaseRowProxy_processvalues(PyObject *values, PyObject *processors, int astuple) { Py_ssize_t num_values, num_processors; PyObject **valueptr, **funcptr, **resultptr; - PyObject *func, *result, *processed_value; + PyObject *func, *result, *processed_value, *values_fastseq; num_values = Py_SIZE(values); num_processors = Py_SIZE(processors); if (num_values != num_processors) { - PyErr_SetString(PyExc_RuntimeError, - "number of values in row differ from number of column processors"); + PyErr_Format(PyExc_RuntimeError, + "number of values in row (%d) differ from number of column " + "processors (%d)", + num_values, num_processors); return NULL; } @@ -166,9 +168,11 @@ BaseRowProxy_processvalues(PyObject *values, PyObject *processors, int astuple) if (result == NULL) return NULL; - /* we don't need to use PySequence_Fast as long as values, processors and - * result are simple tuple or lists. */ - valueptr = PySequence_Fast_ITEMS(values); + values_fastseq = PySequence_Fast(values, "row must be a sequence"); + if (values_fastseq == NULL) + return NULL; + + valueptr = PySequence_Fast_ITEMS(values_fastseq); funcptr = PySequence_Fast_ITEMS(processors); resultptr = PySequence_Fast_ITEMS(result); while (--num_values >= 0) { @@ -177,6 +181,7 @@ BaseRowProxy_processvalues(PyObject *values, PyObject *processors, int astuple) processed_value = PyObject_CallFunctionObjArgs(func, *valueptr, NULL); if (processed_value == NULL) { + Py_DECREF(values_fastseq); Py_DECREF(result); return NULL; } @@ -189,6 +194,7 @@ BaseRowProxy_processvalues(PyObject *values, PyObject *processors, int astuple) funcptr++; resultptr++; } + Py_DECREF(values_fastseq); return result; } @@ -199,19 +205,12 @@ BaseRowProxy_values(BaseRowProxy *self) self->processors, 0); } -static PyTupleObject * -BaseRowProxy_tuplevalues(BaseRowProxy *self) -{ - return (PyTupleObject *)BaseRowProxy_processvalues(self->row, - self->processors, 1); -} - static PyObject * BaseRowProxy_iter(BaseRowProxy *self) { PyObject *values, *result; - values = (PyObject *)BaseRowProxy_tuplevalues(self); + values = BaseRowProxy_processvalues(self->row, self->processors, 1); if (values == NULL) return NULL; @@ -395,7 +394,7 @@ BaseRowProxy_setrow(BaseRowProxy *self, PyObject *value, void *closure) if (!PyTuple_CheckExact(value)) { PyErr_SetString(PyExc_TypeError, - "The 'row' attribute value must be a tuple"); + "The 'row' attribute value must be a sequence"); return -1; } -- 2.47.3