Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc09de96

Browse files
committed
allow user-created instances of PySequence and PyIterable
1 parent5d0d01a commitc09de96

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

‎src/runtime/pyiterable.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ internal PyIterable(IntPtr ptr) : base(ptr)
1313
internalPyIterable(BorrowedReferencereference):base(reference){}
1414
internalPyIterable(inStolenReferencereference):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+
publicPyIterable(PyObjecto):base(FromObject(o)){}
21+
22+
staticBorrowedReferenceFromObject(PyObjecto)
23+
{
24+
if(oisnull)thrownewArgumentNullException(nameof(o));
25+
returno.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

‎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
usingSystem;
23

34
namespacePython.Runtime
@@ -14,6 +15,18 @@ public class PySequence : PyIterable
1415
internalPySequence(BorrowedReferencereference):base(reference){}
1516
internalPySequence(inStolenReferencereference):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+
publicPySequence(PyObjecto):base(FromObject(o)){}
23+
24+
staticBorrowedReferenceFromObject(PyObjecto)
25+
{
26+
if(oisnull)thrownewArgumentNullException(nameof(o));
27+
if(!IsSequenceType(o))thrownewArgumentException("object is not a sequence");
28+
returno.Reference;
29+
}
1730

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp