8000 enable creating PyIter from existing PyObject · pythonnet/pythonnet@86aedb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86aedb0

Browse files
committed
enable creating PyIter from existing PyObject
1 parent ea61b03 commit 86aedb0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/runtime/pyiter.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ public PyIter(IntPtr ptr) : base(ptr)
2525
{
2626
}
2727

28+
/// <summary>
29+
/// Creates new <see cref="PyIter"/> from an untyped reference to Python object.
30+
/// The object must support iterator protocol.
31+
/// </summary>
32+
public PyIter(PyObject pyObject) : base(FromPyObject(pyObject)) { }
33+
static BorrowedReference FromPyObject(PyObject pyObject) {
34+
if (pyObject is null) throw new ArgumentNullException(nameof(pyObject));
35+
36+
if (!Runtime.PyIter_Check(pyObject.Reference))
37+
throw new ArgumentException("Object does not support iterator protocol");
38+
39+
return pyObject.Reference;
40+
}
41+
42+
internal PyIter(BorrowedReference reference) : base(reference) { }
43+
2844
/// <summary>
2945
/// PyIter factory function.
3046
/// </summary>

0 commit comments

Comments
 (0)
0