Skip to content

Commit b00a2e8

Browse files
cursoragentipetrov
andcommitted
Add test for nested list decoding and improve ListEncoderDecoder
Co-authored-by: ipetrov <[email protected]>
1 parent c0b315f commit b00a2e8

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

DSPythonNet3/DSPythonNet3.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<ProjectGuid>{F1541C2D-80A9-4FE7-8D9E-75A8B9FF3479}</ProjectGuid>
44
<OutputType>Library</OutputType>
@@ -10,6 +10,7 @@
1010
<ItemGroup>
1111
<InternalsVisibleTo Include="PythonMigrationViewExtension" />
1212
<InternalsVisibleTo Include="DynamoPythonTests" />
13+
<InternalsVisibleTo Include="DSPythonNet3Tests" />
1314

1415
<PackageReference Include="DynamoVisualProgramming.Core" Version="$(DynamoPackageVersion)" ExcludeAssets="runtime" />
1516
<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="$(DynamoPackageVersion)" ExcludeAssets="runtime" />

DSPythonNet3/Encoders/ListEncodeDecoder.cs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
2828
return false;
2929
}
3030

31-
using (var pyList = PyList.AsList(pyObj))
31+
if (typeof(T).IsGenericType)
3232
{
33-
if (typeof(T).IsGenericType)
33+
using (var pyList = PyList.AsList(pyObj))
3434
{
3535
value = pyList.ToList<T>();
3636
}
37-
else
38-
{
39-
value = (T)pyList.ToList();
40-
}
4137
return true;
4238
}
39+
40+
var converted = ConvertToArrayList(pyObj);
41+
value = (T)converted;
42+
return true;
4343
}
4444

4545
public PyObject TryEncode(object value)
@@ -57,5 +57,54 @@ bool IPyObjectDecoder.CanDecode(PyType objectType, Type targetType)
5757
}
5858
return decodableTypes.IndexOf(targetType) >= 0;
5959
}
60+
61+
private static IList ConvertToArrayList(PyObject pyObj)
62+
{
63+
using var pyList = PyList.AsList(pyObj);
64+
var result = new ArrayList();
65+
foreach (PyObject item in pyList)
66+
{
67+
using (item)
68+
{
69+
result.Add(ConvertItem(item));
70+
}
71+
}
72+
73+
return result;
74+
}
75+
76+
private static object ConvertItem(PyObject item)
77+
{
78+
if (TryGetClrObject(item, out var clrObject))
79+
{
80+
return clrObject;
81+
}
82+
83+
if (PyString.IsStringType(item))
84+
{
85+
return item.AsManagedObject(typeof(string));
86+
}
87+
88+
if (PyList.IsListType(item) || PyTuple.IsTupleType(item))
89+
{
90+
return ConvertToArrayList(item);
91+
}
92+
93+
return item.AsManagedObject(typeof(object));
94+
}
95+
96+
private static bool TryGetClrObject(PyObject pyObj, out object clrObject)
97+
{
98+
try
99+
{
100+
clrObject = pyObj.GetManagedObject();
101+
return true;
102+
}
103+
catch
104+
{
105+
clrObject = null;
106+
return false;
107+
}
108+
}
60109
}
61110
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections;
2+
using DSPythonNet3;
3+
using DSPythonNet3.Encoders;
4+
using NUnit.Framework;
5+
using Python.Runtime;
6+
7+
namespace DSPythonNet3Tests
8+
{
9+
public class ListEncoderDecoderTests
10+
{
11+
[Test]
12+
public void TryDecode_ConvertsNestedPythonListsToClrLists()
13+
{
14+
DSPythonNet3Evaluator.InitializePython();
15+
16+
using (Py.GIL())
17+
using (var scope = Py.CreateScope())
18+
{
19+
scope.Exec("value = [[1, [2, 3]], ['a', ['b']]]");
20+
using var pyList = scope.Get("value");
21+
22+
var decoder = new ListEncoderDecoder();
23+
var success = decoder.TryDecode(pyList, out IList result);
24+
25+
Assert.That(success, Is.True);
26+
Assert.That(result, Is.InstanceOf<IList>());
27+
28+
var first = result[0] as IList;
29+
Assert.That(first, Is.Not.Null);
30+
Assert.That(first[0], Is.EqualTo(1));
31+
32+
var secondLevel = first?[1] as IList;
33+
Assert.That(secondLevel, Is.Not.Null);
34+
Assert.That(secondLevel?[0], Is.EqualTo(2));
35+
Assert.That(secondLevel?[1], Is.EqualTo(3));
36+
37+
var second = result[1] as IList;
38+
Assert.That(second, Is.Not.Null);
39+
Assert.That(second?[0], Is.EqualTo("a"));
40+
41+
var thirdLevel = second?[1] as IList;
42+
Assert.That(thirdLevel, Is.Not.Null);
43+
Assert.That(thirdLevel?[0], Is.EqualTo("b"));
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)