allow user-created instances of PySequence and PyIterable · pythonnet/pythonnet@c09de96

Original file line numberDiff line numberDiff line change

@@ -13,6 +13,18 @@ internal PyIterable(IntPtr ptr) : base(ptr)

1313

internal PyIterable(BorrowedReference reference) : base(reference) { }

1414

internal PyIterable(in StolenReference reference) : base(reference) { }

1515
16+

/// <summary>

17+

/// Creates new instance from an existing object.

18+

/// </summary>

19+

/// <remarks>This constructor does not check if <paramref name="o"/> is actually iterable.</remarks>

20+

public PyIterable(PyObject o) : base(FromObject(o)) { }

21+
22+

static BorrowedReference FromObject(PyObject o)

23+

{

24+

if (o is null) throw new ArgumentNullException(nameof(o));

25+

return o.Reference;

26+

}

27+
1628

/// <summary>

1729

/// Return a new PyIter object for the object. This allows any iterable

1830

/// python object to be iterated over in C#. A PythonException will be