Not sure about the try block being related, I included it in my example
mostly because the example is a simplified version of some code I was
working on that had a try/except block.
I tried the function without the try block and it raised the same
exception (just uncaught):
CREATE OR REPLACE FUNCTION pyreplacenotry(src text, s text)
RETURNS text AS
$BODY$
src=src.replace(s,'')
return src
$BODY$
LANGUAGE 'plpythonu' VOLATILE
COST 100;
ALTER FUNCTION pyreplacenotry(text, text) OWNER TO dgardner;
gives me:
ERROR: PL/Python: PL/Python function "pyreplacenotry" failed
DETAIL: <type 'exceptions.UnboundLocalError'>: local variable 'src'
referenced before assignment
********** Error **********
ERROR: PL/Python: PL/Python function "pyreplacenotry" failed
SQL state: XX000
Detail: <type 'exceptions.UnboundLocalError'>: local variable 'src'
referenced before assignment
However this works:
CREATE OR REPLACE FUNCTION pyreplacenoreassign(src text, s text)
RETURNS text AS
$BODY$
return src.replace(s,'')
$BODY$
LANGUAGE 'plpythonu' VOLATILE
COST 100;
ALTER FUNCTION pyreplacenoreassign(text, text) OWNER TO dgardner;
Tom Lane wrote:
"David Gardner" <dgard...@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
dgard...@creatureshop.com