5

私はpython4delphiを使用しています。ラップされたDelphiクラス関数からオブジェクトを返すにはどうすればよいですか?

コードスニペット:

Pythonスクリプトにラップした単純なDelphiクラスがありますよね?

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;

クラスにPythonコードへのアクセスを許可するために、demo32のようなTPySimpleを作成しました。私のPythonモジュール名はtestです。

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

2 に答える 2

1

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:

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;

Memo1.Linesの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オブジェクトへのポインタである可能性があります。

最後に、「AddMethod」を使用してDemo8で妥協案を見つけました。まったく美しくない!! しかし、それは機能します。

于 2016-09-30T09:36:45.710 に答える
0

OPによると、彼はここで答えを見つけました:http ://code.google.com/p/python4delphi/issues/detail?id=17

(参照用のコピー&ペースト)

やあ、

私は提案をしました-D2010(およびそれ以降)の新しいRTTI(実行時型情報)機能を利用して、DelphiオブジェクトをPythonに簡単に公開できるようにします。

現在、ホストされているPythonコードにクラスを公開するには、コードを記述しすぎる必要があります(demo06を確認してください)。新しいバージョンのDelphiで新しいRTTI機能を利用すると、プロセスを大幅に改善できると思います。

たとえば、Delphiクロム埋め込みプロジェクトを確認してください。Delphiクラスのインターフェイスを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 +#から抽出されました。

D2010以降に導入されたRTTIに関するいくつかのイントロ:http: //robstechcorner.blogspot.com/2009/09/delphi-2010-rtti-basics.html

于 2014-05-15T10:19:28.997 に答える