Re: BUG #5232: plpythonu s=s.op() raises an exception - Mailing list pgsql-bugs

From Peter Eisentraut
Subject Re: BUG #5232: plpythonu s=s.op() raises an exception
Date
Msg-id 1259884630.30348.3.camel@vanquo.pezone.net
Whole thread Raw
In response to Re: BUG #5232: plpythonu s=s.op() raises an exception  (David Gardner <dgardner@creatureshop.com>)
Responses Re: BUG #5232: plpythonu s=s.op() raises an exception  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
On tor, 2009-12-03 at 14:46 -0800, David Gardner wrote:
> 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

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.

pgsql-bugs by date:

Previous
From: David Gardner
Date:
Subject: Re: BUG #5232: plpythonu s=s.op() raises an exception
Next
From: Tom Lane
Date:
Subject: Re: BUG #5232: plpythonu s=s.op() raises an exception