Skip to content

Commit

Permalink
Merge pull request #3 from DynamoDS/master
Browse files Browse the repository at this point in the history
Update master
  • Loading branch information
Astul-Betizagasti authored Jul 9, 2020
2 parents e446a29 + 555a01c commit efee990
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 33 deletions.
Binary file modified extern/Python/Python.Runtime.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public override void Save(string newPath, bool isBackup = false, EngineControlle
base.Save(newPath, isBackup, engine);
}

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
protected override bool PopulateXmlDocument(XmlDocument document)
{
if (!base.PopulateXmlDocument(document))
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ internal void StopPeriodicEvaluation()

#endregion

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
protected override bool PopulateXmlDocument(XmlDocument document)
{
if (!base.PopulateXmlDocument(document))
Expand Down
2 changes: 2 additions & 0 deletions src/DynamoCore/Graph/Workspaces/WorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ internal bool containsInvalidInputSymbols()
return this.Nodes.OfType<Nodes.CustomNodes.Symbol>().Any(node => !node.Parameter.NameIsValid);
}

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
private void SerializeElementResolver(XmlDocument xmlDoc)
{
Debug.Assert(xmlDoc != null);
Expand All @@ -1520,6 +1521,7 @@ private void SerializeElementResolver(XmlDocument xmlDoc)
root.AppendChild(mapElement);
}

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
protected virtual bool PopulateXmlDocument(XmlDocument xmlDoc)
{
try
Expand Down
22 changes: 20 additions & 2 deletions src/DynamoCoreWpf/Views/Core/NodeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,28 @@
</Rectangle.ToolTip>
</Rectangle>

<Border Name="previewState"
<Border Name="previewStateNameBlock"
Grid.Row="1"
Grid.ColumnSpan="3"
Grid.RowSpan="3"
Grid.RowSpan="1"
Canvas.ZIndex="12"
Margin="1,1,1,1"
Background="#99B8CA"
Opacity=".4"
Style="{StaticResource SZoomFadePreview}"
MouseDown="NameBlock_OnMouseDown">
<Border.Visibility>
<Binding Path="IsVisible"
UpdateSourceTrigger="PropertyChanged"
Mode="OneWay"
Converter="{StaticResource InverseBoolToVisibilityConverter}">
</Binding>
</Border.Visibility>
</Border>
<Border Name="previewState"
Grid.Row="2"
Grid.ColumnSpan="3"
Grid.RowSpan="2"
Canvas.ZIndex="12"
Margin="1,1,1,1"
Background="#99B8CA"
Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/DSCPython/CPythonEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static object EvaluatePythonScript(
/// </summary>
private static void InitializeEncoders()
{
var encoders = new IPyObjectEncoder[] { new BigIntegerEncoder() };
var encoders = new IPyObjectEncoder[] { new BigIntegerEncoder(), new ListEncoder() };
var decoders = encoders.Cast<IPyObjectDecoder>().ToArray();
Array.ForEach(encoders, e => PyObjectConversions.RegisterEncoder(e));
Array.ForEach(decoders, d => PyObjectConversions.RegisterDecoder(d));
Expand Down
1 change: 1 addition & 0 deletions src/Libraries/DSCPython/DSCPython.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</Compile>
<Compile Include="Encoders\BigIntegerEncoder.cs" />
<Compile Include="CPythonEvaluator.cs" />
<Compile Include="Encoders\ListEncoder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
60 changes: 60 additions & 0 deletions src/Libraries/DSCPython/Encoders/ListEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Dynamo.Utilities;
using Python.Runtime;

namespace DSCPython.Encoders
{
internal class ListEncoder : IPyObjectEncoder, IPyObjectDecoder
{
private static readonly Type[] decodableTypes = new Type[]
{
typeof(IList), typeof(ArrayList),
typeof(IList<>), typeof(List<>)
};

public bool CanDecode(PyObject objectType, Type targetType)
{
if (targetType.IsGenericType)
{
targetType = targetType.GetGenericTypeDefinition();
}
return decodableTypes.IndexOf(targetType) >= 0;
}

public bool CanEncode(Type type)
{
return typeof(IList).IsAssignableFrom(type);
}

public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (!PySequence.IsSequenceType(pyObj))
{
value = default;
return false;
}

using (var pyList = PyList.AsList(pyObj))
{
if (typeof(T).IsGenericType)
{
value = pyList.ToList<T>();
}
else
{
value = (T)pyList.ToList();
}
return true;
}
}

public PyObject TryEncode(object value)
{
// This is a no-op to prevent Python.NET from encoding generic lists
// https://github.com/pythonnet/pythonnet/pull/963#issuecomment-642938541
return PyObject.FromManagedObject(value);
}
}
}
64 changes: 35 additions & 29 deletions test/Libraries/DynamoPythonTests/PythonEvalTestsWithLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override void GetLibrariesToPreload(List<string> libraries)
};

[Test]
public void TestBigIntegerEncoding()
public void TestBigIntegerEncodingDecoding()
{
string code = @"
import sys
Expand All @@ -46,8 +46,7 @@ from FFITarget import DummyMath
}

[Test]
[Category("Failure")]
public void TestListEncoding()
public void TestListDecoding()
{
string code = @"
import sys
Expand All @@ -56,16 +55,32 @@ import clr
from DSCore import List
l = ['a']
# Python list => .NET IList - Does not work in CPython
l = List.AddItemToEnd('b',l)
# .NET IList => Python list - Does not work in IronPython. Couldn't test CPython because of previous bug
# l.append('c')
l.Add(c)
# Python list => .NET IList
untypedList = List.AddItemToEnd('b', l)
untypedList.Add('c')
l2 = ['a','b']
# Python list => .NET IList<>
typedList = List.SetDifference(l2, l)
typedList.Add('b')
l3 = [[1,2],[3,4]]
# Python list (nested) => .NET IList<IList<>>
flatennedList = List.Flatten(l3)
l4 = []
# Python list (empty) => .NET IList
elementCount = List.Count(l4)
OUT = l
sum = 0
# Python-wrapped .NET List can be iterated over
for i in flatennedList:
sum = sum + i
OUT = untypedList, typedList, flatennedList, elementCount, sum
";
var empty = new ArrayList();
var expected = new ArrayList { "a", "b", "c" };
var expected = new ArrayList { new ArrayList { "a", "b", "c" }, new ArrayList { "b", "b" }, new ArrayList { 1, 2, 3, 4 }, 0, 10 };
foreach (var pythonEvaluator in Evaluators)
{
var result = pythonEvaluator(code, empty, empty);
Expand All @@ -86,11 +101,7 @@ from FFITarget import DummyCollection
from DSCore import List
from array import array
# Python array => .NET IList - array is in a builtin library. This does not work in either engine
# native = array('l', [1,2])
# native = List.AddItemToEnd(3, native)
# .NET array => Python list - Works in both engines
# .NET array => Python list
a = DummyCollection.MakeArray(1,2)
a[0] = a[1] + 1
b = len(a)
Expand All @@ -110,8 +121,7 @@ from array import array
}

[Test]
[Category("Failure")]
public void TestTupleEncoding()
public void TestTupleDecoding()
{
string code = @"
import sys
Expand All @@ -122,9 +132,9 @@ from FFITarget import DummyCollection
from DSCore import List
t = (1,2,3)
# Python tuple => .NET array - Works in both
# Python tuple => .NET array
a = DummyCollection.MakeArray(t)
# Python tuple => .NET IList - Does not work in CPython
# Python tuple => .NET IList
l = List.AddItemToEnd(4, t)
OUT = a, l
Expand All @@ -140,8 +150,7 @@ from DSCore import List
}

[Test]
[Category("Failure")]
public void TestRangeEncoding()
public void TestRangeDecodingCPython()
{
string code = @"
import sys
Expand All @@ -154,19 +163,16 @@ from DSCore import List
r = range(0, 10, 2)
# Python range => .NET array - Works in both
a = DummyCollection.MakeArray(r)
# Python range => .NET IList - Does not work in CPython
l = List.AddItemToEnd(10, r)
# Python range => .NET IList - Does not work in IronPython
l = List.AddItemToEnd(10, range(0, 10, 2))
OUT = a, l
";
var empty = new ArrayList();
var expected = new ArrayList { new ArrayList { 0, 2, 4, 6, 8 }, new ArrayList { 0, 2, 4, 6, 8, 10 } };
foreach (var pythonEvaluator in Evaluators)
{
var result = pythonEvaluator(code, empty, empty);
Assert.IsTrue(result is IEnumerable);
CollectionAssert.AreEqual(expected, result as IEnumerable);
}
var result = DSCPython.CPythonEvaluator.EvaluatePythonScript(code, empty, empty);
Assert.IsTrue(result is IEnumerable);
CollectionAssert.AreEqual(expected, result as IEnumerable);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static List<string> getDynamoDefaultLibs()
"DesignScriptBuiltin.dll",
"DSCoreNodes.dll",
"DSOffice.dll",
"DSIronPython.dll",
"FunctionObject.ds",
"BuiltIn.ds",
"DynamoConversions.dll",
Expand Down

0 comments on commit efee990

Please sign in to comment.