Skip to content

Drop List and IEnumerable conversions #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
37 changes: 3 additions & 34 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion src/runtime/iterator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System;
using System.Collections;

Expand All @@ -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();
}


Expand Down Expand Up @@ -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)
Expand Down