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
2 changes: 2 additions & 0 deletions 2 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ details about the cause of the failure
- BREAKING: Parameters marked with `ParameterAttributes.Out` are no longer returned in addition
to the regular method return value (unless they are passed with `ref` or `out` keyword).
- BREAKING: Drop support for the long-deprecated CLR.* prefix.
- `PyObject` now implements `IEnumerable<PyObject>` in addition to `IEnumerable`

### Fixed

Expand All @@ -40,6 +41,7 @@ details about the cause of the failure
- Fixed a bug where indexers could not be used if they were inherited
- Made it possible to use `__len__` also on `ICollection<>` interface objects
- Made it possible to call `ToString`, `GetHashCode`, and `GetType` on inteface objects
- Fixed objects returned by enumerating `PyObject` being disposed too soon

### Removed

Expand Down
37 changes: 37 additions & 0 deletions 37 src/embed_tests/TestPyIter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Linq;
using System.Text;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
class TestPyIter
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void KeepOldObjects()
{
using (Py.GIL())
using (var testString = new PyString("hello world! !$%&/()=?"))
{
PyObject[] chars = testString.ToArray();
Assert.IsTrue(chars.Length > 1);
string reconstructed = string.Concat(chars.Select(c => c.As<string>()));
Assert.AreEqual(testString.As<string>(), reconstructed);
}
}
}
}
34 changes: 14 additions & 20 deletions 34 src/runtime/pyiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Python.Runtime
/// PY3: https://docs.python.org/3/c-api/iterator.html
/// for details.
/// </summary>
public class PyIter : PyObject, IEnumerator<object>
public class PyIter : PyObject, IEnumerator<PyObject>
{
private PyObject _current;

Expand Down Expand Up @@ -46,41 +46,35 @@ public static PyIter GetIter(PyObject iterable)

protected override void Dispose(bool disposing)
{
if (null != _current)
{
_current.Dispose();
_current = null;
}
_current = null;
base.Dispose(disposing);
}

public bool MoveNext()
{
// dispose of the previous object, if there was one
if (null != _current)
NewReference next = Runtime.PyIter_Next(Reference);
if (next.IsNull())
{
_current.Dispose();
_current = null;
}
if (Exceptions.ErrorOccurred())
{
throw new PythonException();
}

IntPtr next = Runtime.PyIter_Next(obj);
if (next == IntPtr.Zero)
{
// stop holding the previous object, if there was one
_current = null;
return false;
}

_current = new PyObject(next);
_current = next.MoveToPyObject();
return true;
}

public void Reset()
{
//Not supported in python.
throw new NotSupportedException();
}

public object Current
{
get { return _current; }
}
public PyObject Current => _current;
object System.Collections.IEnumerator.Current => _current;
}
}
5 changes: 3 additions & 2 deletions 5 src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Python.Runtime
/// for details.
/// </summary>
[Serializable]
public partial class PyObject : DynamicObject, IEnumerable, IDisposable
public partial class PyObject : DynamicObject, IEnumerable<PyObject>, IDisposable
{
#if TRACE_ALLOC
/// <summary>
Expand Down Expand Up @@ -705,10 +705,11 @@ public PyObject GetIterator()
/// python object to be iterated over in C#. A PythonException will be
/// raised if the object is not iterable.
/// </remarks>
public IEnumerator GetEnumerator()
public IEnumerator<PyObject> GetEnumerator()
{
return PyIter.GetIter(this);
}
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();


/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions 2 src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,8 @@ internal static bool PyIter_Check(IntPtr pointer)

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyIter_Next(IntPtr pointer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there are still a few uses.

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern NewReference PyIter_Next(BorrowedReference pointer);


//====================================================================
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.