Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a06da16

Browse files
committed
allow user-created instances of PySequence
1 parent 5d0d01a commit a06da16

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/runtime/pysequence.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23

34
namespace Python.Runtime
@@ -14,6 +15,18 @@ public class PySequence : PyIterable
1415
internal PySequence(BorrowedReference reference) : base(reference) { }
1516
internal PySequence(in StolenReference reference) : base(reference) { }
1617

18+
/// <summary>
19+
/// Creates new instance from an existing object.
20+
/// </summary>
21+
/// <exception cref="ArgumentException"><paramref name="o"/> does not provide sequence protocol</exception>
22+
public PySequence(PyObject o) : base(FromObject(o)) { }
23+
24+
static BorrowedReference FromObject(PyObject o)
25+
{
26+
if (o is null) throw new ArgumentNullException(nameof(o));
27+
if (!IsSequenceType(o)) throw new ArgumentException("object is not a sequence");
28+
return o.Reference;
29+
}
1730

1831
/// <summary>
1932
/// Returns <c>true</c> if the given object implements the sequence protocol.

0 commit comments

Comments
 (0)