PDA

View Full Version : Python scripting in delphi



arthurprs
07-02-2008, 04:46 PM
im a python addict and pascal lover, and i'm currently studding the python API to start a project (i already did some tests).

The project will be started from zero expect the dynamic loading of python dll methods taken from "Python for Delphi".

I did not find a good name for the project, any suggestion is welcome.


(sorry for english errors)

noeska
07-02-2008, 04:56 PM
what is the idea behind your project?
Integrating python as an scripting language inside delphi?
Does not the mentioned python for delphi already do that?
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
http://membres.lycos.fr/marat/delphi/python.htm

arthurprs
07-02-2008, 06:04 PM
easy , simple, fast integration, i'm currently using python for delphi but its too strange -.-' it really need an simplification and redesign (at least for me)

edit: it takes 10 lines to make a work of 4

arthurprs
08-02-2008, 08:18 PM
i already did some work, removed TPythonEngine, defined some const's,

what features will be interesting to have?

arthurprs
09-02-2008, 04:29 AM
I'm having a problem with assembler here,

function PyArg_Parse(args: PPyObject; format: PChar;
argp: array of Pointer): Integer; cdecl;
begin
{$IFDEF DELPHI6_OR_HIGHER}
Result := 0;
{ Do not optimize this to a "pure" assembler routine, because such
a routine does not copy the array arguments in the prologue code }
asm
lea edx, format
push [edx]

sub edx, TYPE PChar
push [edx]

mov eax, Self
mov eax, [eax].DLL_PyArg_Parse
call eax

pop edx
pop edx
mov Result, eax
end;
{$ELSE}
Result := DLL_PyArg_Parse(args, format);
{$ENDIF}
end;

I don't know much assembly, but i think the problem is that the assembly is trying to call DLL_PyArg_Parse as a class function (as in Python for Delphi original code), but now DLL_PyArg_Parse is a normal procedure loaded from python dll.

How can i solve the problem?


sorry for the english :oops:


edit: same problem on some other functions, i really need help here

JSoftware
09-02-2008, 09:50 AM
I don't think the code makes very much sense but I ofcourse haven't seen most of it. Would this be better?
function PyArg_Parse(args: PPyObject; format: PChar;
argp: array of Pointer): Integer; cdecl;
begin
{$IFDEF DELPHI6_OR_HIGHER}
Result := 0;
{ Do not optimize this to a "pure" assembler routine, because such
a routine does not copy the array arguments in the prologue code }
asm
lea edx, format
push [edx]

sub edx, TYPE PChar
push [edx]

mov eax, DLL_PyArg_Parse
call eax

pop edx
pop edx
mov Result, eax
end;
{$ELSE}
Result := DLL_PyArg_Parse(args, format);
{$ENDIF}
end;

arthurprs
09-02-2008, 05:13 PM
it works and now i know how fix another problems, thanks!

arthurprs
09-02-2008, 08:49 PM
the current test

program Project1;

{$APPTYPE CONSOLE}

uses
FastMM4,
SysUtils,
Classes,
Math,
python,
PythonEx;

function testfunc( Self,args : PPyObject): PPyObject; cdecl;
var
s : PChar;
begin
PyArg_ParseTuple(args,argstring,[@s]);
Result := Py_BuildValue(argstring+argint,[s,Length(s)]);
end;

function testfunc2( Self,args : PPyObject): PPyObject; cdecl;
var
s : PChar;
begin
PyArg_ParseTuple(args,argstring,[@s]);
Result := Py_BuildValue(argstring+argchar,[s,s[0]]);
end;

var
i : single;
n : single;

begin
Py_Initialize;

n := DegToRad(359);

Py_RegisterMethod('test','test',testfunc);
Py_RegisterMethod('test2','test2',testfunc2);

PyRun_SimpleString('import test; a , b = test.test("=.= im getting crazy with assembly"); print "length of ''%s'' is %s" % (a , b)');
PyRun_SimpleString('import test2; a , b = test2.test2("delphi"); print "first char of ''%s'' is %s" % (a , b)');

Py_RunMethod('math','cos',argsingle,[n],argsingle,[@i]);

Writeln('cos of 359 degrees = ',floattostr(i));
Py_Finalize;
Readln;
end.

http://img246.imageshack.us/img246/2315/capture09022008185219sj1.gif

arthurprs
09-02-2008, 11:24 PM
looks like that the website has an rollback :?