forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPyIterable.cs
More file actions
40 lines (36 loc) · 1.45 KB
/
PyIterable.cs
File metadata and controls
40 lines (36 loc) · 1.45 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Python.Runtime
{
[Serializable]
public class PyIterable : PyObject, IEnumerable<PyObject>
{
internal PyIterable(BorrowedReference reference) : base(reference) { }
internal PyIterable(in StolenReference reference) : base(reference) { }
protected PyIterable(SerializationInfo info, StreamingContext context)
: base(info, context) { }
/// <summary>
/// Creates new instance from an existing object.
/// </summary>
/// <remarks>This constructor does not check if <paramref name="o"/> is actually iterable.</remarks>
public PyIterable(PyObject o) : base(FromObject(o)) { }
static BorrowedReference FromObject(PyObject o)
{
if (o is null) throw new ArgumentNullException(nameof(o));
return o.Reference;
}
/// <summary>
/// Return a new PyIter object for the object. This allows any iterable
/// python object to be iterated over in C#. A PythonException will be
/// raised if the object is not iterable.
/// </summary>
public PyIter GetEnumerator()
{
return PyIter.GetIter(this);
}
IEnumerator<PyObject> IEnumerable<PyObject>.GetEnumerator() => this.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}
}