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

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all 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 deletions 12 src/runtime/pyiterable.cs
Original file line number Diff line number Diff 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 deletions 13 src/runtime/pysequence.cs
Original file line number Diff line number Diff 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.