Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 582b5ac

Browse files
committed
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
1 parent 418df3a commit 582b5ac

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

src/pl/plpython/expected/plpython_unicode_2.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ SELECT * FROM unicode_test;
3939
(0 rows)
4040

4141
SELECT unicode_plan1();
42-
WARNING: PL/Python: plpy.Error: unrecognized error in PLy_spi_execute_plan
42+
WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_plan
4343
CONTEXT: PL/Python function "unicode_plan1"
4444
ERROR: PL/Python: plpy.SPIError: PL/Python: could not convert Python Unicode object to PostgreSQL server encoding
4545
DETAIL: UnicodeError: ASCII encoding error: ordinal not in range(128)

src/pl/plpython/expected/plpython_unicode_3.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ SELECT * FROM unicode_test;
3939
(0 rows)
4040

4141
SELECT unicode_plan1();
42-
WARNING: PL/Python: plpy.Error: unrecognized error in PLy_spi_execute_plan
42+
WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_plan
4343
CONTEXT: PL/Python function "unicode_plan1"
4444
ERROR: PL/Python: plpy.SPIError: PL/Python: could not convert Python Unicode object to PostgreSQL server encoding
4545
DETAIL: UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)

src/pl/plpython/plpython.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -2820,15 +2820,11 @@ PLy_spi_prepare(PyObject *self, PyObject *args)
28202820
int nargs;
28212821

28222822
if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2823-
{
2824-
PLy_exception_set(PLy_exc_spi_error,
2825-
"invalid arguments for plpy.prepare");
28262823
return NULL;
2827-
}
28282824

28292825
if (list && (!PySequence_Check(list)))
28302826
{
2831-
PLy_exception_set(PLy_exc_spi_error,
2827+
PLy_exception_set(PyExc_TypeError,
28322828
"second argument of plpy.prepare must be a sequence");
28332829
return NULL;
28342830
}
@@ -2984,7 +2980,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
29842980
{
29852981
if (!PySequence_Check(list) || PyString_Check(list) || PyUnicode_Check(list))
29862982
{
2987-
PLy_exception_set(PLy_exc_spi_error, "plpy.execute takes a sequence as its second argument");
2983+
PLy_exception_set(PyExc_TypeError, "plpy.execute takes a sequence as its second argument");
29882984
return NULL;
29892985
}
29902986
nargs = PySequence_Length(list);
@@ -3002,7 +2998,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
30022998
if (!so)
30032999
PLy_elog(ERROR, "could not execute plan");
30043000
sv = PyString_AsString(so);
3005-
PLy_exception_set_plural(PLy_exc_spi_error,
3001+
PLy_exception_set_plural(PyExc_TypeError,
30063002
"Expected sequence of %d argument, got %d: %s",
30073003
"Expected sequence of %d arguments, got %d: %s",
30083004
plan->nargs,
@@ -3089,7 +3085,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
30893085
}
30903086

30913087
if (!PyErr_Occurred())
3092-
PLy_exception_set(PLy_exc_error,
3088+
PLy_exception_set(PLy_exc_spi_error,
30933089
"unrecognized error in PLy_spi_execute_plan");
30943090
PLy_elog(WARNING, NULL);
30953091
PLy_spi_exception_set(edata);

0 commit comments

Comments
 (0)