Skip to content

Commit f1df348

Browse files
committed
Try to guess the correct element type from an enumerator, use in conversion
1 parent 8df73d3 commit f1df348

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/runtime/converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ internal static IntPtr ToPython(object value, Type type)
155155
// type is. we'd rather have the object bound to the actual
156156
// implementing class.
157157

158-
type = value.GetType();
158+
type = type ?? value.GetType();
159159

160160
TypeCode tc = Type.GetTypeCode(type);
161161

src/runtime/iterator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using System;
23
using System.Collections;
34

@@ -10,10 +11,18 @@ namespace Python.Runtime
1011
internal class Iterator : ExtensionType
1112
{
1213
private IEnumerator iter;
14+
private Type type;
1315

1416
public Iterator(IEnumerator e)
1517
{
1618
iter = e;
19+
20+
var genericType = e.GetType().GetInterfaces().FirstOrDefault(
21+
x => x.IsGenericType &&
22+
x.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IEnumerator<>)
23+
);
24+
25+
type = genericType?.GetGenericArguments().FirstOrDefault();
1726
}
1827

1928

@@ -41,7 +50,7 @@ public static IntPtr tp_iternext(IntPtr ob)
4150
return IntPtr.Zero;
4251
}
4352
object item = self.iter.Current;
44-
return Converter.ToPythonImplicit(item);
53+
return Converter.ToPython(item, type);
4554
}
4655

4756
public static IntPtr tp_iter(IntPtr ob)

0 commit comments

Comments
 (0)