[fpc-pascal] Executing python code from a pascal program

Leonardo M. Ram� martinrame at yahoo.com
Mon Nov 13 16:12:27 CET 2006


Try addign cdecl or stdcall at the end

procedure Py_Initialize;external name 'Py_Initialize'; cdecl; // or stdcall;
procedure Py_Finalize;external name 'Py_Finalize'; cdecl; // or stdcall;


--- Adrian Maier <adrian.maier at gmail.com> wrote:

> Hello,
> 
> I am trying to write a program that is embedding the python interpreter
> in order to execute python code.  I don't need to transfer values between
> pascal and python.
> 
> More specifically, I'm trying to convert to pascal the following:
> 
> #include <Python.h>
> int main(int argc, char *argv[])
> {
>   Py_Initialize();
>   PyRun_SimpleString("from time import time,ctime\n"
>                      "print 'Today is',ctime(time())\n");
>   Py_Finalize();
>   return 0;
> }
> 
> I have a minimal unit which includes the needed functions  (h2pas wasn't
> able to convert the Python.h  , and i only need those 3 functions anyway ) :
> 
> unit Python;
> interface
>   uses Strings;
>   {$LinkLib python2.4}
> {$IFDEF FPC}
> {$PACKRECORDS C}
> {$ENDIF}
> 
>   procedure Py_Initialize;external name 'Py_Initialize';
>   procedure Py_Finalize;external name 'Py_Finalize';
>   function PyRun_SimpleString(_para1:Pchar):longint; external name
> 'PyRun_SimpleString';
> implementation
> end.
> 
> The pascal program is :
> 
> var cmd: PChar;
> begin
>    cmd:=StrAlloc(256);
> 
>    Py_Initialize();
>    StrPCopy(cmd, 'print "Hello"');
>    PyRun_SimpleString(cmd);   { <--- the program crashes here :
> runtime error 216 }
>    Py_Finalize();
> end.
> 
> The problem is that the program crashes at execution with "Runtime
> error 216 at <a_memory_address> ".   The backtrace is :
> 
> (gdb) backtrace
> #0  0xb7e6c3fa in PyOS_Readline () from /usr/lib/libpython2.4.so.1.0
> #1  0xb7e6ca7b in PyTokenizer_FromFile () from /usr/lib/libpython2.4.so.1.0
> #2  0xb7e6cf5d in PyTokenizer_FromString () from /usr/lib/libpython2.4.so.1.0
> #3  0xb7e69fb3 in PyParser_ParseStringFlagsFilename ()
>    from /usr/lib/libpython2.4.so.1.0
> #4  0xb7e6a091 in PyParser_ParseStringFlags ()
>    from /usr/lib/libpython2.4.so.1.0
> #5  0xb7eeb408 in PyParser_SimpleParseStringFlags ()
>    from /usr/lib/libpython2.4.so.1.0
> #6  0xb7eeb8a1 in PyRun_StringFlags () from /usr/lib/libpython2.4.so.1.0
> #7  0xb7eecba3 in PyRun_SimpleStringFlags () from /usr/lib/libpython2.4.so.1.0
> #8  0xb7eecc21 in PyRun_SimpleString () from /usr/lib/libpython2.4.so.1.0
> #9  0x08048324 in main () at runpy.pas:16
> 
> What am i doing wrong ?  Any idea ?
> 
> 
> Thank you,
> Adrian Maier
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> 


Leonardo M. Ramé
http://leonardorame.blogspot.com


 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com



More information about the fpc-pascal mailing list