Skip to content

Commit 0117d7d

Browse files
committed
Made TPyObject.PythonType a class var.
ExcludedExposedMembers implemented.
1 parent dc99c7f commit 0117d7d

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

Source/PythonEngine.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,9 +2387,9 @@ TPyObject = class
23872387
procedure Set_ob_refcnt(const Value: NativeInt);
23882388
procedure Set_ob_type(const Value: PPyTypeObject);
23892389
public
2390-
PythonType : TPythonType;
2391-
IsSubtype : Boolean;
2392-
PythonAlloc : Boolean;
2390+
IsSubtype: Boolean;
2391+
PythonAlloc: Boolean;
2392+
class var PythonType: TPythonType;
23932393

23942394
// Constructors & Destructors
23952395
constructor Create(APythonType: TPythonType); virtual;
@@ -7987,6 +7987,7 @@ procedure TPythonType.SetPyObjectClass( val : TPyObjectClass );
79877987
if Assigned(val) then
79887988
begin
79897989
FType.tp_basicsize := val.InstanceSize + Sizeof(PyObject);
7990+
val.PythonType := Self;
79907991
val.RegisterMethods( Self );
79917992
val.RegisterMembers( Self );
79927993
val.RegisterGetSets( Self );

Source/WrapDelphi.pas

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,7 +3268,7 @@ function TPyDelphiObject.Dir_Wrapper(args: PPyObject): PPyObject;
32683268

32693269
class function TPyDelphiObject.GetTypeName : string;
32703270
begin
3271-
Result := Copy(DelphiObjectClass.ClassName, 2, MaxInt);
3271+
Result := Copy(DelphiObjectClass.ClassName, 2);
32723272
end;
32733273

32743274
function TPyDelphiObject.HasContainerAccessClass: Boolean;
@@ -3353,11 +3353,9 @@ class procedure TPyDelphiObject.RegisterMethods(PythonType: TPythonType);
33533353
PythonType.AddMethod('Free', @TPyDelphiObject.Free_Wrapper,
33543354
'TObject.Free()'#10 +
33553355
'Frees the Wrapped Delphi Object');
3356-
{$IFNDEF EXTENDED_RTTI}
33573356
PythonType.AddMethod('InheritsFrom', @TPyDelphiObject.InheritsFrom_Wrapper,
33583357
'TObject.InheritsFrom(ClassName)'#10 +
33593358
'Returns True if Delphi Object is or inherits from ClassName');
3360-
{$ENDIF EXTENDED_RTTI}
33613359
PythonType.AddMethod('ToTuple', @TPyDelphiObject.ToTuple_Wrapper,
33623360
'TStrings.ToTuple()'#10 +
33633361
'If the object is a container (TStrings, TComponent...), it returns the content of the sequence as a Python tuple object.');
@@ -3627,8 +3625,17 @@ class procedure TPyDelphiObject.SetupType(PythonType: TPythonType);
36273625

36283626
{$IFDEF EXTENDED_RTTI}
36293627
class function TPyDelphiObject.ExcludedExposedMembers: TArray<string>;
3628+
var
3629+
I, MethodCount: Integer;
36303630
begin
3631-
Result := ['Free', 'CPP_ABI_1', 'CPP_ABI_2', 'CPP_ABI_3'];
3631+
MethodCount := PythonType.MethodCount;
3632+
SetLength(Result, MethodCount + PythonType.GetSetCount);
3633+
3634+
for I := 0 to MethodCount - 1 do
3635+
Result[I] := string(PythonType.Methods[I].ml_name);
3636+
3637+
for I := 0 to PythonType.GetSetCount - 1 do
3638+
Result[MethodCount + I] := string(PythonType.GetSet[I].name);
36323639
end;
36333640

36343641
class procedure TPyDelphiObject.ExposeMethods(AClass: TClass;
@@ -3658,12 +3665,10 @@ class procedure TPyDelphiObject.ExposeMethods(AClass: TClass;
36583665
then
36593666
Continue;
36603667

3661-
// Ignore excluded methods
3662-
if MatchStr(LRttiMethod.Name, AExcludedMethodNames) then
3663-
Continue;
3664-
3665-
// Ignore duplicate methods
3666-
if MatchStr(LRttiMethod.Name, AddedMethods) then
3668+
// Ignore methods with unhandled return type
3669+
if Assigned(LRttiMethod.ReturnType) and (LRttiMethod.ReturnType.TypeKind
3670+
in [tkUnknown, tkMethod, tkPointer, tkProcedure])
3671+
then
36673672
Continue;
36683673

36693674
// Skip methods declared in NearestAncestorClass and its ancestors
@@ -3673,6 +3678,14 @@ class procedure TPyDelphiObject.ExposeMethods(AClass: TClass;
36733678
then
36743679
Continue;
36753680

3681+
// Ignore excluded methods
3682+
if MatchStr(LRttiMethod.Name, AExcludedMethodNames) then
3683+
Continue;
3684+
3685+
// Ignore duplicate methods
3686+
if MatchStr(LRttiMethod.Name, AddedMethods) then
3687+
Continue;
3688+
36763689
AddedMethods := AddedMethods + [LRttiMethod.Name];
36773690

36783691
// Create the exposed method
@@ -3738,6 +3751,13 @@ class procedure TPyDelphiObject.ExposeFields(AClass: TClass;
37383751
if (Ord(LRttiField.Visibility) < Ord(TMemberVisibility.mvPublic)) then
37393752
Continue;
37403753

3754+
// Skip methods declared in NearestAncestorClass and its ancestors
3755+
LClass := (LRttiField.Parent as TRttiInstanceType).MetaclassType;
3756+
if (NearestAncestorClass <> nil) and ((LClass = NearestAncestorClass) or
3757+
not (LClass.InheritsFrom(NearestAncestorClass)))
3758+
then
3759+
Continue;
3760+
37413761
// Ignore excluded fields
37423762
if MatchStr(LRttiField.Name, AExcludedFieldNames) then
37433763
Continue;
@@ -3746,19 +3766,12 @@ class procedure TPyDelphiObject.ExposeFields(AClass: TClass;
37463766
if MatchStr(LRttiField.Name, AddedFields) then
37473767
Continue;
37483768

3749-
// Skip methods declared in NearestAncestorClass and its ancestors
3750-
LClass := (LRttiField.Parent as TRttiInstanceType).MetaclassType;
3751-
if (NearestAncestorClass <> nil) and ((LClass = NearestAncestorClass) or
3752-
not (LClass.InheritsFrom(NearestAncestorClass)))
3753-
then
3754-
Continue;
3755-
37563769
// Skip if the FieldType is missing
37573770
if LRttiField.FieldType = nil then
37583771
Continue;
37593772

37603773
// Skip if the type cannot be handled
3761-
if LRttiField.FieldType.TypeKind in [tkUnknown, tkMethod, tkProcedure] then
3774+
if LRttiField.FieldType.TypeKind in [tkUnknown, tkMethod, tkPointer, tkProcedure] then
37623775
Continue;
37633776

37643777
AddedFields := AddedFields + [LRttiField.Name];
@@ -3812,6 +3825,13 @@ class procedure TPyDelphiObject.ExposeProperties(AClass: TClass;
38123825
if (Ord(LRttiProperty.Visibility) < Ord(TMemberVisibility.mvPublic)) then
38133826
Continue;
38143827

3828+
// Skip methods declared in NearestAncestorClass and its ancestors
3829+
LClass := (LRttiProperty.Parent as TRttiInstanceType).MetaclassType;
3830+
if (NearestAncestorClass <> nil) and ((LClass = NearestAncestorClass) or
3831+
not (LClass.InheritsFrom(NearestAncestorClass)))
3832+
then
3833+
Continue;
3834+
38153835
// Ignore excluded properties
38163836
if MatchStr(LRttiProperty.Name, AExcludedPropertyNames) then
38173837
Continue;
@@ -3820,13 +3840,6 @@ class procedure TPyDelphiObject.ExposeProperties(AClass: TClass;
38203840
if MatchStr(LRttiProperty.Name, AddedProperties) then
38213841
Continue;
38223842

3823-
// Skip methods declared in NearestAncestorClass and its ancestors
3824-
LClass := (LRttiProperty.Parent as TRttiInstanceType).MetaclassType;
3825-
if (NearestAncestorClass <> nil) and ((LClass = NearestAncestorClass) or
3826-
not (LClass.InheritsFrom(NearestAncestorClass)))
3827-
then
3828-
Continue;
3829-
38303843
// Skip if the PropertyType is missing
38313844
if LRttiProperty.PropertyType = nil then
38323845
Continue;
@@ -3836,7 +3849,7 @@ class procedure TPyDelphiObject.ExposeProperties(AClass: TClass;
38363849
Continue;
38373850

38383851
// Skip if the type cannot be handled (as with fields - tkMethod)
3839-
if LRttiProperty.PropertyType.TypeKind in [tkUnknown, tkProcedure] then
3852+
if LRttiProperty.PropertyType.TypeKind in [tkUnknown, tkPointer, tkProcedure] then
38403853
Continue;
38413854

38423855
AddedProperties := AddedProperties + [LRttiProperty.Name];
@@ -3894,6 +3907,13 @@ class procedure TPyDelphiObject.ExposeIndexedProperties(AClass: TClass;
38943907
if (Ord(LRttiProperty.Visibility) < Ord(TMemberVisibility.mvPublic)) then
38953908
Continue;
38963909

3910+
// Skip methods declared in NearestAncestorClass and its ancestors
3911+
LClass := (LRttiProperty.Parent as TRttiInstanceType).MetaclassType;
3912+
if (NearestAncestorClass <> nil) and ((LClass = NearestAncestorClass) or
3913+
not (LClass.InheritsFrom(NearestAncestorClass)))
3914+
then
3915+
Continue;
3916+
38973917
// Ignore excluded properties
38983918
if MatchStr(LRttiProperty.Name, AExcludedPropertyNames) then
38993919
Continue;
@@ -3902,13 +3922,6 @@ class procedure TPyDelphiObject.ExposeIndexedProperties(AClass: TClass;
39023922
if MatchStr(LRttiProperty.Name, AddedProperties) then
39033923
Continue;
39043924

3905-
// Skip methods declared in NearestAncestorClass and its ancestors
3906-
LClass := (LRttiProperty.Parent as TRttiInstanceType).MetaclassType;
3907-
if (NearestAncestorClass <> nil) and ((LClass = NearestAncestorClass) or
3908-
not (LClass.InheritsFrom(NearestAncestorClass)))
3909-
then
3910-
Continue;
3911-
39123925
// Skip if the PropertyType is missing
39133926
if LRttiProperty.PropertyType = nil then
39143927
Continue;
@@ -3918,7 +3931,7 @@ class procedure TPyDelphiObject.ExposeIndexedProperties(AClass: TClass;
39183931
Continue;
39193932

39203933
// Skip if the type cannot be handled (as with fields - tkMethod)
3921-
if LRttiProperty.PropertyType.TypeKind in [tkUnknown, tkProcedure] then
3934+
if LRttiProperty.PropertyType.TypeKind in [tkUnknown, tkPointer, tkProcedure] then
39223935
Continue;
39233936

39243937
AddedProperties := AddedProperties + [LRttiProperty.Name];

0 commit comments

Comments
 (0)