Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a945a65

Browse files
committedDec 27, 2022
Fix #382
1 parent ab86012 commit a945a65

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed
 

‎Demos/Demo31/Unit1.dfm

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,22 @@ object Form1: TForm1
1313
Font.Name = 'MS Sans Serif'
1414
Font.Pitch = fpVariable
1515
Font.Style = []
16-
OldCreateOrder = True
1716
Visible = True
1817
OnCreate = FormCreate
19-
PixelsPerInch = 96
2018
TextHeight = 13
2119
object Splitter1: TSplitter
2220
Left = 0
2321
Top = 169
24-
Width = 721
22+
Width = 715
2523
Height = 3
2624
Cursor = crVSplit
2725
Align = alTop
28-
ExplicitWidth = 677
2926
end
3027
object Memo1: TMemo
3128
Left = 0
3229
Top = 172
33-
Width = 721
34-
Height = 395
30+
Width = 715
31+
Height = 378
3532
Align = alClient
3633
Font.Charset = DEFAULT_CHARSET
3734
Font.Color = clWindowText
@@ -42,8 +39,8 @@ object Form1: TForm1
4239
Lines.Strings = (
4340

4441
'from spam import DelphiVersion, MainForm, DVar, CreateComponent,' +
45-
' Application, Screen, mrOk, Form, Button, CheckBox, OpenDialog, ' +
46-
'caFree'
42+
' Application, Screen, mrOk, Component, Form, Button, CheckBox, O' +
43+
'penDialog, caFree'
4744

4845
'from spam import Point, Monitor, DrawGrid, gdSelected, clBlue, s' +
4946
'sCtrl, PageControl, TabSheet'
@@ -493,6 +490,9 @@ object Form1: TForm1
493490
' self.assertTrue(MainForm.Caption == '#39'From TTestRTTIAcc' +
494491
'ess.ObjectField'#39')'
495492
''
493+
' def testInheritance(self):'
494+
' self.assertTrue(issubclass(Form, Component))'
495+
' self.assertTrue(issubclass(Button, Component))'
496496
''
497497
'if __name__ == '#39'__main__'#39':'
498498
' try:'
@@ -506,8 +506,8 @@ object Form1: TForm1
506506
end
507507
object Panel1: TPanel
508508
Left = 0
509-
Top = 567
510-
Width = 721
509+
Top = 550
510+
Width = 715
511511
Height = 41
512512
Align = alBottom
513513
BevelOuter = bvNone
@@ -525,7 +525,7 @@ object Form1: TForm1
525525
object Memo2: TMemo
526526
Left = 0
527527
Top = 0
528-
Width = 721
528+
Width = 715
529529
Height = 169
530530
Align = alTop
531531
Font.Charset = DEFAULT_CHARSET

‎Source/PythonEngine.pas

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,7 @@ TPythonType = class(TGetSetContainer)
25602560
procedure SetModule( val : TPythonModule );
25612561
procedure SetServices( val : TTypeServices );
25622562
procedure SetTypeName( const val : AnsiString );
2563+
procedure SetBaseType(AType: TPythonType);
25632564
function CreateMethod(pSelf, args, kwds: PPyObject): PPyObject; cdecl;
25642565
procedure InitServices;
25652566
procedure SetDocString( value : TStringList );
@@ -2591,6 +2592,7 @@ TPythonType = class(TGetSetContainer)
25912592
property TheType : PyTypeObject read FType write FType;
25922593
property TheTypePtr : PPyTypeObject read GetTypePtr;
25932594
property PyObjectClass : TPyObjectClass read FPyObjectClass write SetPyObjectClass stored False;
2595+
property BaseType: TPythonType write SetBaseType;
25942596
property InstanceCount : Integer read FInstanceCount;
25952597
property CreateHits : Integer read FCreateHits;
25962598
property DeleteHits : Integer read FDeleteHits;
@@ -8024,6 +8026,14 @@ procedure TPythonType.ReallocMethods;
80248026
FType.tp_methods := MethodsData;
80258027
end;
80268028

8029+
procedure TPythonType.SetBaseType(AType: TPythonType);
8030+
begin
8031+
if AType = nil then
8032+
FType.tp_base := nil
8033+
else
8034+
FType.tp_base := @AType.FType;
8035+
end;
8036+
80278037
procedure TPythonType.SetDocString( value : TStringList );
80288038
begin
80298039
FDocString.Assign( value );

‎Source/WrapDelphi.pas

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3723,8 +3723,9 @@ procedure TPyDelphiWrapper.Notify(ADeletedObject: TObject);
37233723

37243724
procedure TPyDelphiWrapper.RegisterDelphiWrapper(
37253725
AWrapperClass: TPyDelphiObjectClass);
3726-
Var
3726+
var
37273727
RegisteredClass : TRegisteredClass;
3728+
Index: Integer;
37283729
begin
37293730
Assert(Assigned(AWrapperClass));
37303731

@@ -3734,6 +3735,15 @@ procedure TPyDelphiWrapper.RegisterDelphiWrapper(
37343735
RegisteredClass.PythonType.Engine := Engine;
37353736
RegisteredClass.PythonType.Module := fModule;
37363737
RegisteredClass.PythonType.PyObjectClass := AWrapperClass;
3738+
// Find nearest registered parent class and set it as base
3739+
for Index := fClassRegister.Count - 1 downto 0 do
3740+
with TRegisteredClass(fClassRegister[Index]) do
3741+
if RegisteredClass.DelphiClass.InheritsFrom(DelphiClass) then
3742+
begin
3743+
RegisteredClass.PythonType.BaseType := PythonType;
3744+
Break;
3745+
end;
3746+
37373747
fClassRegister.Add(RegisteredClass);
37383748
if AWrapperClass.DelphiObjectClass.InheritsFrom(TPersistent) then
37393749
Classes.RegisterClass(TPersistentClass(AWrapperClass.DelphiObjectClass));

‎Source/WrapDelphiClasses.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ class procedure TPyDelphiComponent.RegisterMethods(
10221022
'Indicates whether the component has a parent to handle its filing.');
10231023
PythonType.AddMethod('BindMethodsToEvents', @TPyDelphiComponent.BindMethodsToEvents,
10241024
'TComponent.BindMethodsToEvents(prefix)'#10 +
1025-
'Connects methods to component events if they are named using the following patter: Prefix_ComponentName_EventName.'+#10+
1025+
'Connects methods to component events if they are named using the following pattern: Prefix_ComponentName_EventName.'+#10+
10261026
'Example: def handle_button1_OnClick(Sender): pass'+#10+
10271027
'The function returns a list of tuples. Each tuple contains the name of the component, the name of the event and the method object assigned to the event.'+#10+
10281028
'Note that the prefix parameter is optional and will default to "handle_".');

‎Tests/WrapDelphiTest.pas

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ TTestWrapDelphi = class(TObject)
127127
procedure TestPassVariantArray;
128128
[Test]
129129
procedure TestClassRefParam;
130+
[Test]
131+
procedure TestInheritance;
130132
end;
131133

132134
implementation
@@ -205,7 +207,7 @@ procedure TTestWrapDelphi.SetupFixture;
205207
Py := PyDelphiWrapper.WrapInterface(TValue.From(FTestInterface));
206208
DelphiModule.SetVar('rtti_interface', Py);
207209
PythonEngine.Py_DecRef(Py);
208-
PythonEngine.ExecString('from delphi import rtti_var, rtti_rec, rtti_interface, Object, Collection, Strings');
210+
PythonEngine.ExecString('from delphi import rtti_var, rtti_rec, rtti_interface, Object, Persistent, Collection, Strings');
209211
Rtti_Var := MainModule.rtti_var;
210212
Rtti_Rec := MainModule.rtti_rec;
211213
Rtti_Interface := MainModule.rtti_interface;
@@ -277,6 +279,17 @@ procedure TTestWrapDelphi.TestGetStaticArray;
277279
Assert.AreEqual(Int64(999), Int64(PythonEngine.PyObjectAsVariant(PythonEngine.PyList_GetItem(ExtractPythonObjectFrom(List), 999))));
278280
end;
279281

282+
procedure TTestWrapDelphi.TestInheritance;
283+
var
284+
Py_Strings, Py_Persistent, Py_Object: PPyObject;
285+
begin
286+
Py_Strings := ExtractPythonObjectFrom(MainModule.Strings);
287+
Py_Persistent := ExtractPythonObjectFrom(MainModule.Persistent);
288+
Py_Object := ExtractPythonObjectFrom(MainModule.Object);
289+
Assert.AreEqual(PythonEngine.PyObject_IsSubclass(Py_Strings, Py_Persistent), 1);
290+
Assert.AreEqual(PythonEngine.PyObject_IsSubclass(Py_Strings, Py_Object), 1);
291+
end;
292+
280293
procedure TTestWrapDelphi.TestInterface;
281294
begin
282295
Rtti_Interface.SetString('Test');

0 commit comments

Comments
 (0)
Please sign in to comment.