>From c56e62e3a9961e589ae3f5efdd9bf06b6b21e917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Urba=C5=84ski?= Date: Sun, 2 Jan 2011 11:39:26 +0100 Subject: [PATCH 14/16] Improve exception usage in PL/Python. Specifically, use the builtin ValueError, not SPIError for errors having to do with argument counts or types. Use SPIError, not simply plpy.Error for errors in PLy_spi_execute_plan. Finally, do not set a Python exception if PyArg_ParseTuple failed, as it arleady sets the correct exception. --- src/pl/plpython/plpython.c | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c index 38820c7..f4942a4 100644 --- a/src/pl/plpython/plpython.c +++ b/src/pl/plpython/plpython.c @@ -2831,14 +2831,12 @@ PLy_spi_prepare(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "s|O", &query, &list)) { - PLy_exception_set(PLy_exc_spi_error, - "invalid arguments for plpy.prepare"); return NULL; } if (list && (!PySequence_Check(list))) { - PLy_exception_set(PLy_exc_spi_error, + PLy_exception_set(PyExc_ValueError, "second argument of plpy.prepare must be a sequence"); return NULL; } @@ -2991,7 +2989,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) { if (!PySequence_Check(list) || PyString_Check(list) || PyUnicode_Check(list)) { - PLy_exception_set(PLy_exc_spi_error, "plpy.execute takes a sequence as its second argument"); + PLy_exception_set(PyExc_ValueError, "plpy.execute takes a sequence as its second argument"); return NULL; } nargs = PySequence_Length(list); @@ -3009,9 +3007,9 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) if (so == NULL) PLy_elog(ERROR, "could not execute plan"); sv = PyString_AsString(so); - PLy_exception_set_plural(PLy_exc_spi_error, - "Expected sequence of %d argument, got %d: %s", - "Expected sequence of %d arguments, got %d: %s", + PLy_exception_set_plural(PyExc_ValueError, + "Expected sequence of %d argument, got %d: %s", + "Expected sequence of %d arguments, got %d: %s", plan->nargs, plan->nargs, nargs, sv); Py_DECREF(so); @@ -3096,7 +3094,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) } if (!PyErr_Occurred()) - PLy_exception_set(PLy_exc_error, + PLy_exception_set(PLy_exc_spi_error, "unrecognized error in PLy_spi_execute_plan"); PLy_elog(WARNING, NULL); PLy_exception_set(PLy_exc_spi_error, edata->message); -- 1.7.2.3