8000 Make plpython cope with funny characters in function names. · prmdeveloper/postgres@875973f · GitHub
[go: up one dir, main page]

Skip to content

Commit 875973f

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 56f5e77 commit 875973f

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/plpython.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
16121612
bool isnull;
16131613
int i,
16141614
rv;
1615+
char *ptr;
16151616

16161617
procStruct = (Form_pg_proc) GETSTRUCT(procTup);
16171618
rv = snprintf(procName, sizeof(procName),
@@ -1621,6 +1622,15 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
16211622
if (rv >= sizeof(procName) || rv < 0)
16221623
elog(ERROR, "procedure name would overrun buffer");
16231624

1625+
/* Replace any not-legal-in-Python-names characters with '_' */
1626+
for (ptr = procName; *ptr; ptr++)
1627+
{
1628+
if (!((*ptr >= 'A' && *ptr <= 'Z') ||
1629+
(*ptr >= 'a' && *ptr <= 'z') ||
1630+
(*ptr >= '0' && *ptr <= '9')))
1631+
*ptr = '_';
1632+
}
1633+
16241634
proc = PLy_malloc(sizeof(PLyProcedure));
16251635
proc->proname = PLy_strdup(NameStr(procStruct->proname));
16261636
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)
0