Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf461fa7

Browse files
committed
Make plpython cope with funny characters in function names.
A function name that's double-quoted in SQL can contain almost anycharacters, but we were using that name directly as part of the namegenerated for the Python-level function, and Python doesn't likeanything that isn't pretty much a standard identifier. To fix,replace anything that isn't an ASCII letter or digit with an underscorein the generated name. This doesn't create any risk of duplicate Pythonfunction names because we were already appending the function OID tothe generated name to ensure uniqueness. Per bug #13960 from Jim Nasby.Patch by Jim Nasby, modified a bit by me. Back-patch to allsupported branches.
1 parentda5cf6a commitf461fa7

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 FUNCTIONargument_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-
selectargument_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
@@ -145,6 +145,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
145145
boolisnull;
146146
inti,
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

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-
CREATEFUNCTIONargument_test_one(u users, a1text, a2text) RETURNStext
14+
-- test multiple arguments and odd characters in function name
15+
CREATE FUNCTION"Argument test #1"(u users, a1text, a2text) RETURNStext
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-
selectargument_test_one(users, fname, lname)from userswhere lname='doe'order by1;
26+
select"Argument test #1"(users, fname, lname)from userswhere lname='doe'order by1;
2727

2828

2929
-- check module contents

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp