pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Andrey Avakimov <aquarius1993@rambler.ru>
To: Pgsql Sql <pgsql-sql@postgresql.org>
Subject: Re: PLPython and named arguments
Date: Tue, 4 Oct 2016 06:19:45 -0700
Message-ID: <ff3905f6-1954-973c-8314-6a3645299446@aklaver.com> (raw)
In-Reply-To: <1475583602.246825.24420.25624@mail.rambler.ru>
References: <1475583602.246825.24420.25624@mail.rambler.ru>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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



view thread (4+ messages)  latest in thread

Message-ID: <ff3905f6-1954-973c-8314-6a3645299446@aklaver.com>
Permalink:  ../ff3905f6-1954-973c-8314-6a3645299446@aklaver.com/
Also on:    postgresql.org/message-id/ff3905f6-1954-973c-8314-6a3645299446@aklaver.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: adrian.klaver@aklaver.com, aquarius1993@rambler.ru
  Subject: Re: PLPython and named arguments
  In-Reply-To: <ff3905f6-1954-973c-8314-6a3645299446@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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