Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
koubaa committed Feb 16, 2021
commit 080dbc3dd91f6e99792e335d635bd14ca066b677
50 changes: 2 additions & 48 deletions src/runtime/CollectionWrappers/IterableWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,11 @@ internal class IterableWrapper<T> : IEnumerable<T>
public IterableWrapper(PyObject pyObj)
{
if (pyObj == null)
throw new PythonException();
throw new ArgumentNullException();
pyObject = pyObj;
}

private void propagateIterationException()
{
var err = Runtime.PyErr_Occurred();
if (err == null) return;

//remove StopIteration exceptions
if (0 != Runtime.PyErr_ExceptionMatches(Exceptions.StopIteration))
{
Runtime.PyErr_Clear();
return;
}

Runtime.CheckExceptionOccurred();
}

IEnumerator IEnumerable.GetEnumerator()
{
PyObject iterObject = null;
using (Py.GIL())
{
iterObject = new PyObject(Runtime.PyObject_GetIter(pyObject.Handle));
}

while (true)
{
IntPtr item = IntPtr.Zero;
using (Py.GIL())
{
item = Runtime.PyIter_Next(iterObject.Handle);
}
if (item == IntPtr.Zero) break;

object obj = null;
if (!Converter.ToManaged(item, typeof(object), out obj, true))
{
Runtime.XDecref(item);
Runtime.CheckExceptionOccurred();
}

Runtime.XDecref(item);
yield return obj;
}

propagateIterationException();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public IEnumerator<T> GetEnumerator()
{
Expand Down Expand Up @@ -88,8 +44,6 @@ public IEnumerator<T> GetEnumerator()
Runtime.XDecref(item);
yield return (T)obj;
}

propagateIterationException();
}
}
}
24 changes: 5 additions & 19 deletions src/runtime/CollectionWrappers/ListWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,12 @@ public T this[int index]
{
var item = Runtime.PyList_GetItem(pyObject.Reference, index);
var pyItem = new PyObject(item);
Copy link
Member

Choose a reason for hiding this comment

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

Dispose this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lostmsu again I thought the point of making it IDisposable was that the finalizer would take care of that

Copy link
Member

Choose a reason for hiding this comment

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

Wrap it by using statement is better. If you knew it's no use anymore you should dispose it manually instead of waiting for the auto collection to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lostmsu @amos402 sorry, I might be missing context (I rebased and I don't know if this comment lines up with the code it was pointing to initially).

What specifically should I be disposing or wrap in a using statement?

Copy link
Member

Choose a reason for hiding this comment

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

using (var pyItem = new PyObject(item)) { }

Copy link
Member

Choose a reason for hiding this comment

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

@koubaa this still needs a fix. With C# 8+ which we use now, it should just be using var pyItem = new PyObject(item); so that the Python object, corresponding to the item would be disposed and not pollute finalizer thread unless duplicated.


if (!Converter.ToManaged(pyItem.Handle, typeof(T), out object obj, true))
Runtime.CheckExceptionOccurred();

return (T)obj;
return pyItem.As<T>();
}
set
{
IntPtr pyItem = Converter.ToPython(value, typeof(T));
if (pyItem == IntPtr.Zero)
{
throw new InvalidCastException(
"cannot cast " + value.ToString() + "to type: " + typeof(T).ToString(),
new PythonException());
}

var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem);
var pyItem = value.ToPython();
var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem.Handle);
if (result == -1)
Runtime.CheckExceptionOccurred();
}
Expand All @@ -48,12 +37,9 @@ public void Insert(int index, T item)
if (IsReadOnly)
throw new InvalidOperationException("Collection is read-only");

IntPtr pyItem = Converter.ToPython(item, typeof(T));
if (pyItem == IntPtr.Zero)
throw new PythonException();
var pyItem = item.ToPython();

var result = Runtime.PyList_Insert(pyObject.Reference, index, pyItem);
Runtime.XDecref(pyItem);
var result = Runtime.PyList_Insert(pyObject.Reference, index, pyItem.Handle);
if (result == -1)
Runtime.CheckExceptionOccurred();
}
Expand Down
1 change: 0 additions & 1 deletion src/runtime/CollectionWrappers/SequenceWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public int Count
if (size == -1)
{
Runtime.CheckExceptionOccurred();
throw new Exception("Unable to get sequence size!");
}

return (int)size;
Expand Down