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

Skip to content

Commit 61f6942

Browse files
committed
enable creating PyIter from existing PyObject
1 parent fa4cebd commit 61f6942

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/runtime/pyiter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public class PyIter : PyObject, IEnumerator<PyObject>
2424
public PyIter(IntPtr ptr) : base(ptr)
2525
{
2626
}
27+
/// <summary>
28+
/// Creates new <see cref="PyIter"/> from an untyped reference to Python object.
29+
/// </summary>
30+
public PyIter(PyObject pyObject) : base(FromPyObject(pyObject)) { }
31+
static BorrowedReference FromPyObject(PyObject pyObject) {
32+
if (pyObject is null) throw new ArgumentNullException(nameof(pyObject));
33+
34+
if (!Runtime.PyIter_Check(pyObject.Reference))
35+
throw new ArgumentException("Object does not support iterator protocol");
36+
37+
return pyObject.Reference;
38+
}
39+
2740
internal PyIter(BorrowedReference reference) : base(reference) { }
2841

2942
protected override void Dispose(bool disposing)

0 commit comments

Comments
 (0)
0