|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Collections; |
4 |
| - |
5 |
| -namespace Python.Runtime.CollectionWrappers |
6 |
| -{ |
7 |
| - internal class IterableWrapper<T> : IEnumerable<T> |
8 |
| - { |
9 |
| - protected readonly PyObject pyObject; |
10 |
| - |
11 |
| - public IterableWrapper(PyObject pyObj) |
12 |
| - { |
13 |
| - if (pyObj == null) |
14 |
| - throw new ArgumentNullException(); |
15 |
| - pyObject = new PyObject(pyObj.Reference); |
16 |
| - } |
17 |
| - |
18 |
| - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
19 |
| - |
20 |
| - public IEnumerator<T> GetEnumerator() |
21 |
| - { |
22 |
| - PyIter iterObject; |
23 |
| - using (Py.GIL()) |
24 |
| - { |
25 |
| - iterObject = PyIter.GetIter(pyObject); |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections; |
| 4 | + |
| 5 | +namespace Python.Runtime.CollectionWrappers |
| 6 | +{ |
| 7 | + internal class IterableWrapper<T> : IEnumerable<T> |
| 8 | + { |
| 9 | + protected readonly PyObject pyObject; |
| 10 | + |
| 11 | + public IterableWrapper(PyObject pyObj) |
| 12 | + { |
| 13 | + if (pyObj == null) |
| 14 | + throw new ArgumentNullException(); |
| 15 | + pyObject = new PyObject(pyObj.Reference); |
| 16 | + } |
| 17 | + |
| 18 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 19 | + |
| 20 | + public IEnumerator<T> GetEnumerator() |
| 21 | + { |
| 22 | + PyIter iterObject; |
| 23 | + using (Py.GIL()) |
| 24 | + { |
| 25 | + iterObject = PyIter.GetIter(pyObject); |
26 | 26 | }
|
27 |
| - |
28 |
| - using var _ = iterObject; |
29 |
| - while (true) |
| 27 | + try |
30 | 28 | {
|
31 |
| - using var GIL = Py.GIL(); |
32 |
| - |
33 |
| - if (!iterObject.MoveNext()) |
| 29 | + while (true) |
34 | 30 | {
|
35 |
| - iterObject.Dispose(); |
36 |
| - break; |
| 31 | + using var _ = Py.GIL(); |
| 32 | + if (!iterObject.MoveNext()) |
| 33 | + { |
| 34 | + break; |
| 35 | + } |
| 36 | + yield return iterObject.Current.As<T>()!; |
37 | 37 | }
|
38 |
| - yield return iterObject.Current.As<T>()!; |
39 | 38 | }
|
40 |
| - } |
41 |
| - } |
42 |
| -} |
| 39 | + finally |
| 40 | + { |
| 41 | + using var _ = Py.GIL(); |
| 42 | + iterObject.Dispose(); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments