This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyiter.cs
82 lines (71 loc) · 2.09 KB
/
pyiter.cs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
namespace Python.Runtime
{
/// <summary>
/// Represents a standard Python iterator object. See the documentation at
/// http://www.python.org/doc/2.4.4/api/iterator.html for details.
/// </summary>
public class PyIter : PyObject, IEnumerator<object>
{
private PyObject _current = null;
/// <summary>
/// PyIter Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyIter from an existing iterator reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyIter(IntPtr ptr) : base(ptr)
{
}
/// <summary>
/// PyIter Constructor
/// </summary>
///
/// <remarks>
/// Creates a Python iterator from an iterable. Like doing "iter(iterable)" in python.
/// </remarks>
public PyIter(PyObject iterable) : base()
{
obj = Runtime.PyObject_GetIter(iterable.obj);
if (obj == IntPtr.Zero)
throw new PythonException();
}
protected override void Dispose(bool disposing)
{
if (null != _current)
{
_current.Dispose();
_current = null;
}
base.Dispose(disposing);
}
#region IEnumerator Members
public bool MoveNext()
{
// dispose of the previous object, if there was one
if (null != _current)
{
_current.Dispose();
_current = null;
}
IntPtr next = Runtime.PyIter_Next(obj);
if (next == IntPtr.Zero)
return false;
_current = new PyObject(next);
return true;
}
public void Reset()
{
//Not supported in python.
}
public object Current
{
get { return _current; }
}
#endregion
}
}