2012-03-19 199 views
5

我正在使用python4delphi。如何從包裝的Delphi類函數返回一個對象?Python4Delphi:在函數中返回一個python對象。 (DelphiWrapper)

代碼段:

我有一個簡單的Delphi類,我包裹的Python腳本,對不對?

TSimple = Class 
Private 
    function getvar1:string; 
Public  
Published 
    property var1:string read getVar1; 
    function getObj:TSimple; 
end; 
... 
function TSimple.getVar1:string; 
begin 
    result:='hello'; 
end; 
function TSimple.getObj:TSimple; 
begin 
    result:=self; 
end; 

我使TPySimple像demo32一樣給予Python代碼的類訪問權限。我的Python模塊名稱是測試。

TPySimple = class(TPyDelphiPersistent) 
    // Constructors & Destructors 
    constructor Create(APythonType : TPythonType); override; 
    constructor CreateWith(PythonType : TPythonType; args : PPyObject); override; 
    // Basic services 
    function Repr : PPyObject; override; 

    class function DelphiObjectClass : TClass; override; 
end; 
... 

{ TPySimple } 

constructor TPySimple.Create(APythonType: TPythonType); 
begin 
    inherited; 
    // we need to set DelphiObject property 
    DelphiObject := TSimple.Create; 
    with TSimple(DelphiObject) do begin 
    end; 
    Owned := True; // We own the objects we create 
end; 

constructor TPySimple.CreateWith(PythonType: TPythonType; args: PPyObject); 
begin 
    inherited; 
    with GetPythonEngine, DelphiObject as TSimple do 
    begin 
     if PyArg_ParseTuple(args, ':CreateSimple') = 0 then 
     Exit; 
    end; 
end; 

class function TPySimple.DelphiObjectClass: TClass; 
begin 
    Result := TSimple; 
end; 

function TPySimple.Repr: PPyObject; 
begin 
    with GetPythonEngine, DelphiObject as TSimple do 
    Result := VariantAsPyObject(Format('',[])); 
    // or Result := PyString_FromString(PAnsiChar(Format('()',[]))); 
end; 

而現在的Python代碼:

import test 

a = test.Simple() 
# try access the property var1 and everything is right 
print a.var1 
# work's, but.. 
b = a.getObj(); 
# raise a exception that not find any attributes named getObj. 
# if the function returns a string for example, it's work. 
+4

有人投票不用說爲什麼(一些人有壞習慣,我希望我可以低估他們!)我的猜測是,你應該解釋更多關於你的意思(也許是代碼片段),什麼是不工作或者你已經嘗試過的東西。 – 2012-03-19 16:50:11

+1

這個問題對我來說似乎很模糊。缺乏活動支持這種感覺。 – 2012-03-19 19:43:27

+0

好吧,夥計們,拿一些代碼片段。 – 2012-03-19 20:26:55

回答

0

據他找到了答案這裏的OP:http://code.google.com/p/python4delphi/issues/detail?id=17

(A複製粘貼供參考)

嗨,

我有一個建議 - 使暴露的Delphi對象python無痛通過利用D2010(及以上版本)中的新RTTI(運行時類型信息)功能。

當前將一個類暴露給託管的Python代碼需要你編寫太多的代碼(檢查demo06),我想如果我們在新版本的Delphi中利用新的RTTI特性,可以改進過程許多。

例如,檢查出的德爾福鉻嵌入式的項目,所有你需要做的,以公開任何德爾福類JavaScript環境的接口,是註冊類:

// this is your class exposed to JS 
    Test = class 
    class procedure doit(const v: string); 
    end; 

initialization 
// Register your class 
    TCefRTTIExtension.Register('app', Test); 

// and in JavaScript code to call that class above: 
app.doit(''foo'')', '', 0); 

酷!不是嗎?

以上代碼從提取:http://groups.google.com/group/delphichromiumembedded/browse_thread/thread/1793e7ca66012f0c/8ab31a5ecdb6bf48?lnk=gst&q=JavaScript+return+#

約RTTI一些介紹,因爲D2010介紹: http://robstechcorner.blogspot.com/2009/09/delphi-2010-rtti-basics.html

0

我已經在使用DelphiWrapper遇到同樣的問題。

首先,使用{$ MethodInfo的ON}將防止異常使RTTI:

提出一個例外是無法找到名爲getObj任何屬性。

寫TSimple類是這樣的:

{$METHODINFO ON} 
TSimple = Class 
Private 
    function getvar1:string; 
Public  
Published 
    property var1:string read getVar1; 
    function getObj:TSimple; 
end; 
{$METHODINFO OFF} 

現在getObj函數返回一個值 - 一個整數!

我會告訴你們我做了什麼。我修改Demo32:

Unit1.pas:在Memo1

TPoint = class(TPersistent) 
private 
    fx, fy : Integer; 
    fName : String; 
public 
    constructor Create(); 
    procedure OffsetBy(dx, dy : integer); 
    function MySelf: TPoint; //**access self using function** 
published 
    property x : integer read fx write fx; 
    property y : integer read fy write fy; 
    property Name : string read fName write fName; 
    property MySelf2: TPoint read MySelf; //**access self using property** 
end; 

Python代碼。行:

import spam 

p = spam.Point(2, 5) 

b = p.MySelf() //**using function** 
print 'Using MySelf: ', type(b), b 
c = p.MySelf2 //**using property** 
print 'Using MySelf2: ', type(c), c 

然後它給了這樣的結果:

Using MySelf: <type 'int'> 31957664 
Using MySelf2: <type 'Point'> (2, 5) 

該函數返回一個int,這是有線。也許它是一個指向由DelphiWrapper錯誤包裝的TPoint對象的指針。

最後我在Demo8中找到了一個使用「AddMethod」的折中方法。一點都不漂亮!但它的工作。