Commit 3a4a33ad authored by Peter Eisentraut's avatar Peter Eisentraut

PL/Python: Report argument parsing errors using exceptions

Instead of calling PLy_elog() for reporting Python argument parsing
errors, generate appropriate exceptions.  This matches the existing plpy
functions and is more consistent with the behavior of the Python
argument parsing routines.
parent 420c1661
...@@ -56,17 +56,29 @@ INFO: other types ...@@ -56,17 +56,29 @@ INFO: other types
DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
-- should fail -- should fail
DO $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu; DO $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
ERROR: invalid SQLSTATE code ERROR: ValueError: invalid SQLSTATE code
CONTEXT: PL/Python anonymous code block CONTEXT: Traceback (most recent call last):
PL/Python anonymous code block, line 1, in <module>
plpy.info('wrong sqlstate', sqlstate='54444A')
PL/Python anonymous code block
DO $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu; DO $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
ERROR: 'blabla' is an invalid keyword argument for this function ERROR: TypeError: 'blabla' is an invalid keyword argument for this function
CONTEXT: PL/Python anonymous code block CONTEXT: Traceback (most recent call last):
PL/Python anonymous code block, line 1, in <module>
plpy.info('unsupported argument', blabla='fooboo')
PL/Python anonymous code block
DO $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu; DO $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
ERROR: the message is already specified ERROR: TypeError: Argument 'message' given by name and position
CONTEXT: PL/Python anonymous code block CONTEXT: Traceback (most recent call last):
PL/Python anonymous code block, line 1, in <module>
plpy.info('first message', message='second message')
PL/Python anonymous code block
DO $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu; DO $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
ERROR: the message is already specified ERROR: TypeError: Argument 'message' given by name and position
CONTEXT: PL/Python anonymous code block CONTEXT: Traceback (most recent call last):
PL/Python anonymous code block, line 1, in <module>
plpy.info('first message', 'second message', message='third message')
PL/Python anonymous code block
-- raise exception in python, handle exception in plgsql -- raise exception in python, handle exception in plgsql
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL, CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
_sqlstate text DEFAULT NULL, _sqlstate text DEFAULT NULL,
......
...@@ -444,7 +444,10 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw) ...@@ -444,7 +444,10 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
{ {
/* the message should not be overwriten */ /* the message should not be overwriten */
if (PyTuple_Size(args) != 0) if (PyTuple_Size(args) != 0)
PLy_elog(ERROR, "the message is already specified"); {
PLy_exception_set(PyExc_TypeError, "Argument 'message' given by name and position");
return NULL;
}
if (message) if (message)
pfree(message); pfree(message);
...@@ -467,18 +470,28 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw) ...@@ -467,18 +470,28 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
else if (strcmp(keyword, "constraint_name") == 0) else if (strcmp(keyword, "constraint_name") == 0)
constraint_name = object_to_string(value); constraint_name = object_to_string(value);
else else
PLy_elog(ERROR, "'%s' is an invalid keyword argument for this function", {
keyword); PLy_exception_set(PyExc_TypeError,
"'%s' is an invalid keyword argument for this function",
keyword);
return NULL;
}
} }
} }
if (sqlstatestr != NULL) if (sqlstatestr != NULL)
{ {
if (strlen(sqlstatestr) != 5) if (strlen(sqlstatestr) != 5)
PLy_elog(ERROR, "invalid SQLSTATE code"); {
PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
return NULL;
}
if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5) if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
PLy_elog(ERROR, "invalid SQLSTATE code"); {
PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
return NULL;
}
sqlstate = MAKE_SQLSTATE(sqlstatestr[0], sqlstate = MAKE_SQLSTATE(sqlstatestr[0],
sqlstatestr[1], sqlstatestr[1],
......
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