Skip to content

Commit

Permalink
Merge pull request #1 from DynamoDS/master
Browse files Browse the repository at this point in the history
update master
  • Loading branch information
Astul-Betizagasti authored Jul 6, 2020
2 parents 7f2c8ed + a898bd0 commit 1319780
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/DynamoCore/Configuration/ExecutionSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public ExecutionSession(Scheduler.UpdateGraphAsyncTask updateTask, DynamoModel m
parameters[ParameterKeys.MinorVersion] = pathManager.MinorFileVersion;
parameters[ParameterKeys.NumberFormat] = model.PreferenceSettings.NumberFormat;
parameters[ParameterKeys.LastExecutionDuration] = new TimeSpan(updateTask.ExecutionEndTime.TickCount - updateTask.ExecutionStartTime.TickCount);
parameters[ParameterKeys.PackagePaths] = pathManager.PackagesDirectories;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/Controls/TooltipWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Padding="10">
<Grid Width="325">
<Grid.RowDefinitions>
<RowDefinition Height="96"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
Expand Down
19 changes: 18 additions & 1 deletion src/Libraries/DSCPython/CPythonEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,24 @@ public static DataMarshaler OutputMarshaler
}
}

return outputMarshaler.Marshal(pyObj.AsManagedObject(typeof(object)));
var unmarshalled = pyObj.AsManagedObject(typeof(object));
if (unmarshalled is PyObject)
{
using (unmarshalled as PyObject)
{
if (unmarshalled.Equals(pyObj))
{
// Object can't be unmarshalled. Prevent a stack overflow.
throw new InvalidOperationException(Properties.Resources.FailedToUnmarshalOutput);
}
else
{
return outputMarshaler.Marshal(unmarshalled);
}
}
}

return outputMarshaler.Marshal(unmarshalled);
});
}
return outputMarshaler;
Expand Down
9 changes: 9 additions & 0 deletions src/Libraries/DSCPython/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Libraries/DSCPython/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="FailedToUnmarshalOutput" xml:space="preserve">
<value>Output could not be converted to a .NET value</value>
</data>
<data name="InternalErrorTraceBackInfo" xml:space="preserve">
<value>Internal error, please report: Trace back information could not be extracted</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions src/Libraries/DSCPython/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="FailedToUnmarshalOutput" xml:space="preserve">
<value>Output could not be converted to a .NET value</value>
</data>
<data name="InternalErrorTraceBackInfo" xml:space="preserve">
<value>Internal error, please report: Trace back information could not be extracted</value>
</data>
Expand Down
8 changes: 7 additions & 1 deletion src/NodeServices/ExecutionSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IExecutionSession
/// <returns>True if the file is found</returns>
bool ResolveFilePath(ref string filepath);
}

//TODO(DYN-2879) make this class static for Dynamo 3.
/// <summary>
/// List of possible session parameter keys to obtain the session parameters.
/// </summary>
Expand Down Expand Up @@ -69,5 +69,11 @@ public class ParameterKeys
/// The duration of an execution covered by an <see cref="IExecutionSession"/>
/// </summary>
public static readonly string LastExecutionDuration = "LastExecutionDuration";

/// <summary>
/// The current collection of packagepaths that Dynamo is loading packages from.
/// The Return value is IEnumerable
/// </summary>
public static readonly string PackagePaths = nameof(PackagePaths);
}
}
18 changes: 17 additions & 1 deletion test/DynamoCoreTests/Configuration/ExecutionSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Dynamo.Tests.Configuration
class ExecutionSessionTests : DynamoModelTestBase
{
private TimeSpan lastExecutionDuration = new TimeSpan();
private IEnumerable<string> packagePaths;

protected override void GetLibrariesToPreload(List<string> libraries)
{
Expand All @@ -35,6 +36,22 @@ public void TestExecutionSession()
Assert.IsNotNull(lastExecutionDuration);
}

[Test]
[Category("UnitTests")]
public void TestExecutionSessionPackagePaths()
{
ExecutionEvents.GraphPreExecution += ExecutionEvents_GraphPreExecution;
RunModel(@"core\HomogeneousList\HomogeneousInputsValid.dyn");
Assert.IsNotEmpty(packagePaths, "packgePaths was empty");
ExecutionEvents.GraphPreExecution -= ExecutionEvents_GraphPreExecution;
}

private void ExecutionEvents_GraphPreExecution(Session.IExecutionSession session)
{
packagePaths = ExecutionEvents.ActiveSession.GetParameterValue(Session.ParameterKeys.PackagePaths) as IEnumerable<string>;

}

[TestFixtureTearDown]
public void TearDown()
{
Expand All @@ -44,7 +61,6 @@ public void TearDown()
private void ExecutionEvents_GraphPostExecution(Session.IExecutionSession session)
{
lastExecutionDuration = (TimeSpan)session.GetParameterValue(Session.ParameterKeys.LastExecutionDuration);

Assert.IsNotNull(session.GetParameterKeys());

var filepath = "ExecutionEvents.dyn";
Expand Down
5 changes: 5 additions & 0 deletions test/Engine/FFITarget/DummyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,10 @@ public static IDictionary AcceptTypedDictionary(Dictionary<string, DummyCollecti
{
return dictionary;
}

public static int[] MakeArray(params int[] values)
{
return values;
}
}
}
25 changes: 25 additions & 0 deletions test/Libraries/DynamoPythonTests/PythonEditTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,30 @@ public void TestWorkspaceWithMultiplePythonEngines()
Assert.IsTrue(ViewModel.Model.CurrentWorkspace.HasUnsavedChanges);
AssertPreviewValue(pythonNode2GUID, new List<string> { "2.7.9", "2.7.9" });
}

[Test]

public void Python_CanReferenceDynamoServicesExecutionSession()
{
// open test graph
var examplePath = Path.Combine(TestDirectory, @"core\python", "python_refDynamoServices.dyn");
ViewModel.OpenCommand.Execute(examplePath);

var guid = "296e339254e845b695caa1a116500be0";

var nodeModel = ViewModel.Model.CurrentWorkspace.NodeFromWorkspace(guid);
var pynode = nodeModel as PythonNode;
var count = 0;
foreach (var pythonEngine in GetPythonEnginesList())
{
UpdatePythonEngineAndRun(pynode, pythonEngine);

ViewModel.HomeSpace.Run();
count++;
Assert.AreEqual(count, (GetModel().CurrentWorkspace as HomeWorkspaceModel).EvaluationCount);

AssertPreviewCount(guid, 2);
}
}
}
}
27 changes: 27 additions & 0 deletions test/Libraries/DynamoPythonTests/PythonEvalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,32 @@ public void CPythonEngineIncludesTraceBack()
Assert.AreEqual(@"SyntaxError : ('invalid syntax', ('<string>', 3, 7, ""print 'hello'\n""))", exc.Message);
}
}

[Test]
public void UnsupportedOutputShouldFailGracefullyInCPython()
{
var code = @"
import weakref
class myobj:
pass
o = myobj()
wr = weakref.ref(o)
OUT = wr
";
var empty = new ArrayList();

try
{
DSCPython.CPythonEvaluator.EvaluatePythonScript(code, empty, empty);
Assert.Fail("An exception was expected");
}
catch (Exception exc)
{
Assert.AreEqual("Output could not be converted to a .NET value", exc.Message);
}
}
}
}
160 changes: 160 additions & 0 deletions test/Libraries/DynamoPythonTests/PythonEvalTestsWithLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class PythonEvalTestsWithLibraries : DynamoModelTestBase
protected override void GetLibrariesToPreload(List<string> libraries)
{
libraries.Add("FFITarget.dll");
libraries.Add("DSCoreNodes.dll");
}

public IEnumerable<PythonEvaluatorDelegate> Evaluators = new List<PythonEvaluatorDelegate> {
Expand Down Expand Up @@ -43,5 +44,164 @@ from FFITarget import DummyMath
Assert.AreEqual(expected, result);
}
}

[Test]
[Category("Failure")]
public void TestListEncoding()
{
string code = @"
import sys
import clr
clr.AddReference('DSCoreNodes')
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)
OUT = l
";
var empty = new ArrayList();
var expected = new ArrayList { "a", "b", "c" };
foreach (var pythonEvaluator in Evaluators)
{
var result = pythonEvaluator(code, empty, empty);
Assert.IsTrue(result is IEnumerable);
CollectionAssert.AreEqual(expected, result as IEnumerable);
}
}

[Test]
public void TestArrayEncoding()
{
string code = @"
import sys
import clr
clr.AddReference('FFITarget')
clr.AddReference('DSCoreNodes')
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
a = DummyCollection.MakeArray(1,2)
a[0] = a[1] + 1
b = len(a)
a[1] = b
a = List.AddItemToEnd(1, a)
OUT = a
";
var empty = new ArrayList();
var expected = new ArrayList { 3, 2, 1 };
foreach (var pythonEvaluator in Evaluators)
{
var result = pythonEvaluator(code, empty, empty);
Assert.IsTrue(result is IEnumerable);
CollectionAssert.AreEqual(expected, result as IEnumerable);
}
}

[Test]
[Category("Failure")]
public void TestTupleEncoding()
{
string code = @"
import sys
import clr
clr.AddReference('FFITarget')
clr.AddReference('DSCoreNodes')
from FFITarget import DummyCollection
from DSCore import List
t = (1,2,3)
# Python tuple => .NET array - Works in both
a = DummyCollection.MakeArray(t)
# Python tuple => .NET IList - Does not work in CPython
l = List.AddItemToEnd(4, t)
OUT = a, l
";
var empty = new ArrayList();
var expected = new ArrayList { new ArrayList { 1, 2, 3 }, new ArrayList { 1, 2, 3, 4 } };
foreach (var pythonEvaluator in Evaluators)
{
var result = pythonEvaluator(code, empty, empty);
Assert.IsTrue(result is IEnumerable);
CollectionAssert.AreEqual(expected, result as IEnumerable);
}
}

[Test]
[Category("Failure")]
public void TestRangeEncoding()
{
string code = @"
import sys
import clr
clr.AddReference('FFITarget')
clr.AddReference('DSCoreNodes')
from FFITarget import DummyCollection
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)
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);
}
}

[Test]
[Category("Failure")]
public void TestDictionaryEncoding()
{
string code = @"
import sys
import clr
clr.AddReference('FFITarget')
clr.AddReference('DesignScriptBuiltin')
from DesignScript.Builtin import Dictionary
from FFITarget import DummyCollection
d = {'one': 1, 'two': 2, 'three': 3}
# Python dict => DS Dictionary - Does not work in either engine
# d2 = Dictionary.SetValueAtKeys(d, ['four'], [4])
# Python dict => .NET IDictionary - Does not work in CPython
d = DummyCollection.AcceptIDictionary(d)
# .NET IDictionary => Python dict - Works in IronPython. Could not test in CPython due to previous bug
d['five'] = 5
OUT = d
";
var empty = new ArrayList();
var expected = new Dictionary<string, int> {
{ "one", 1 }, { "two", 2 }, { "three", 3 }, { "five", 5 }
};
foreach (var pythonEvaluator in Evaluators)
{
var result = pythonEvaluator(code, empty, empty);
Assert.IsTrue(result is IDictionary);
CollectionAssert.AreEquivalent(expected, result as IDictionary);
}
}
}
}
Loading

0 comments on commit 1319780

Please sign in to comment.