Skip to content

Commit 2ce19f8

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 6ce8236 commit 2ce19f8

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/pl/plpython/expected/plpython_test.out

Lines changed: 4 additions & 4 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
147147
bool isnull;
148148
int i,
149149
rv;
150+
char *ptr;
150151

151152
procStruct = (Form_pg_proc) GETSTRUCT(procTup);
152153
rv = snprintf(procName, sizeof(procName),
@@ -156,6 +157,15 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
156157
if (rv >= sizeof(procName) || rv < 0)
157158
elog(ERROR, "procedure name would overrun buffer");
158159

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

src/pl/plpython/sql/plpython_test.sql

Lines changed: 3 additions & 3 deletions
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)