Skip to content

Commit

Permalink
more consistency in marshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
eirannejad committed Dec 16, 2023
1 parent 4de4208 commit a99a258
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/runtime/McNeel.PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ PyModule PrepareScope(string scopeName, string pythonFile)
#endregion

#region Marshalling
// NOTE:
// ensures incoming data is marshalled into
// closest-matching Python objects
public PyObject MarshInput(object value)
{
switch (value)
Expand All @@ -583,9 +586,9 @@ public PyObject MarshInput(object value)
}
return pyDict;

case IEnumerable enumerable:
case IList list:
PyList pyList = new PyList();
foreach (object obj in enumerable)
foreach (object obj in list)
{
pyList.Append(MarshInput(obj));
}
Expand All @@ -604,17 +607,25 @@ PyObject MarshToPyObject(object value)
return PyObject.FromNullableReference(_value.Borrow());
}

// NOTE:
// ensures outgoing data is marshalled into
// closest-matching dotnet objects
public object MarshOutput(object value)
{
switch (value)
{
case object[] array:
return array.Select(i => MarshOutput(i)).ToArray();

case List<object> enumerable:
return enumerable.Select(i => MarshOutput(i)).ToList();
case IList list:
var dotnetList = new List<object>();
foreach (object obj in dotnetList)
{
dotnetList.Append(MarshOutput(obj));
}
return dotnetList;

case Dictionary<object, object> dict:
case IDictionary<object, object> dict:
return dict.Select(p =>
{
return new KeyValuePair<object, object>(MarshOutput(p.Key), MarshOutput(p.Value));
Expand Down

0 comments on commit a99a258

Please sign in to comment.