aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Eisentraut <peter_e@gmx.net>2009-12-19 22:23:21 +0000
committerPeter Eisentraut <peter_e@gmx.net>2009-12-19 22:23:21 +0000
commit8f649c9ac45fbdff06efce84eb8cca17b29e9d3a (patch)
tree3e1c2fba3f336bb7b26e774ad6d8c56d6455ac5e
parent16dc6f0cf2474d36166cae77d239f85ccbf88880 (diff)
downloadpostgresql-8f649c9ac45fbdff06efce84eb8cca17b29e9d3a.tar.gz
postgresql-8f649c9ac45fbdff06efce84eb8cca17b29e9d3a.zip
Add documentation why reassigning PL/Python function parameters in the
function body can have undesirable outcomes. (bug #5232)
-rw-r--r--doc/src/sgml/plpython.sgml38
1 files changed, 37 insertions, 1 deletions
diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml
index 01feab8ec1a..a623f3509bd 100644
--- a/doc/src/sgml/plpython.sgml
+++ b/doc/src/sgml/plpython.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.42 2009/12/15 22:59:53 petere Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.43 2009/12/19 22:23:21 petere Exp $ -->
<chapter id="plpython">
<title>PL/Python - Python Procedural Language</title>
@@ -214,6 +214,42 @@ def __plpython_procedure_pymax_23456():
</para>
<para>
+ 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:
+<programlisting>
+CREATE FUNCTION pystrip(x text)
+ RETURNS text
+AS $$
+ x = x.strip() # error
+ return x
+$$ LANGUAGE plpythonu;
+</programlisting>
+ because assigning to <varname>x</varname>
+ makes <varname>x</varname> a local variable for the entire block,
+ and so the <varname>x</varname> on the right-hand side of the
+ assignment refers to a not-yet-assigned local
+ variable <varname>x</varname>, not the PL/Python function
+ parameter. Using the <literal>global</literal> statement, this can
+ be made to work:
+<programlisting>
+CREATE FUNCTION pystrip(x text)
+ RETURNS text
+AS $$
+ global x
+ x = x.strip() # ok now
+ return x
+$$ LANGUAGE plpythonu;
+</programlisting>
+ 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.
+ </para>
+
+ <para>
If an SQL null value<indexterm><primary>null value</primary><secondary
sortas="PL/Python">PL/Python</secondary></indexterm> is passed to a
function, the argument value will appear as <symbol>None</symbol> in