The attached patch mimics Universal Newline Support for PL/Python
functions.  For background, see PEP 278 and the recent "plpython
function problem workaround" thread in pgsql-general:

http://www.python.org/peps/pep-0278.html
http://archives.postgresql.org/pgsql-general/2005-03/msg00823.php

In short, embedded Python code must have lines that end in \n, not
\r\n or \r.  This patch adds logic to PLy_procedure_munge_source()
to skip \r followed by \n, otherwise to convert \r to \n.  I tested
it in HEAD but REL8_0_STABLE has the same version of plpython.c (1.58),
so it should work there as well.

Should the PL/Python documentation mention this behavior?  If so
then I can resubmit with a documentation patch.

How should I submit regression tests?  Here's what I did:

CREATE FUNCTION test_lf() RETURNS integer AS
'x=100\ny=23\nreturn x+y\n'
LANGUAGE plpythonu;

CREATE FUNCTION test_crlf() RETURNS integer AS
'x=100\r\ny=23\r\nreturn x+y\r\n'
LANGUAGE plpythonu;

CREATE FUNCTION test_cr() RETURNS integer AS
'x=100\ry=23\rreturn x+y\r'
LANGUAGE plpythonu;

Here's what an unpatched system does:

SELECT test_lf();
 test_lf 
---------
     123
(1 row)

SELECT test_cr();
ERROR:  plpython: could not compile function "test_cr"
DETAIL:  exceptions.SyntaxError: invalid syntax (line 2)

SELECT test_crlf();
ERROR:  plpython: could not compile function "test_crlf"
DETAIL:  exceptions.SyntaxError: invalid syntax (line 2)

Here's a patched system:

SELECT test_lf();
 test_lf 
---------
     123
(1 row)

SELECT test_cr();
 test_cr 
---------
     123
(1 row)

SELECT test_crlf();
 test_crlf 
-----------
       123
(1 row)

This patch doesn't address the indentation problem mentioned here:

http://archives.postgresql.org/pgsql-general/2005-03/msg00762.php

The solution to that problem is still being discussed.

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Index: src/pl/plpython/plpython.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/pl/plpython/plpython.c,v
retrieving revision 1.58
diff -c -r1.58 plpython.c
*** src/pl/plpython/plpython.c  17 Dec 2004 02:14:48 -0000      1.58
--- src/pl/plpython/plpython.c  19 Mar 2005 04:29:55 -0000
***************
*** 1206,1215 ****
  
        while (*sp != '\0')
        {
!               if (*sp == '\n')
                {
!                       *mp++ = *sp++;
                        *mp++ = '\t';
                }
                else
                        *mp++ = *sp++;
--- 1206,1219 ----
  
        while (*sp != '\0')
        {
!               if (*sp == '\r' && *(sp + 1) == '\n')
!                       sp++;
! 
!               if (*sp == '\n' || *sp == '\r')
                {
!                       *mp++ = '\n';
                        *mp++ = '\t';
+                       sp++;
                }
                else
                        *mp++ = *sp++;
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to