Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 741c164

Browse files
committed
allow array return from invoked method
1 parent 0c73fc5 commit 741c164

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

‎Source/WrapDelphi.pas‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,19 @@ function RegisteredUnits : TRegisteredUnits;
946946
{ Helper functions }
947947

948948
{$IFDEF EXTENDED_RTTI}
949+
function DynArrayToPython(const Value: TValue): PPyObject;
950+
var
951+
I: Integer;
952+
V: Variant;
953+
begin
954+
Result := GetPythonEngine().PyList_New(Value.GetArrayLength);
955+
for I := 0 to Value.GetArrayLength() - 1 do
956+
begin
957+
V := Value.GetArrayElement(i).AsVariant;
958+
GetPythonEngine().PyList_SetItem(Result, I, GetPythonEngine().VariantAsPyObject(V));
959+
end;
960+
end;
961+
949962
function SimpleValueToPython(const Value: TValue; out ErrMsg: string): PPyObject;
950963
begin
951964
Result := nil;
@@ -980,8 +993,10 @@ function SimpleValueToPython(const Value: TValue; out ErrMsg: string): PPyObject
980993
Result := SetToPython(Value.TypeData.CompType^,
981994
PInteger(Value.GetReferenceToRawData)^);
982995
end;
983-
tkClass, tkMethod, tkArray,
984-
tkRecord, tkInterface, tkDynArray,
996+
tkArray, tkDynArray:
997+
Result := DynArrayToPython(Value);
998+
tkClass, tkMethod,
999+
tkRecord, tkInterface,
9851000
tkClassRef, tkPointer, tkProcedure:
9861001
ErrMsg := rs_ErrValueToPython;
9871002
else
@@ -1045,7 +1060,7 @@ function SimplePythonToValue(PyValue: PPyObject; TypeInfo: PTypeInfo;
10451060
Result := True;
10461061
end;
10471062
tkClass, tkMethod, tkArray,
1048-
tkRecord, tkInterface, tkDynArray,
1063+
tkRecord, tkInterface,
10491064
tkClassRef, tkPointer, tkProcedure:
10501065
ErrMsg := rs_ErrPythonToValue;
10511066
else

‎Tests/WrapDelphiTest.pas‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ TTestRecord = record
3434
end;
3535

3636
TFruitDynArray = TArray<TFruit>;
37+
TStaticArray = array[0..999] of Int64;
3738
TTestRttiAccess = class
3839
private
3940
FFruit: TFruit;
@@ -49,6 +50,8 @@ TTestRttiAccess = class
4950
procedure BuyFruits(AFruits: TFruits);
5051
procedure SellFruits(const AFruits: TFruitDynArray);
5152
procedure SellFruitsInt(const AFruits:TIntegerDynArray);
53+
function GetDynArray: TInt64DynArray;
54+
function GetStaticArray: TStaticArray;
5255
property Fruit: TFruit read FFruit write FFruit;
5356
property Fruits: TFruits read FFruits write FFruits;
5457

@@ -106,6 +109,10 @@ TTestWrapDelphi = class(TObject)
106109
procedure TestInterfaceField;
107110
[Test]
108111
procedure TestDynArrayParameters;
112+
[Test]
113+
procedure TestGetDynArray;
114+
[Test]
115+
procedure TestGetStaticArray;
109116
end;
110117

111118
implementation
@@ -217,6 +224,26 @@ procedure TTestWrapDelphi.TestEnumProperty;
217224
Assert.IsTrue(TestRttiAccess.Fruit = Banana);
218225
end;
219226

227+
procedure TTestWrapDelphi.TestGetDynArray;
228+
var
229+
List: Variant;
230+
begin
231+
List := Rtti_Var.GetDynArray();
232+
Assert.IsTrue(VarIsPythonList(List));
233+
Assert.AreEqual(1000000, Integer(len(List)));
234+
Assert.AreEqual(Int64(999999), Int64(PythonEngine.PyObjectAsVariant(PythonEngine.PyList_GetItem(ExtractPythonObjectFrom(List), 999999))));
235+
end;
236+
237+
procedure TTestWrapDelphi.TestGetStaticArray;
238+
var
239+
List: Variant;
240+
begin
241+
List := Rtti_Var.GetStaticArray();
242+
Assert.IsTrue(VarIsPythonList(List));
243+
Assert.AreEqual(1000, Integer(len(List)));
244+
Assert.AreEqual(Int64(999), Int64(PythonEngine.PyObjectAsVariant(PythonEngine.PyList_GetItem(ExtractPythonObjectFrom(List), 999))));
245+
end;
246+
220247
procedure TTestWrapDelphi.TestInterface;
221248
begin
222249
Rtti_Interface.SetString('Test');
@@ -369,6 +396,23 @@ procedure TTestWrapDelphi.TestDynArrayParameters;
369396
Assert.IsTrue(TestRttiAccess.Fruits = [Orange]);
370397
end;
371398

399+
function TTestRttiAccess.GetDynArray: TInt64DynArray;
400+
var
401+
I: Integer;
402+
begin
403+
SetLength(Result, 1000000);
404+
for I := 0 to Length(Result) - 1 do
405+
Result[I] := I;
406+
end;
407+
408+
function TTestRttiAccess.GetStaticArray: TStaticArray;
409+
var
410+
I: Integer;
411+
begin
412+
for I := 0 to Length(Result) - 1 do
413+
Result[I] := I;
414+
end;
415+
372416
procedure TTestRttiAccess.SellFruits(const AFruits: TFruitDynArray);
373417
var
374418
Fruit: TFruit;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /