@@ -610,7 +610,6 @@ TPythonVersionProp = record
610610 m_free : inquiry;
611611 end ;
612612
613-
614613 // object.h
615614 PyTypeObject = { $IFDEF CPUX86} packed { $ENDIF} record
616615 ob_refcnt: NativeInt;
@@ -699,6 +698,7 @@ TPythonVersionProp = record
699698 tp_xxx8 : NativeInt;
700699 tp_xxx9 : NativeInt;
701700 tp_xxx10 : NativeInt;
701+ tp_pythontype : Pointer; // Introduced for FindPythonType optimization
702702 end ;
703703
704704 // from pystate.h
@@ -1903,8 +1903,7 @@ TPythonEngine = class(TPythonInterface)
19031903 procedure AddClient ( client : TEngineClient );
19041904 procedure RemoveClient ( client : TEngineClient );
19051905 function FindClient ( const aName : string ) : TEngineClient;
1906- function FindPythonType (const TypeName : AnsiString): TPythonType; overload;
1907- function FindPythonType (PyType: PPyTypeObject): TPythonType; overload;
1906+ function FindPythonType (const TypeName : AnsiString): TPythonType;
19081907 function TypeByName ( const aTypeName : AnsiString ) : PPyTypeObject;
19091908 function ModuleByName ( const aModuleName : AnsiString ) : PPyObject;
19101909 function MethodsByName ( const aMethodsContainer: string ) : PPyMethodDef;
@@ -2780,6 +2779,7 @@ function pyio_GetTypesStats(self, args : PPyObject) : PPyObject; cdecl;
27802779function GetPythonEngine : TPythonEngine;
27812780function PythonOK : Boolean;
27822781function PythonToDelphi ( obj : PPyObject ) : TPyObject;
2782+ function FindPythonType (PyType: PPyTypeObject): TPythonType;
27832783function IsDelphiObject ( obj : PPyObject ) : Boolean;
27842784procedure PyObjectDestructor ( pSelf : PPyObject); cdecl;
27852785procedure Register ;
@@ -6153,19 +6153,6 @@ function TPythonEngine.FindPythonType(const TypeName: AnsiString): TPythonType;
61536153 end ;
61546154end ;
61556155
6156- function TPythonEngine.FindPythonType (PyType: PPyTypeObject): TPythonType;
6157- var
6158- I : Integer;
6159- begin
6160- Result := nil ;
6161- for I := 0 to ClientCount - 1 do
6162- if (Clients[I] is TPythonType) and (TPythonType(Clients[I]).TheTypePtr = PyType) then
6163- begin
6164- Result := TPythonType(Clients[I]);
6165- Break;
6166- end ;
6167- end ;
6168-
61696156function TPythonEngine.FindFunction (const ModuleName,FuncName: AnsiString): PPyObject;
61706157var
61716158 module ,func: PPyObject;
@@ -8649,6 +8636,7 @@ procedure TPythonType.Initialize;
86498636 InitServices;
86508637 if Engine.PyType_Ready(TheTypePtr) <> 0 then
86518638 Engine.CheckError;
8639+ FType.tp_pythontype := Self; // Store self into FType
86528640 inherited ;
86538641end ;
86548642
@@ -9370,27 +9358,27 @@ function PythonOK : Boolean;
93709358 (gPythonEngine.Initialized or gPythonEngine.Finalizing);
93719359end ;
93729360
9373- function IsDelphiObject ( obj : PPyObject ) : Boolean ;
9361+ function FindPythonType (PyType: PPyTypeObject): TPythonType ;
93749362var
9375- t : PPyTypeObject;
9363+ Typ : PPyTypeObject;
93769364begin
9377- Result := False ;
9365+ Result := nil ;
93789366 // Here's a simple trick: we compare the object destructor to
93799367 // our special destructor for Delphi objects, or
93809368 // we check if one of the parent types of obj has a Delphi destructor.
9381- if Assigned(obj) then
9369+ Typ := PyType;
9370+ while Assigned(Typ) do
93829371 begin
9383- t := obj^.ob_type;
9384- while Assigned(t) do
9385- begin
9386- if @t^.tp_dealloc = @PyObjectDestructor then
9387- begin
9388- Result := True;
9389- Break;
9390- end ;
9391- t := t^.tp_base;
9392- end ;
9372+ if @Typ^.tp_dealloc = @PyObjectDestructor then
9373+ Exit(Typ.tp_pythontype);
9374+ Typ := Typ^.tp_base;
93939375 end ;
9376+ // var
9377+ end ;
9378+
9379+ function IsDelphiObject ( obj : PPyObject ) : Boolean;
9380+ begin
9381+ Result := Assigned(obj) and (FindPythonType(obj^.ob_type) <> nil );
93949382end ;
93959383
93969384procedure Register ;
0 commit comments