Skip to content

Return interface objects when iterating over interface collections #1257

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

Merged
merged 1 commit into from
Oct 12, 2020
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
19 changes: 17 additions & 2 deletions src/runtime/classbase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -184,7 +185,6 @@ public static IntPtr tp_iter(IntPtr ob)

var e = co.inst as IEnumerable;
IEnumerator o;

if (e != null)
{
o = e.GetEnumerator();
Expand All @@ -199,7 +199,22 @@ public static IntPtr tp_iter(IntPtr ob)
}
}

return new Iterator(o).pyHandle;
var elemType = typeof(object);
var iterType = co.inst.GetType();
foreach(var ifc in iterType.GetInterfaces())
{
if (ifc.IsGenericType)
{
var genTypeDef = ifc.GetGenericTypeDefinition();
if (genTypeDef == typeof(IEnumerable<>) || genTypeDef == typeof(IEnumerator<>))
{
elemType = ifc.GetGenericArguments()[0];
break;
}
}
}

return new Iterator(o, elemType).pyHandle;
}


Expand Down
6 changes: 4 additions & 2 deletions src/runtime/iterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ namespace Python.Runtime
internal class Iterator : ExtensionType
{
private IEnumerator iter;
private Type elemType;

public Iterator(IEnumerator e)
public Iterator(IEnumerator e, Type elemType)
{
iter = e;
this.elemType = elemType;
}


Expand Down Expand Up @@ -41,7 +43,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.elemType);
}

public static IntPtr tp_iter(IntPtr ob)
Expand Down
16 changes: 16 additions & 0 deletions src/tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,19 @@ def test_implementation_access():
assert 100 == i.__implementation__
assert clrVal == i.__raw_implementation__
assert i.__implementation__ != i.__raw_implementation__


def test_interface_collection_iteration():
"""Test interface type is used when iterating over interface collection"""
import System
from System.Collections.Generic import List
elem = System.IComparable(System.Int32(100))
typed_list = List[System.IComparable]()
typed_list.Add(elem)
for e in typed_list:
assert type(e).__name__ == "IComparable"

untyped_list = System.Collections.ArrayList()
untyped_list.Add(elem)
for e in untyped_list:
assert type(e).__name__ == "int"