diff --git a/src/runtime/pyiter.cs b/src/runtime/pyiter.cs index da2a600c6..72e967be2 100644 --- a/src/runtime/pyiter.cs +++ b/src/runtime/pyiter.cs @@ -25,6 +25,22 @@ public PyIter(IntPtr ptr) : base(ptr) { } + /// + /// Creates new from an untyped reference to Python object. + /// The object must support iterator protocol. + /// + public PyIter(PyObject pyObject) : base(FromPyObject(pyObject)) { } + static BorrowedReference FromPyObject(PyObject pyObject) { + if (pyObject is null) throw new ArgumentNullException(nameof(pyObject)); + + if (!Runtime.PyIter_Check(pyObject.Reference)) + throw new ArgumentException("Object does not support iterator protocol"); + + return pyObject.Reference; + } + + internal PyIter(BorrowedReference reference) : base(reference) { } + /// /// PyIter factory function. ///