-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy pathIterableWrapper.cs
More file actions
46 lines (42 loc) · 1.16 KB
/
Copy pathIterableWrapper.cs
File metadata and controls
46 lines (42 loc) · 1.16 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
using System;
using System.Collections.Generic;
using System.Collections;
namespace Python.Runtime.CollectionWrappers
{
internal class IterableWrapper<T> : IEnumerable<T>
{
protected readonly PyObject pyObject;
public IterableWrapper(PyObject pyObj)
{
if (pyObj == null)
throw new ArgumentNullException();
pyObject = new PyObject(pyObj.Reference);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<T> GetEnumerator()
{
PyIter iterObject;
using (Py.GIL())
{
iterObject = PyIter.GetIter(pyObject);
}
try
{
while (true)
{
using var _ = Py.GIL();
if (!iterObject.MoveNext())
{
break;
}
yield return iterObject.Current.As<T>()!;
}
}
finally
{
using var _ = Py.GIL();
iterObject.Dispose();
}
}
}
}