Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Libraries/DSCPython/CPythonEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ internal delegate void EvaluationEventHandler(EvaluationState state,
[IsVisibleInDynamoLibrary(false)]
public static class CPythonEvaluator
{
private const string DynamoSkipAttributeName = "__dynamoskipconversion__";
static PyScope globalScope;
internal static readonly string globalScopeName = "global";
static CPythonEvaluator()
Expand Down Expand Up @@ -387,6 +388,12 @@ public static DataMarshaler OutputMarshaler
{
return outputMarshaler.Marshal(clrObj);
}

if (IsMarkedToSkipConversion(pyObj))
{
return GetDynamoCPythonHandle(pyObj);
}

// Dictionaries are iterable, so they should come first
if (PyDict.IsDictType(pyObj))
{
Expand Down Expand Up @@ -439,11 +446,7 @@ public static DataMarshaler OutputMarshaler
{
if (unmarshalled.Equals(pyObj))
{
var globalScope = PyScopeManager.Global.Get(globalScopeName);
//try moving object to global scope
globalScope.Set(pyObj.Handle.ToString(), pyObj);

return new DynamoCPythonHandle(pyObj.Handle);
return GetDynamoCPythonHandle(pyObj);
}
else
{
Expand All @@ -459,6 +462,18 @@ public static DataMarshaler OutputMarshaler
}
}

private static DynamoCPythonHandle GetDynamoCPythonHandle(PyObject pyObj)
{
var globalScope = PyScopeManager.Global.Get(globalScopeName);
globalScope.Set(pyObj.Handle.ToString(), pyObj);
return new DynamoCPythonHandle(pyObj.Handle);
}

private static bool IsMarkedToSkipConversion(PyObject pyObj)
{
return pyObj.HasAttr(DynamoSkipAttributeName);
}

private static DataMarshaler inputMarshaler;
private static DataMarshaler outputMarshaler;

Expand Down
44 changes: 44 additions & 0 deletions test/Libraries/DynamoPythonTests/PythonEvalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
using ProtoCore.Lang;

namespace DSPythonTests
{
Expand Down Expand Up @@ -212,6 +214,48 @@ def __str__(self):
});
}

[Test]
public void PythonObjectWithDynamoSkipisNotMarshaled()
{
var code = @"

class iterable:
def __str__(self):
return 'I want to participate in conversion'
def __iter__(self):
return iter([0,1,2,3])
def __getitem__(self,key):
return key

o = iterable()
OUT = o
";

var code2 = @"

class notiterable:
def __dynamoskipconversion__(self):
pass
def __str__(self):
return 'I want to skip in conversion'
def __iter__(self):
return iter([0,1,2,3])
def __getitem__(self,key):
return key

o = notiterable()
OUT = o
";
var empty = new ArrayList();
var result1 = DSCPython.CPythonEvaluator.EvaluatePythonScript(code, empty, empty);
var result2 = DSCPython.CPythonEvaluator.EvaluatePythonScript(code2, empty, empty);
Assert.IsInstanceOf(typeof(IList), result1);
Assert.IsTrue(new List<object>() { 0L, 1L, 2L, 3L }
.SequenceEqual((IEnumerable<Object>)result1));
Assert.IsInstanceOf(typeof(DSCPython.DynamoCPythonHandle),result2 );

}

[Test]
public void NonListIterablesCanBeOutput()
{
Expand Down