Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/runtime/CollectionWrappers/ListWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public T this[int index]
{
get
{
using var _ = Py.GIL();
var item = Runtime.PyList_GetItem(pyObject, index);
var pyItem = new PyObject(item);
return pyItem.As<T>()!;
}
set
{
using var _ = Py.GIL();
var pyItem = value.ToPython();
var result = Runtime.PyList_SetItem(pyObject, index, new NewReference(pyItem).Steal());
if (result == -1)
Expand All @@ -37,6 +39,7 @@ public void Insert(int index, T item)
if (IsReadOnly)
throw new InvalidOperationException("Collection is read-only");

using var _ = Py.GIL();
var pyItem = item.ToPython();

int result = Runtime.PyList_Insert(pyObject, index, pyItem);
Expand Down
23 changes: 16 additions & 7 deletions src/runtime/CollectionWrappers/SequenceWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public int Count
{
get
{
var size = Runtime.PySequence_Size(pyObject.Reference);
if (size == -1)
nint size = -1;
{
Runtime.CheckExceptionOccurred();
using var _ = Py.GIL();
size = Runtime.PySequence_Size(pyObject.Reference);
if (size == -1)
{
Runtime.CheckExceptionOccurred();
}
}

return checked((int)size);
Expand All @@ -38,6 +42,7 @@ public void Clear()
{
if (IsReadOnly)
throw new NotImplementedException();
using var _ = Py.GIL();
int result = Runtime.PySequence_DelSlice(pyObject, 0, Count);
if (result == -1)
{
Expand Down Expand Up @@ -77,12 +82,16 @@ protected bool removeAt(int index)
if (index >= Count || index < 0)
return false;

int result = Runtime.PySequence_DelItem(pyObject, index);

if (result == 0)
return true;
{
using var _ = Py.GIL();
int result = Runtime.PySequence_DelItem(pyObject, index);

if (result == 0)
return true;

Runtime.CheckExceptionOccurred();
Runtime.CheckExceptionOccurred();
}
return false;
}

Expand Down
Loading