Skip to content

Commit 3f2f677

Browse files
patstewtonyroberts
authored andcommitted
Convert IEnumerables to python lists.
1 parent acb1fac commit 3f2f677

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pythonnet/src/runtime/converter.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Runtime.InteropServices;
1313
using System.Globalization;
1414
using System.Security;
15+
using System.Collections;
1516

1617
namespace Python.Runtime {
1718

@@ -91,6 +92,16 @@ internal static IntPtr ToPython(Object value, Type type) {
9192
return result;
9293
}
9394

95+
if (value is IEnumerable)
96+
{
97+
var resultlist = new PyList();
98+
foreach (object o in (IEnumerable)value)
99+
{
100+
resultlist.Append(new PyObject(ToPython(o, o.GetType())));
101+
}
102+
return resultlist.Handle;
103+
}
104+
94105
// hmm - from Python, we almost never care what the declared
95106
// type is. we'd rather have the object bound to the actual
96107
// implementing class.
@@ -738,10 +749,13 @@ static bool ToEnum(IntPtr value, Type obType, out Object result,
738749
return false;
739750

740751
}
741-
742-
743-
744752
}
745753

746-
754+
public static class ConverterExtension
755+
{
756+
public static PyObject ToPython(this object o)
757+
{
758+
return new PyObject(Converter.ToPython(o, o.GetType()));
759+
}
760+
}
747761
}

0 commit comments

Comments
 (0)