@@ -1937,18 +1937,25 @@ function ValidateClassRef(PyValue: PPyObject; RefClass: TClass;
1937
1937
end ;
1938
1938
1939
1939
{ $IFDEF EXTENDED_RTTI}
1940
- function DynArrayToPython (const Value : TValue): PPyObject;
1940
+ function DynArrayToPython (const Value : TValue; DelphiWrapper: TPyDelphiWrapper;
1941
+ out ErrMsg: string): PPyObject;
1941
1942
var
1942
1943
I: Integer;
1943
- V: Variant;
1944
1944
PyEngine: TPythonEngine;
1945
+ PyObj: PPyObject;
1945
1946
begin
1946
1947
PyEngine := GetPythonEngine();
1947
1948
Result := PyEngine.PyList_New(Value .GetArrayLength);
1948
1949
for I := 0 to Value .GetArrayLength() - 1 do
1949
1950
begin
1950
- V := Value .GetArrayElement(i).AsVariant;
1951
- PyEngine.PyList_SetItem(Result, I, PyEngine.VariantAsPyObject(V));
1951
+ PyObj := TValueToPyObject(Value .GetArrayElement(i), DelphiWrapper, ErrMsg);
1952
+ if not Assigned(PyObj) then
1953
+ begin
1954
+ PyEngine.Py_DECREF(Result);
1955
+ Result := nil ;
1956
+ Break;
1957
+ end ;
1958
+ PyEngine.PyList_SetItem(Result, I, PyObj);
1952
1959
end ;
1953
1960
end ;
1954
1961
@@ -1986,14 +1993,8 @@ function SimpleValueToPython(const Value: TValue; out ErrMsg: string): PPyObject
1986
1993
Result := SetToPython(Value .TypeData.CompType^,
1987
1994
PInteger(Value .GetReferenceToRawData)^);
1988
1995
end ;
1989
- tkArray, tkDynArray:
1990
- Result := DynArrayToPython(Value );
1991
- tkClass, tkMethod,
1992
- tkRecord, tkInterface, { $IFDEF MANAGED_RECORD} tkMRecord,{ $ENDIF}
1993
- tkClassRef, tkPointer, tkProcedure:
1994
- ErrMsg := rs_ErrValueToPython;
1995
1996
else
1996
- ErrMsg := rs_ErrUnexpected ;
1997
+ ErrMsg := rs_ErrValueToPython ;
1997
1998
end ;
1998
1999
except
1999
2000
on E: Exception do begin
@@ -2128,9 +2129,10 @@ function ValidateDynArray(PyValue: PPyObject; const RttiType: TRttiType;
2128
2129
Arr: array of TValue;
2129
2130
I: Integer;
2130
2131
elType: PPTypeInfo;
2131
- V: Variant;
2132
- Num: Int64;
2133
2132
PyEngine: TPythonEngine;
2133
+ RttiContext: TRttiContext;
2134
+ ElementType: TRttiType;
2135
+ ArrElem: PPyObject;
2134
2136
begin
2135
2137
Result := False;
2136
2138
PyEngine := GetPythonEngine;
@@ -2149,18 +2151,17 @@ function ValidateDynArray(PyValue: PPyObject; const RttiType: TRttiType;
2149
2151
if elType = nil then
2150
2152
Exit;
2151
2153
2154
+ ElementType := RttiContext.GetType(elType^);
2155
+ if ElementType = nil then
2156
+ Exit;
2157
+
2152
2158
try
2153
2159
SetLength(Arr, PyEngine.PySequence_Length(PyValue));
2154
2160
for I := 0 to PyEngine.PySequence_Length(PyValue) - 1 do
2155
2161
begin
2156
- V := PyEngine.GetSequenceItem(PyValue, i);
2157
- if elType^.Kind = tkEnumeration then
2158
- begin
2159
- Num := TValue.FromVariant(V).Cast(TypeInfo(Int64)).AsInt64;
2160
- Arr[i] := TValue.FromOrdinal(elType^, Num);
2161
- end
2162
- else
2163
- Arr[i] := TValue.FromVariant(V).Cast(elType^);
2162
+ ArrElem := PyEngine.PySequence_GetItem(PyValue, I);
2163
+ if not PyObjectToTValue(ArrElem, ElementType, Arr[I], ErrMsg) then
2164
+ Break;
2164
2165
end ;
2165
2166
ParamValue := TValue.FromArray(RttiType.Handle, Arr);
2166
2167
Result := True;
@@ -2169,7 +2170,7 @@ function ValidateDynArray(PyValue: PPyObject; const RttiType: TRttiType;
2169
2170
end ;
2170
2171
end ;
2171
2172
2172
- function PyObjectToTValue (PyArg : PPyObject; ArgType: TRttiType;
2173
+ function PyObjectToTValue (PyArg: PPyObject; ArgType: TRttiType;
2173
2174
out Arg: TValue; out ErrMsg: string): Boolean;
2174
2175
var
2175
2176
Obj: TObject;
@@ -2245,6 +2246,8 @@ function TValueToPyObject(const Value: TValue;
2245
2246
tkInterface: Result := DelphiWrapper.WrapInterface(Value );
2246
2247
tkRecord{ $IFDEF MANAGED_RECORD} ,tkMRecord{ $ENDIF} :
2247
2248
Result := DelphiWrapper.WrapRecord(Value );
2249
+ tkArray, tkDynArray:
2250
+ Result := DynArrayToPython(Value , DelphiWrapper, ErrMsg);
2248
2251
else
2249
2252
Result := SimpleValueToPython(Value , ErrMsg);
2250
2253
end ;
0 commit comments