diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-02-16 21:08:15 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-02-16 21:08:15 -0500 |
commit | 2ce19f8a57f5f5eba3d86dc8457d2e992df5af8f (patch) | |
tree | 4ec8efde40642f6742e3b8a39fb816fbf4ef8cb2 | |
parent | 6ce8236ff40c8b5aeea93d2a155be1ef3210b8c4 (diff) | |
download | postgresql-2ce19f8a57f5f5eba3d86dc8457d2e992df5af8f.tar.gz postgresql-2ce19f8a57f5f5eba3d86dc8457d2e992df5af8f.zip |
Make plpython cope with funny characters in function names.
A function name that's double-quoted in SQL can contain almost any
characters, but we were using that name directly as part of the name
generated for the Python-level function, and Python doesn't like
anything that isn't pretty much a standard identifier. To fix,
replace anything that isn't an ASCII letter or digit with an underscore
in the generated name. This doesn't create any risk of duplicate Python
function names because we were already appending the function OID to
the generated name to ensure uniqueness. Per bug #13960 from Jim Nasby.
Patch by Jim Nasby, modified a bit by me. Back-patch to all
supported branches.
-rw-r--r-- | src/pl/plpython/expected/plpython_test.out | 8 | ||||
-rw-r--r-- | src/pl/plpython/plpy_procedure.c | 10 | ||||
-rw-r--r-- | src/pl/plpython/sql/plpython_test.sql | 6 |
3 files changed, 17 insertions, 7 deletions
diff --git a/src/pl/plpython/expected/plpython_test.out b/src/pl/plpython/expected/plpython_test.out index a884fc0e27f..f377e614ed3 100644 --- a/src/pl/plpython/expected/plpython_test.out +++ b/src/pl/plpython/expected/plpython_test.out @@ -16,8 +16,8 @@ select stupidn(); zarkon (1 row) --- test multiple arguments -CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text +-- test multiple arguments and odd characters in function name +CREATE FUNCTION "Argument test #1"(u users, a1 text, a2 text) RETURNS text AS 'keys = list(u.keys()) keys.sort() @@ -27,8 +27,8 @@ for key in keys: words = a1 + " " + a2 + " => {" + ", ".join(out) + "}" return words' LANGUAGE plpythonu; -select argument_test_one(users, fname, lname) from users where lname = 'doe' order by 1; - argument_test_one +select "Argument test #1"(users, fname, lname) from users where lname = 'doe' order by 1; + Argument test #1 ----------------------------------------------------------------------- jane doe => {fname: jane, lname: doe, userid: 1, username: j_doe} john doe => {fname: john, lname: doe, userid: 2, username: johnd} diff --git a/src/pl/plpython/plpy_procedure.c b/src/pl/plpython/plpy_procedure.c index 16ff84560bb..ed98b6f99ad 100644 --- a/src/pl/plpython/plpy_procedure.c +++ b/src/pl/plpython/plpy_procedure.c @@ -147,6 +147,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger) bool isnull; int i, rv; + char *ptr; procStruct = (Form_pg_proc) GETSTRUCT(procTup); rv = snprintf(procName, sizeof(procName), @@ -156,6 +157,15 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger) if (rv >= sizeof(procName) || rv < 0) elog(ERROR, "procedure name would overrun buffer"); + /* Replace any not-legal-in-Python-names characters with '_' */ + for (ptr = procName; *ptr; ptr++) + { + if (!((*ptr >= 'A' && *ptr <= 'Z') || + (*ptr >= 'a' && *ptr <= 'z') || + (*ptr >= '0' && *ptr <= '9'))) + *ptr = '_'; + } + proc = PLy_malloc(sizeof(PLyProcedure)); proc->proname = PLy_strdup(NameStr(procStruct->proname)); proc->pyname = PLy_strdup(procName); diff --git a/src/pl/plpython/sql/plpython_test.sql b/src/pl/plpython/sql/plpython_test.sql index c8d5ef5f534..3a761047a09 100644 --- a/src/pl/plpython/sql/plpython_test.sql +++ b/src/pl/plpython/sql/plpython_test.sql @@ -11,8 +11,8 @@ CREATE FUNCTION stupidn() RETURNS text AS 'return "zarkon"' LANGUAGE plpython2u; select stupidn(); --- test multiple arguments -CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text +-- test multiple arguments and odd characters in function name +CREATE FUNCTION "Argument test #1"(u users, a1 text, a2 text) RETURNS text AS 'keys = list(u.keys()) keys.sort() @@ -23,7 +23,7 @@ words = a1 + " " + a2 + " => {" + ", ".join(out) + "}" return words' LANGUAGE plpythonu; -select argument_test_one(users, fname, lname) from users where lname = 'doe' order by 1; +select "Argument test #1"(users, fname, lname) from users where lname = 'doe' order by 1; -- check module contents |