From 37f11c308136b64f521abfb87bdebd3f9df7b9e9 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Sun, 10 Jul 2005 04:56:55 +0000 Subject: This patch addresses the problem mentioned in the "process crash when a plpython function returns unicode" thread: http://archives.postgresql.org/pgsql-bugs/2005-06/msg00105.php In several places PL/Python was calling PyObject_Str() and then PyString_AsString() without checking if the former had returned NULL to indicate an error. PyString_AsString() doesn't expect a NULL argument, so passing one causes a segmentation fault. This patch adds checks for NULL and raises errors via PLy_elog(), which prints details of the underlying Python exception. The patch also adds regression tests for these checks. All tests pass on my Solaris 9 box running HEAD and Python 2.4.1. In one place the patch doesn't call PLy_elog() because that could cause infinite recursion; see the comment I added. I'm not sure how to test that particular case or whether it's even possible to get an error there: the value that the code should check is the Python exception type, so I wonder if a NULL value "shouldn't happen." This patch converts NULL to "Unknown Exception" but I wonder if an Assert() would be appropriate. The patch is against HEAD but the same changes should be applied to earlier versions because they have the same problem. The patch might not apply cleanly against earlier versions -- will the committer take care of little differences or should I submit different versions of the patch? Michael Fuhr --- src/pl/plpython/sql/plpython_function.sql | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/pl/plpython/sql/plpython_function.sql') diff --git a/src/pl/plpython/sql/plpython_function.sql b/src/pl/plpython/sql/plpython_function.sql index 25ac388495a..e3ec2afe014 100644 --- a/src/pl/plpython/sql/plpython_function.sql +++ b/src/pl/plpython/sql/plpython_function.sql @@ -313,3 +313,31 @@ LANGUAGE plpythonu; CREATE OR REPLACE FUNCTION newline_crlf() RETURNS integer AS 'x = 100\r\ny = 23\r\nreturn x + y\r\n' LANGUAGE plpythonu; + +-- +-- Unicode error handling +-- + +CREATE FUNCTION unicode_return_error() RETURNS text AS ' +return u"\\x80" +' LANGUAGE plpythonu; + +CREATE FUNCTION unicode_trigger_error() RETURNS trigger AS ' +TD["new"]["testvalue"] = u"\\x80" +return "MODIFY" +' LANGUAGE plpythonu; + +CREATE TRIGGER unicode_test_bi BEFORE INSERT ON unicode_test + FOR EACH ROW EXECUTE PROCEDURE unicode_trigger_error(); + +CREATE FUNCTION unicode_plan_error1() RETURNS text AS ' +plan = plpy.prepare("SELECT $1 AS testvalue", ["text"]) +rv = plpy.execute(plan, [u"\\x80"], 1) +return rv[0]["testvalue"] +' LANGUAGE plpythonu; + +CREATE FUNCTION unicode_plan_error2() RETURNS text AS ' +plan = plpy.prepare("SELECT $1 AS testvalue1, $2 AS testvalue2", ["text", "text"]) +rv = plpy.execute(plan, u"\\x80", 1) +return rv[0]["testvalue1"] +' LANGUAGE plpythonu; -- cgit v1.2.3