agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
PLPython and named arguments
4+ messages / 3 participants
[nested] [flat]

* PLPython and named arguments
@ 2016-10-04 12:20 Andrey Avakimov <aquarius1993@rambler.ru>
  2016-10-04 12:37 ` Re: PLPython and named arguments Tom Lane <tgl@sss.pgh.pa.us>
  2016-10-04 13:19 ` Re: PLPython and named arguments Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Andrey Avakimov @ 2016-10-04 12:20 UTC (permalink / raw)
  To: pgsql-sql

Hello

My question is about plpython behavior. In some cases procedure ignores named
arguments and raises an error like:

UnboundLocalError: local variable 'arg_from' referenced before assignment

This can be solved by using such code:

arg_from, arg_to = args

But the reasons of such behavior are still unclear.

Does anyone faced this thing?

I will gladly provide any further information if needed.

Best Regards,
Andrew

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PLPython and named arguments
  2016-10-04 12:20 PLPython and named arguments Andrey Avakimov <aquarius1993@rambler.ru>
@ 2016-10-04 12:37 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-10-04 12:37 UTC (permalink / raw)
  To: Andrey Avakimov <aquarius1993@rambler.ru>; +Cc: pgsql-sql

"Andrey Avakimov" <aquarius1993@rambler.ru> writes:
> My question is about plpython behavior. In some cases procedure ignores named
> arguments and raises an error like:
> UnboundLocalError: local variable 'arg_from' referenced before assignment

Let's see an example?

			regards, tom lane


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PLPython and named arguments
  2016-10-04 12:20 PLPython and named arguments Andrey Avakimov <aquarius1993@rambler.ru>
@ 2016-10-04 13:19 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2016-10-04 13:36   ` Re: PLPython and named arguments Andrey Avakimov <aquarius1993@rambler.ru>
  1 sibling, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2016-10-04 13:19 UTC (permalink / raw)
  To: Andrey Avakimov <aquarius1993@rambler.ru>; pgsql-sql

On 10/04/2016 05:20 AM, Andrey Avakimov wrote:
> Hello
>
> My question is about plpython behavior. In some cases procedure ignores
> named arguments and raises an error like:
>
> UnboundLocalError: local variable 'arg_from' referenced before assignment

Because of this?:

https://www.postgresql.org/docs/9.6/static/plpython-funcs.html
"
The arguments are set as global variables. Because of the scoping rules 
of Python, this has the subtle consequence that an argument variable 
cannot be reassigned inside the function to the value of an expression 
that involves the variable name itself, unless the variable is 
redeclared as global in the block. For example, the following won't work:

CREATE FUNCTION pystrip(x text)
   RETURNS text
AS $$
   x = x.strip()  # error
   return x
$$ LANGUAGE plpythonu;

because assigning to x makes x a local variable for the entire block, 
and so the x on the right-hand side of the assignment refers to a 
not-yet-assigned local variable x, not the PL/Python function parameter. 
Using the global statement, this can be made to work:

CREATE FUNCTION pystrip(x text)
   RETURNS text
AS $$
   global x
   x = x.strip()  # ok now
   return x
$$ LANGUAGE plpythonu;

But it is advisable not to rely on this implementation detail of 
PL/Python. It is better to treat the function parameters as read-only."


So:

test=# select * from pystrip('test  ');
ERROR:  UnboundLocalError: local variable 'x' referenced before assignment
CONTEXT:  Traceback (most recent call last):
   PL/Python function "pystrip", line 2, in <module>
     x = x.strip()  # error
PL/Python function "pystrip"



>
> This can be solved by using such code:
>
> arg_from, arg_to = args
>
> But the reasons of such behavior are still unclear.
>
> Does anyone faced this thing?
>
> I will gladly provide any further information if needed.
>
> Best Regards,
> Andrew
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PLPython and named arguments
  2016-10-04 12:20 PLPython and named arguments Andrey Avakimov <aquarius1993@rambler.ru>
  2016-10-04 13:19 ` Re: PLPython and named arguments Adrian Klaver <adrian.klaver@aklaver.com>
@ 2016-10-04 13:36   ` Andrey Avakimov <aquarius1993@rambler.ru>
  0 siblings, 0 replies; 4+ messages in thread

From: Andrey Avakimov @ 2016-10-04 13:36 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-sql


OMG... I feel ashamed that I missed that part of the documentation.
Thank you

Best Regards,
Andrew

> 04.10.2016, 16:22:55 пользователь Adrian Klaver (adrian.klaver@aklaver.com) написал:
> 
> On 10/04/2016 05:20 AM, Andrey Avakimov wrote:
> > Hello
> >
> > My question is about plpython behavior. In some cases procedure ignores
> > named arguments and raises an error like:
> >
> > UnboundLocalError: local variable 'arg_from' referenced before assignment
> 
> Because of this?:
> 
> https://www.postgresql.org/docs/9.6/static/plpython-funcs.html
> "
> The arguments are set as global variables. Because of the scoping rules 
> of Python, this has the subtle consequence that an argument variable 
> cannot be reassigned inside the function to the value of an expression 
> that involves the variable name itself, unless the variable is 
> redeclared as global in the block. For example, the following won't work:
> 
> CREATE FUNCTION pystrip(x text)
>       RETURNS text
> AS $$
>       x = x.strip()    # error
>       return x
> $$ LANGUAGE plpythonu;
> 
> because assigning to x makes x a local variable for the entire block, 
> and so the x on the right-hand side of the assignment refers to a 
> not-yet-assigned local variable x, not the PL/Python function parameter. 
> Using the global statement, this can be made to work:
> 
> CREATE FUNCTION pystrip(x text)
>       RETURNS text
> AS $$
>       global x
>       x = x.strip()    # ok now
>       return x
> $$ LANGUAGE plpythonu;
> 
> But it is advisable not to rely on this implementation detail of 
> PL/Python. It is better to treat the function parameters as read-only."
> 
> So:
> 
> test=# select * from pystrip('test    ');
> ERROR:    UnboundLocalError: local variable 'x' referenced before assignment
> CONTEXT:    Traceback (most recent call last):
>       PL/Python function "pystrip", line 2, in <module>
>           x = x.strip()    # error
> PL/Python function "pystrip"
> 
> >
> > This can be solved by using such code:
> >
> > arg_from, arg_to = args
> >
> > But the reasons of such behavior are still unclear.
> >
> > Does anyone faced this thing?
> >
> > I will gladly provide any further information if needed.
> >
> > Best Regards,
> > Andrew
> >
> 
> -- 
> Adrian Klaver
> adrian.klaver@aklaver.com
> 
> -- 
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
> 
> 

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2016-10-04 13:36 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-10-04 12:20 PLPython and named arguments Andrey Avakimov <aquarius1993@rambler.ru>
2016-10-04 12:37 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-10-04 13:19 ` Adrian Klaver <adrian.klaver@aklaver.com>
2016-10-04 13:36   ` Andrey Avakimov <aquarius1993@rambler.ru>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox