Bug reference: 5232
Logged by: David Gardner
Email address: dgar...@creatureshop.com
PostgreSQL version: 8.4.1
Operating system: Debian, amd64
Description: plpythonu s=s.op() raises an exception
Details:
If I create the following:
CREATE OR REPLACE FUNCTION pyreplace(src text,s text)
RETURNS text AS
$BODY$
try:
src=src.replace(s,'')
return src
except Exception,e:
return str(e)
$BODY$
LANGUAGE 'plpythonu' VOLATILE
COST 100;
ALTER FUNCTION pyreplace(src text,s text) OWNER TO dgardner;
Then:
SELECT * FROM pyreplace('this is a very long string','is');
pyreplace
---------------------------------------------------
local variable 'src' referenced before assignment
(1 row)
However in python I can do:
def pyreplace(src,s):
try:
src=src.replace(s,'')
return src
except Exception,e:
return str(e)
pyreplace('this is a very long string','is')
-----
produces:
'th a very long string'
--
Sent via pgsql-bugs mailing list (pgsql...@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Weird. You seem to need both the try block and the overwrite of the
parameter to make it misbehave. I suspect this means we're doing
something a bit wrong in setting up the python variable for the
parameter. Unfortunately I don't know enough about python to go further
than that.
regards, tom lane
"David Gardner" <dgar...@creatureshop.com> writes:CREATE OR REPLACE FUNCTION pyreplace(src text,s text) RETURNS text AS $BODY$ try: src=src.replace(s,'') return src except Exception,e: return str(e) $BODY$ LANGUAGE 'plpythonu' VOLATILE COST 100;Weird. You seem to need both the try block and the overwrite of the parameter to make it misbehave. I suspect this means we're doing something a bit wrong in setting up the python variable for the parameter. Unfortunately I don't know enough about python to go further than that. regards, tom lane
-- David Gardner Pipeline Tools Programmer Jim Henson Creature Shop dgar...@creatureshop.com
What is going on internally is something like this:
src = 'xyz'
def pyreplacenotry():
src=src.replace('x', 'y')
return src
pyreplacenotry()
which fails with that same error. So you should not try to assign to
the parameters of a function.
If it's allowed in normal Python functions, that definitely needs to be
documented. Better would be to fix it or at least throw a more
intelligible error ...
regards, tom lane
I have added documentation for it.