File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,18 @@ internal PyIterable(IntPtr ptr) : base(ptr)
13
13
internal PyIterable ( BorrowedReference reference ) : base ( reference ) { }
14
14
internal PyIterable ( in StolenReference reference ) : base ( reference ) { }
15
15
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
+
16
28
/// <summary>
17
29
/// Return a new PyIter object for the object. This allows any iterable
18
30
/// python object to be iterated over in C#. A PythonException will be
Original file line number Diff line number Diff line change
1
+ #nullable enable
1
2
using System ;
2
3
3
4
namespace Python . Runtime
@@ -14,6 +15,18 @@ public class PySequence : PyIterable
14
15
internal PySequence ( BorrowedReference reference ) : base ( reference ) { }
15
16
internal PySequence ( in StolenReference reference ) : base ( reference ) { }
16
17
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
+ }
17
30
18
31
/// <summary>
19
32
/// Returns <c>true</c> if the given object implements the sequence protocol.
You can’t perform that action at this time.
0 commit comments