Skip to content

Commit f461fa7

Browse files
committed
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.
1 parent da5cf6a commit f461fa7

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/pl/plpython/expected/plpython_test.out

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ select stupidn();
1616
zarkon
1717
(1 row)
1818

19-
-- test multiple arguments
20-
CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
19+
-- test multiple arguments and odd characters in function name
20+
CREATE FUNCTION "Argument test #1"(u users, a1 text, a2 text) RETURNS text
2121
AS
2222
'keys = list(u.keys())
2323
keys.sort()
@@ -27,8 +27,8 @@ for key in keys:
2727
words = a1 + " " + a2 + " => {" + ", ".join(out) + "}"
2828
return words'
2929
LANGUAGE plpythonu;
30-
select argument_test_one(users, fname, lname) from users where lname = 'doe' order by 1;
31-
argument_test_one
30+
select "Argument test #1"(users, fname, lname) from users where lname = 'doe' order by 1;
31+
Argument test #1
3232
-----------------------------------------------------------------------
3333
jane doe => {fname: jane, lname: doe, userid: 1, username: j_doe}
3434
john doe => {fname: john, lname: doe, userid: 2, username: johnd}

src/pl/plpython/plpy_procedure.c

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
145145
bool isnull;
146146
int i,
147147
rv;
148+
char *ptr;
148149

149150
procStruct = (Form_pg_proc) GETSTRUCT(procTup);
150151
rv = snprintf(procName, sizeof(procName),
@@ -154,6 +155,15 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
154155
if (rv >= sizeof(procName) || rv < 0)
155156
elog(ERROR, "procedure name would overrun buffer");
156157

158+
/* Replace any not-legal-in-Python-names characters with '_' */
159+
for (ptr = procName; *ptr; ptr++)
160+
{
161+
if (!((*ptr >= 'A' && *ptr <= 'Z') ||
162+
(*ptr >= 'a' && *ptr <= 'z') ||
163+
(*ptr >= '0' && *ptr <= '9')))
164+
*ptr = '_';
165+
}
166+
157167
proc = PLy_malloc(sizeof(PLyProcedure));
158168
proc->proname = PLy_strdup(NameStr(procStruct->proname));
159169
proc->pyname = PLy_strdup(procName);

src/pl/plpython/sql/plpython_test.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ CREATE FUNCTION stupidn() RETURNS text AS 'return "zarkon"' LANGUAGE plpython2u;
1111

1212
select stupidn();
1313

14-
-- test multiple arguments
15-
CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
14+
-- test multiple arguments and odd characters in function name
15+
CREATE FUNCTION "Argument test #1"(u users, a1 text, a2 text) RETURNS text
1616
AS
1717
'keys = list(u.keys())
1818
keys.sort()
@@ -23,7 +23,7 @@ words = a1 + " " + a2 + " => {" + ", ".join(out) + "}"
2323
return words'
2424
LANGUAGE plpythonu;
2525

26-
select argument_test_one(users, fname, lname) from users where lname = 'doe' order by 1;
26+
select "Argument test #1"(users, fname, lname) from users where lname = 'doe' order by 1;
2727

2828

2929
-- check module contents

0 commit comments

Comments
 (0)