Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-11

Executing python code from a pascal program

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Adrian Maier

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

Reply by email

Leonardo M. Ram�
to Adrian Maier

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 wrote 76 lines
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 - 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

Reply by email

Leonardo M. Ram�
to Leonardo M. Ram�

I created a Python.pas unit this way using Delphi and it works correctly (note the cdecl directive before external):

unit python;
interface
  procedure Py_Initialize; cdecl; external 'python24.dll';
  procedure Py_Finalize; cdecl; external 'python24.dll';
  function PyRun_SimpleString(_para1:Pchar):longint; cdecl; external 'python24.dll';
implementation
end.

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

Reply by email

Adrian Maier
to Leonardo M. Ram�
Leonardo M. Ramé wrote 12 lines
I created a Python.pas unit this way using Delphi and it works correctly (note the cdecl directive before external): unit python; interface procedure Py_Initialize; cdecl; external 'python24.dll'; procedure Py_Finalize; cdecl; external 'python24.dll'; function PyRun_SimpleString(_para1:Pchar):longint; cdecl; external 'python24.dll'; implementation end.

Thanks Leonardo !

The trick was to add cdecl _before_ external :

function PyRun_SimpleString(_para1:Pchar):longint; cdecl; external
name 'PyRun_SimpleString';

I had initially added cdecl _after_ the 'external' clause , but this generated an
error ( directive CDECL has conflicts with other directives ) which confused me into thinking that cdecl was not needed.

Thanks,
Adrian Maier

Reply by email

Tiziano_mk
to Adrian Maier: I am trying to write a program that is embedding the python interpreter in order to…

Adrian Maier ha scritto:

Show quoted text 6 lines
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.

you welcome!

If you are using Lazarus on Windows, I can send you the core units of Python for Delphi adapted in order to be compiled with Lazarus (not yet usable under Free Pascal only).

references: http://membres.lycos.fr/marat/delphi/python.htm

If you need using pure Free Pascal (RTL+FCL), or are on Linux or MacOS, this has to be done in other ways, but the P4D library is the model to follow.

There is another idea to explore:
With Python 2.5 there is a module named "ctypes" that allows calling DLL functions from Python. And we know how to build a DLL with FPC, true?.

I (on windows) and my friend (on Linux) are actually working in order to make some experiment and documentation about this matter. If you could partecipate and help...

bye
tiziano

Reply by email

Adrian Maier
to Tiziano_mk
Tiziano_mk wrote 12 lines
Adrian Maier ha scritto: > 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. > you welcome! If you are using Lazarus on Windows, I can send you the core units of Python for Delphi adapted in order to be compiled with Lazarus (not yet usable under Free Pascal only).

I'm on Linux . Thanks anyway !

Show quoted text 13 lines
references: http://membres.lycos.fr/marat/delphi/python.htm If you need using pure Free Pascal (RTL+FCL), or are on Linux or MacOS, this has to be done in other ways, but the P4D library is the model to follow. There is another idea to explore: With Python 2.5 there is a module named "ctypes" that allows calling DLL functions from Python. And we know how to build a DLL with FPC, true?. I (on windows) and my friend (on Linux) are actually working in order to make some experiment and documentation about this matter. If you could partecipate and help...

It's good to know about this. But this is a solution for the opposite problem: calling pascal code from python.

Cheers,
Adrian Maier

Reply by email

Tiziano_mk
to Adrian Maier

Adrian Maier ha scritto:

Show quoted text 11 lines
On 11/14/06, Tiziano_mk wrote: > Adrian Maier ha scritto: > > I (on windows) and my friend (on Linux) are actually working in order to > make some experiment and documentation about this matter. If you could > partecipate and help... It's good to know about this. But this is a solution for the opposite problem: calling pascal code from python.

yes, it's another problem (just to puntualize).

Anyway, mainly, we also are trying to call Python from pascal on Linux.

bye
tiziano

Reply by email