Merge pull request #1482 from losttech/Mini/PyIterFromPyObj · pythonnet/pythonnet@191bc89

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>