Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aVrG5-0004Nd-L9 for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Feb 2016 01:50:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aVrG4-0007OT-Ad for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Feb 2016 01:50:24 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aVrFP-0006dU-VF for pgsql-bugs@postgresql.org; Wed, 17 Feb 2016 01:49:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aVrFN-00075z-FH for pgsql-bugs@postgresql.org; Wed, 17 Feb 2016 01:49:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u1H1ncwt025530; Tue, 16 Feb 2016 20:49:38 -0500 From: Tom Lane To: Jim Nasby cc: pgsql-bugs@postgresql.org Subject: Re: BUG #13960: plpython fails with certain function names In-reply-to: <56C3CB9A.4030009@BlueTreble.com> References: <20160215001447.2605.36302@wrigleys.postgresql.org> <3868.1455497399@sss.pgh.pa.us> <56C12241.2030107@BlueTreble.com> <4535.1455498553@sss.pgh.pa.us> <56C3CB9A.4030009@BlueTreble.com> Comments: In-reply-to Jim Nasby message dated "Tue, 16 Feb 2016 19:23:38 -0600" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <25495.1455673715.0@sss.pgh.pa.us> Date: Tue, 16 Feb 2016 20:49:38 -0500 Message-ID: <25529.1455673778@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <25495.1455673715.1@sss.pgh.pa.us> Jim Nasby writes: > On 2/14/16 7:09 PM, Tom Lane wrote: >> Yeah, that's what I was thinking about. But yes, if we append the OID >> anyway then we might as well just strip all non-alphanumeric chars >> from the name. Safe and you still get some debuggability. > Attached. I opted not to modify the name in-place. If it's safe to > modify in place, might want to just replace invalid characters with '_' > instead of making a copy. I like the idea of replacing invalid characters with '_'. It's definitely not safe to scribble on the pg_proc tuple, but we could get the same result with a few wasted cycles by rescanning the procName string after building it, as per attached. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="plpython_name_2.patch"; charset="us-ascii" Content-ID: <25495.1455673715.2@sss.pgh.pa.us> Content-Description: plpython_name_2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/pl/plpython/expected/plpython_test.out b/src/pl/plpython/e= xpected/plpython_test.out index 7b76faf..f8270a7 100644 *** a/src/pl/plpython/expected/plpython_test.out --- b/src/pl/plpython/expected/plpython_test.out *************** select stupidn(); *** 16,23 **** zarkon (1 row) =20=20 ! -- test multiple arguments ! CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text AS 'keys =3D list(u.keys()) keys.sort() --- 16,23 ---- zarkon (1 row) =20=20 ! -- test multiple arguments and odd characters in function name ! CREATE FUNCTION "Argument test #1"(u users, a1 text, a2 text) RETURNS text AS 'keys =3D list(u.keys()) keys.sort() *************** for key in keys: *** 27,34 **** words =3D a1 + " " + a2 + " =3D> {" + ", ".join(out) + "}" return words' LANGUAGE plpythonu; ! select argument_test_one(users, fname, lname) from users where lname =3D = 'doe' order by 1; ! argument_test_one=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ----------------------------------------------------------------------- jane doe =3D> {fname: jane, lname: doe, userid: 1, username: j_doe} john doe =3D> {fname: john, lname: doe, userid: 2, username: johnd} --- 27,34 ---- words =3D a1 + " " + a2 + " =3D> {" + ", ".join(out) + "}" return words' LANGUAGE plpythonu; ! select "Argument test #1"(users, fname, lname) from users where lname =3D= 'doe' order by 1; ! Argument test #1=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ----------------------------------------------------------------------- jane doe =3D> {fname: jane, lname: doe, userid: 1, username: j_doe} john doe =3D> {fname: john, lname: doe, userid: 2, username: johnd} diff --git a/src/pl/plpython/plpy_procedure.c b/src/pl/plpython/plpy_proced= ure.c index e1f5620..a0d0792 100644 *** a/src/pl/plpython/plpy_procedure.c --- b/src/pl/plpython/plpy_procedure.c *************** PLy_procedure_create(HeapTuple procTup,=20 *** 146,151 **** --- 146,152 ---- MemoryContext cxt; MemoryContext oldcxt; int rv; + char *ptr; =20=20 procStruct =3D (Form_pg_proc) GETSTRUCT(procTup); rv =3D snprintf(procName, sizeof(procName), *************** PLy_procedure_create(HeapTuple procTup,=20 *** 155,160 **** --- 156,170 ---- if (rv >=3D sizeof(procName) || rv < 0) elog(ERROR, "procedure name would overrun buffer"); =20=20 + /* Replace any not-legal-in-Python-names characters with '_' */ + for (ptr =3D procName; *ptr; ptr++) + { + if (!((*ptr >=3D 'A' && *ptr <=3D 'Z') || + (*ptr >=3D 'a' && *ptr <=3D 'z') || + (*ptr >=3D '0' && *ptr <=3D '9'))) + *ptr =3D '_'; + } +=20 cxt =3D AllocSetContextCreate(TopMemoryContext, procName, ALLOCSET_DEFAULT_MINSIZE, diff --git a/src/pl/plpython/sql/plpython_test.sql b/src/pl/plpython/sql/pl= python_test.sql index c8d5ef5..3a76104 100644 *** a/src/pl/plpython/sql/plpython_test.sql --- b/src/pl/plpython/sql/plpython_test.sql *************** CREATE FUNCTION stupidn() RETURNS text A *** 11,18 **** =20=20 select stupidn(); =20=20 ! -- test multiple arguments ! CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text AS 'keys =3D list(u.keys()) keys.sort() --- 11,18 ---- =20=20 select stupidn(); =20=20 ! -- test multiple arguments and odd characters in function name ! CREATE FUNCTION "Argument test #1"(u users, a1 text, a2 text) RETURNS text AS 'keys =3D list(u.keys()) keys.sort() *************** words =3D a1 + " " + a2 + " =3D> {" + ", ".j *** 23,29 **** return words' LANGUAGE plpythonu; =20=20 ! select argument_test_one(users, fname, lname) from users where lname =3D = 'doe' order by 1; =20=20 =20=20 -- check module contents --- 23,29 ---- return words' LANGUAGE plpythonu; =20=20 ! select "Argument test #1"(users, fname, lname) from users where lname =3D= 'doe' order by 1; =20=20 =20=20 -- check module contents ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--