Skip to content

Commit 191bc89

Browse files
authored
Merge pull request #1482 from losttech/Mini/PyIterFromPyObj
Create PyIter from existing PyObject
2 parents 1391639 + 86aedb0 commit 191bc89

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy