Repository navigation
Expand file tree
/
Copy pathIterator.cs
More file actions
61 lines (56 loc) · 1.77 KB
/
Copy pathIterator.cs
File metadata and controls
61 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections;
namespace Python.Runtime
{
/// <summary>
/// Implements a generic Python iterator for IEnumerable objects and
/// managed array objects. This supports 'for i in object:' in Python.
/// </summary>
internal class Iterator : ExtensionType
{
private readonly IEnumerator iter;
private readonly Type elemType;
public Iterator(IEnumerator e, Type elemType)
{
iter = e;
this.elemType = elemType;
}
/// <summary>
/// Implements support for the Python iteration protocol.
/// </summary>
public static NewReference tp_iternext(BorrowedReference ob)
{
var self = (Iterator)GetManagedObject(ob)!;
try
{
if (!self.iter.MoveNext())
{
Exceptions.SetError(Exceptions.StopIteration, Runtime.PyNone);
return default;
}
}
catch (Exception e)
{
if (e.InnerException != null)
{
e = e.InnerException;
}
Exceptions.SetError(e);
return default;
}
// Current and the conversion can both throw - the latter for a type
// refused by an IClrTypeFilter - and must not unwind through Python
try
{
object item = self.iter.Current;
return Converter.ToPython(item, self.elemType);
}
catch (Exception e)
{
Exceptions.SetError(e);
return default;
}
}
public static NewReference tp_iter(BorrowedReference ob) => new (ob);
}
}