diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index 5d8769a73..789748436 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -135,22 +135,6 @@ internal static IntPtr ToPython(object value, Type type) return result; } - if (value is IList && !(value is INotifyPropertyChanged) && value.GetType().IsGenericType) - { - using (var resultlist = new PyList()) - { - foreach (object o in (IEnumerable)value) - { - using (var p = new PyObject(ToPython(o, o?.GetType()))) - { - resultlist.Append(p); - } - } - Runtime.XIncref(resultlist.Handle); - return resultlist.Handle; - } - } - // it the type is a python subclass of a managed type then return the // underlying python object rather than construct a new wrapper object. var pyderived = value as IPythonDerivedType; @@ -171,7 +155,7 @@ internal static IntPtr ToPython(object value, Type type) // type is. we'd rather have the object bound to the actual // implementing class. - type = value.GetType(); + type = type ?? value.GetType(); TypeCode tc = Type.GetTypeCode(type); @@ -231,21 +215,6 @@ internal static IntPtr ToPython(object value, Type type) return Runtime.PyLong_FromUnsignedLongLong((ulong)value); default: - if (value is IEnumerable) - { - using (var resultlist = new PyList()) - { - foreach (object o in (IEnumerable)value) - { - using (var p = new PyObject(ToPython(o, o?.GetType()))) - { - resultlist.Append(p); - } - } - Runtime.XIncref(resultlist.Handle); - return resultlist.Handle; - } - } result = CLRObject.GetInstHandle(value, type); return result; } @@ -862,7 +831,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s var listType = typeof(List<>); var constructedListType = listType.MakeGenericType(elementType); - IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) : + IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) : (IList) Activator.CreateInstance(constructedListType); IntPtr item; @@ -883,7 +852,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s items = Array.CreateInstance(elementType, list.Count); list.CopyTo(items, 0); - + result = items; return true; } diff --git a/src/runtime/iterator.cs b/src/runtime/iterator.cs index f9cf10178..a42f5e3af 100644 --- a/src/runtime/iterator.cs +++ b/src/runtime/iterator.cs @@ -1,3 +1,4 @@ +using System.Linq; using System; using System.Collections; @@ -10,10 +11,18 @@ namespace Python.Runtime internal class Iterator : ExtensionType { private IEnumerator iter; + private Type type; public Iterator(IEnumerator e) { iter = e; + + var genericType = e.GetType().GetInterfaces().FirstOrDefault( + x => x.IsGenericType && + x.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IEnumerator<>) + ); + + type = genericType?.GetGenericArguments().FirstOrDefault(); } @@ -41,7 +50,7 @@ public static IntPtr tp_iternext(IntPtr ob) return IntPtr.Zero; } object item = self.iter.Current; - return Converter.ToPythonImplicit(item); + return Converter.ToPython(item, self.type); } public static IntPtr tp_iter(IntPtr ob)