FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

PyIter: do not force dispose previous object upon moving to the next one by lostmsu · Pull Request #1331 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (4) .md  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 src/embed_tests/TestPyIter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 src/runtime/pyiter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 src/runtime/pyobject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 src/runtime/runtime.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Do we still need this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yes, there are still a few uses.

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


//====================================================================
Expand Down

Back | FazBrowse Home | New Git URL