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

Allow user-created instances ofPySequence andPyIterable#1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromlosttech:pyseq-ctor
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletionssrc/runtime/pyiterable.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,18 @@ internal PyIterable(IntPtr ptr) : base(ptr)
internal PyIterable(BorrowedReference reference) : base(reference) { }
internal PyIterable(in StolenReference reference) : base(reference) { }

/// <summary>
/// Creates new instance from an existing object.
/// </summary>
/// <remarks>This constructor does not check if <paramref name="o"/> is actually iterable.</remarks>
public PyIterable(PyObject o) : base(FromObject(o)) { }

static BorrowedReference FromObject(PyObject o)
{
if (o is null) throw new ArgumentNullException(nameof(o));
return o.Reference;
}

/// <summary>
/// Return a new PyIter object for the object. This allows any iterable
/// python object to be iterated over in C#. A PythonException will be
Expand Down
13 changes: 13 additions & 0 deletionssrc/runtime/pysequence.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

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

/// <summary>
/// Creates new instance from an existing object.
/// </summary>
/// <exception cref="ArgumentException"><paramref name="o"/> does not provide sequence protocol</exception>
public PySequence(PyObject o) : base(FromObject(o)) { }

static BorrowedReference FromObject(PyObject o)
{
if (o is null) throw new ArgumentNullException(nameof(o));
if (!IsSequenceType(o)) throw new ArgumentException("object is not a sequence");
return o.Reference;
}

/// <summary>
/// Returns <c>true</c> if the given object implements the sequence protocol.
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp