Re: plpython function problem workaround - Mailing list pgsql-general

From Marco Colombo
Subject Re: plpython function problem workaround
Date
Msg-id Pine.LNX.4.61.0503142008510.20758@Megathlon.ESI
Whole thread Raw
In response to Re: plpython function problem workaround  (Michael Fuhr <mike@fuhr.org>)
Responses Re: plpython function problem workaround  (Michael Fuhr <mike@fuhr.org>)
List pgsql-general
On Mon, 14 Mar 2005, Michael Fuhr wrote:

> Would we?  My understanding is that code passed to PyRun_String()
> and friends must be free of line-ending CRs on all platforms, and
> that the code that reads a "normal" Python script takes care of
> that (i.e., normalizes line endings to be LF only).  Can anybody
> confirm or deny?

I'm not sure of that. I suspect you'll need to pass CRs on windows.

If anyone manages to compile the following code on Windows...

#include "Python.h"

void
run_program(const char *program)
{
     PyObject *ret, *globals, *locals;

     printf("> running:\n%s\n", program);
     globals = PyDict_New();
     locals = PyDict_New();
     ret = PyRun_String(program, Py_file_input, globals, locals);
     if (ret) {
         Py_DECREF(ret);
         printf("\n");
     } else {
         PyErr_Print();
     }
     Py_DECREF(locals);
     Py_DECREF(globals);
     printf("> end\n\n");

}

int
main(int argc, char *argv[])
{
     const char *program1 = "print 1\nprint 2\n";
     const char *program2 = "print 1\r\nprint 2\r\n";

     Py_Initialize();
     printf("> Initialized.\n");
     printf("> Python %s\n", Py_GetVersion());
     run_program(program1);
     run_program(program2);
     Py_Finalize();
     printf("> Finalized.\n");
}

On my Fedora Core 2, I need to complile it with the following command:

gcc -I/usr/include/python2.3 -L/usr/lib/python2.3/config py-test.c -o py-test\
-lpython2.3 -ldl -lm -lpthread -lutil

This is my first attempt to embed python, so I may be missing something...

On Linux, you get:

$ ./py-test 2>&1 | cat -v
> Initialized.
> Python 2.3.3 (#1, May  7 2004, 10:31:40)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)]
> running:
print 1
print 2

1
2

> end

> running:
print 1^M
print 2^M

   File "<string>", line 1
     print 1^M
            ^
SyntaxError: invalid syntax
> end

> Finalized.


I bet on windows the first program fails and the second is ok.

.TM.
--
       ____/  ____/   /
      /      /       /            Marco Colombo
     ___/  ___  /   /              Technical Manager
    /          /   /             ESI s.r.l.
  _____/ _____/  _/               Colombo@ESI.it

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: prelimiary performance comparison pgsql vs mysql
Next
From: Robert Treat
Date:
Subject: Re: PostgreSQL training