Skip to content

Commit 6b88fc2

Browse files
committed
tp_compare is not available in Python 3. In fact the slot is now used for something else.
Use bsRichCompare instead of bsCompare Default implementation of RichCompare in TPyObject (moved up from TPyDelphiObject) using the Compare method.
1 parent 816f7dd commit 6b88fc2

File tree

3 files changed

+22
-53
lines changed

3 files changed

+22
-53
lines changed

PythonForDelphi/Components/Sources/Core/PythonEngine.pas

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7970,8 +7970,23 @@ function TPyObject.Clear: integer;
79707970
end;
79717971

79727972
function TPyObject.RichCompare( obj : PPyObject; Op : TRichComparisonOpcode) : PPyObject;
7973-
begin
7974-
Result := nil;
7973+
Var
7974+
Res : Boolean;
7975+
begin
7976+
Res := False;
7977+
case Op of
7978+
pyLT: Res := Compare(obj) < 0;
7979+
pyLE: Res := Compare(obj) <= 0;
7980+
pyEQ: Res := Compare(obj) = 0;
7981+
pyNE: Res := Compare(obj) <> 0;
7982+
pyGT: Res := Compare(obj) > 0;
7983+
pyGE: Res := Compare(obj) >= 0;
7984+
end;
7985+
if Res then
7986+
Result := PPyObject(GetPythonEngine.Py_True)
7987+
else
7988+
Result := PPyObject(GetPythonEngine.Py_False);
7989+
GetPythonEngine.Py_INCREF( Result );
79757990
end;
79767991

79777992
function TPyObject.Iter : PPyObject;

PythonForDelphi/Components/Sources/Core/WrapDelphi.pas

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,6 @@ TPyDelphiObject = class (TPyInterfacedObject, IFreeNotificationSubscriber)
545545
function SetAttrO( key, value: PPyObject) : Integer; override;
546546
// Objects are equal when they refer to the same DelphiObject
547547
function Compare( obj: PPyObject) : Integer; override;
548-
function RichCompare( obj : PPyObject; Op : TRichComparisonOpcode) : PPyObject; override;
549548
function Repr : PPyObject; override;
550549
// automatic iterator support when the wrapper implements IContainerAccessProvider
551550
function Iter : PPyObject; override;
@@ -590,7 +589,6 @@ TPyDelphiVarParameter = class(TPyObject)
590589
public
591590
destructor Destroy; override;
592591

593-
function Compare( obj: PPyObject) : Integer; override;
594592
function RichCompare( obj : PPyObject; Op : TRichComparisonOpcode) : PPyObject; override;
595593
function Repr : PPyObject; override;
596594

@@ -1803,27 +1801,6 @@ function TPyDelphiObject.Repr: PPyObject;
18031801
[DelphiObjectClass.ClassName, NativeInt(Self)]))) );
18041802
end;
18051803

1806-
function TPyDelphiObject.RichCompare(obj: PPyObject;
1807-
Op: TRichComparisonOpcode): PPyObject;
1808-
Var
1809-
Res : Boolean;
1810-
begin
1811-
Res := False;
1812-
case Op of
1813-
pyLT: Res := Compare(obj) < 0;
1814-
pyLE: Res := Compare(obj) <= 0;
1815-
pyEQ: Res := Compare(obj) = 0;
1816-
pyNE: Res := Compare(obj) <> 0;
1817-
pyGT: Res := Compare(obj) > 0;
1818-
pyGE: Res := Compare(obj) >= 0;
1819-
end;
1820-
if Res then
1821-
Result := PPyObject(GetPythonEngine.Py_True)
1822-
else
1823-
Result := PPyObject(GetPythonEngine.Py_False);
1824-
GetPythonEngine.Py_INCREF( Result );
1825-
end;
1826-
18271804
function TPyDelphiObject.SetAttrO(key, value: PPyObject): Integer;
18281805

18291806
function HandleEvent(PropInfo: PPropInfo) : Integer;
@@ -1991,7 +1968,7 @@ class procedure TPyDelphiObject.SetupType(PythonType: TPythonType);
19911968
PythonType.TypeFlags := PythonType.TypeFlags + [tpfBaseType, tpfHaveRichCompare];
19921969
PythonType.GenerateCreateFunction := False;
19931970
PythonType.DocString.Text := 'Wrapper for Delphi ' + DelphiObjectClass.ClassName;
1994-
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsCompare, bsRichCompare];
1971+
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsRichCompare];
19951972
_ContainerAccessClass := GetContainerAccessClass;
19961973
if Assigned(_ContainerAccessClass) then
19971974
begin
@@ -2195,29 +2172,6 @@ class procedure TPyDelphiMethodObject.SetupType( PythonType : TPythonType );
21952172

21962173
{ TPyDelphiVarParameter }
21972174

2198-
function TPyDelphiVarParameter.Compare(obj: PPyObject): Integer;
2199-
var
2200-
_value : PPyObject;
2201-
begin
2202-
with GetPythonEngine do
2203-
begin
2204-
if Self.Value = nil then
2205-
_value := Py_None
2206-
else
2207-
_value := Self.Value;
2208-
if IsPython3000 then begin
2209-
if PyObject_RichCompareBool(_value, obj, PY_LT) = 1 then
2210-
Result := -1
2211-
else if PyObject_RichCompareBool(_value, obj, PY_EQ) = 1 then
2212-
Result := 0
2213-
else
2214-
Result := 1;
2215-
PyErr_Clear;
2216-
end else
2217-
Result := PyObject_Compare(_value, obj);
2218-
end;
2219-
end;
2220-
22212175
destructor TPyDelphiVarParameter.Destroy;
22222176
begin
22232177
Value := nil;
@@ -2284,7 +2238,7 @@ class procedure TPyDelphiVarParameter.SetupType(PythonType: TPythonType);
22842238
PythonType.TypeFlags := PythonType.TypeFlags + [tpfBaseType];
22852239
PythonType.GenerateCreateFunction := False;
22862240
PythonType.DocString.Text := 'Container object allowing modification of Delphi var parameters from Python';
2287-
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsCompare];
2241+
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsRichCompare];
22882242
end;
22892243

22902244
procedure TPyDelphiVarParameter.SetValue(const Value: PPyObject);

PythonForDelphi/Components/Sources/Core/WrapDelphiTypes.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class procedure TPyDelphiPoint.SetupType(PythonType: TPythonType);
290290
PythonType.TypeFlags := PythonType.TypeFlags + [tpfBaseType];
291291
PythonType.GenerateCreateFunction := False;
292292
PythonType.DocString.Text := 'wrapper for Delphi TPoint type';
293-
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsCompare];
293+
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsRichCompare];
294294
end;
295295

296296
function TPyDelphiPoint.Set_X(AValue: PPyObject;
@@ -523,7 +523,7 @@ class procedure TPyDelphiRect.SetupType(PythonType: TPythonType);
523523
PythonType.TypeFlags := PythonType.TypeFlags + [tpfBaseType];
524524
PythonType.GenerateCreateFunction := False;
525525
PythonType.DocString.Text := 'wrapper for Delphi TRect type';
526-
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsCompare];
526+
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsRichCompare];
527527
end;
528528

529529

@@ -599,7 +599,7 @@ class procedure TPyDelphiSize.SetupType(PythonType: TPythonType);
599599
PythonType.TypeFlags := PythonType.TypeFlags + [tpfBaseType];
600600
PythonType.GenerateCreateFunction := False;
601601
PythonType.DocString.Text := 'wrapper for Delphi TSize type';
602-
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsCompare];
602+
PythonType.Services.Basic := [bsGetAttrO, bsSetAttrO, bsRepr, bsStr, bsRichCompare];
603603
end;
604604

605605
function TPyDelphiSize.Set_CX(AValue: PPyObject;

0 commit comments

Comments
 (0)