Commit 582b5ac6 authored by Peter Eisentraut's avatar Peter Eisentraut

Improve exception usage in PL/Python

Use the built-in TypeError, 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 already sets the correct
exception.

Jan Urbański
parent 418df3a5
...@@ -39,7 +39,7 @@ SELECT * FROM unicode_test; ...@@ -39,7 +39,7 @@ SELECT * FROM unicode_test;
(0 rows) (0 rows)
SELECT unicode_plan1(); SELECT unicode_plan1();
WARNING: PL/Python: plpy.Error: unrecognized error in PLy_spi_execute_plan WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_plan
CONTEXT: PL/Python function "unicode_plan1" CONTEXT: PL/Python function "unicode_plan1"
ERROR: PL/Python: plpy.SPIError: PL/Python: could not convert Python Unicode object to PostgreSQL server encoding ERROR: PL/Python: plpy.SPIError: PL/Python: could not convert Python Unicode object to PostgreSQL server encoding
DETAIL: UnicodeError: ASCII encoding error: ordinal not in range(128) DETAIL: UnicodeError: ASCII encoding error: ordinal not in range(128)
......
...@@ -39,7 +39,7 @@ SELECT * FROM unicode_test; ...@@ -39,7 +39,7 @@ SELECT * FROM unicode_test;
(0 rows) (0 rows)
SELECT unicode_plan1(); SELECT unicode_plan1();
WARNING: PL/Python: plpy.Error: unrecognized error in PLy_spi_execute_plan WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_plan
CONTEXT: PL/Python function "unicode_plan1" CONTEXT: PL/Python function "unicode_plan1"
ERROR: PL/Python: plpy.SPIError: PL/Python: could not convert Python Unicode object to PostgreSQL server encoding ERROR: PL/Python: plpy.SPIError: PL/Python: could not convert Python Unicode object to PostgreSQL server encoding
DETAIL: UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128) DETAIL: UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
......
...@@ -2820,15 +2820,11 @@ PLy_spi_prepare(PyObject *self, PyObject *args) ...@@ -2820,15 +2820,11 @@ PLy_spi_prepare(PyObject *self, PyObject *args)
int nargs; int nargs;
if (!PyArg_ParseTuple(args, "s|O", &query, &list)) if (!PyArg_ParseTuple(args, "s|O", &query, &list))
{
PLy_exception_set(PLy_exc_spi_error,
"invalid arguments for plpy.prepare");
return NULL; return NULL;
}
if (list && (!PySequence_Check(list))) if (list && (!PySequence_Check(list)))
{ {
PLy_exception_set(PLy_exc_spi_error, PLy_exception_set(PyExc_TypeError,
"second argument of plpy.prepare must be a sequence"); "second argument of plpy.prepare must be a sequence");
return NULL; return NULL;
} }
...@@ -2984,7 +2980,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -2984,7 +2980,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
{ {
if (!PySequence_Check(list) || PyString_Check(list) || PyUnicode_Check(list)) 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_TypeError, "plpy.execute takes a sequence as its second argument");
return NULL; return NULL;
} }
nargs = PySequence_Length(list); nargs = PySequence_Length(list);
...@@ -3002,7 +2998,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -3002,7 +2998,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
if (!so) if (!so)
PLy_elog(ERROR, "could not execute plan"); PLy_elog(ERROR, "could not execute plan");
sv = PyString_AsString(so); sv = PyString_AsString(so);
PLy_exception_set_plural(PLy_exc_spi_error, PLy_exception_set_plural(PyExc_TypeError,
"Expected sequence of %d argument, got %d: %s", "Expected sequence of %d argument, got %d: %s",
"Expected sequence of %d arguments, got %d: %s", "Expected sequence of %d arguments, got %d: %s",
plan->nargs, plan->nargs,
...@@ -3089,7 +3085,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -3089,7 +3085,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
} }
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PLy_exception_set(PLy_exc_error, PLy_exception_set(PLy_exc_spi_error,
"unrecognized error in PLy_spi_execute_plan"); "unrecognized error in PLy_spi_execute_plan");
PLy_elog(WARNING, NULL); PLy_elog(WARNING, NULL);
PLy_spi_exception_set(edata); PLy_spi_exception_set(edata);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment