Call PLy_spi_execute_fetch_result inside the try/catch block

This way errors from fetching tuples are correctly reported as errors
in the SPI call.  While at it, avoid palloc(0).

Jan Urbański
This commit is contained in:
Peter Eisentraut 2011-01-25 00:43:25 +02:00
parent 52713d02c7
commit 88dcdf9007
1 changed files with 14 additions and 4 deletions

View File

@ -2978,6 +2978,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
rv; rv;
PLyPlanObject *plan; PLyPlanObject *plan;
volatile MemoryContext oldcontext; volatile MemoryContext oldcontext;
PyObject *ret;
if (list != NULL) if (list != NULL)
{ {
@ -3014,9 +3015,14 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
oldcontext = CurrentMemoryContext; oldcontext = CurrentMemoryContext;
PG_TRY(); PG_TRY();
{ {
char *nulls = palloc(nargs * sizeof(char)); char *nulls;
volatile int j; volatile int j;
if (nargs > 0)
nulls = palloc(nargs * sizeof(char));
else
nulls = NULL;
for (j = 0; j < nargs; j++) for (j = 0; j < nargs; j++)
{ {
PyObject *elem; PyObject *elem;
@ -3055,8 +3061,10 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
rv = SPI_execute_plan(plan->plan, plan->values, nulls, rv = SPI_execute_plan(plan->plan, plan->values, nulls,
PLy_curr_procedure->fn_readonly, limit); PLy_curr_procedure->fn_readonly, limit);
ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
pfree(nulls); if (nargs > 0)
pfree(nulls);
} }
PG_CATCH(); PG_CATCH();
{ {
@ -3099,7 +3107,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
} }
} }
return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv); return ret;
} }
static PyObject * static PyObject *
@ -3107,12 +3115,14 @@ PLy_spi_execute_query(char *query, long limit)
{ {
int rv; int rv;
volatile MemoryContext oldcontext; volatile MemoryContext oldcontext;
PyObject *ret;
oldcontext = CurrentMemoryContext; oldcontext = CurrentMemoryContext;
PG_TRY(); PG_TRY();
{ {
pg_verifymbstr(query, strlen(query), false); pg_verifymbstr(query, strlen(query), false);
rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit); rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
} }
PG_CATCH(); PG_CATCH();
{ {
@ -3138,7 +3148,7 @@ PLy_spi_execute_query(char *query, long limit)
return NULL; return NULL;
} }
return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv); return ret;
} }
static PyObject * static PyObject *