Re: BUG #5232: plpythonu s=s.op() raises an exception

From: David Gardner <dgardner(at)creatureshop(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #5232: plpythonu s=s.op() raises an exception
Date: 2009-12-03 22:46:31
Message-ID: 4B183FC7.5070403@creatureshop.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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" <dgardner(at)creatureshop(dot)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
dgardner(at)creatureshop(dot)com

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Peter Eisentraut 2009-12-03 23:57:10 Re: BUG #5232: plpythonu s=s.op() raises an exception
Previous Message Tom Lane 2009-12-03 22:30:38 Re: BUG #5232: plpythonu s=s.op() raises an exception