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

Skip to content

Commit 0d60500

Browse files
committed
switched collection wrappers (from sample codec) to the new style references
1 parent 4793818 commit 0d60500

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/runtime/CollectionWrappers/IterableWrapper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@ public IEnumerator<T> GetEnumerator()
2727
iterObject = iter.MoveToPyObject();
2828
}
2929

30+
using (iterObject)
3031
while (true)
3132
{
3233
using (Py.GIL())
3334
{
34-
var item = Runtime.PyIter_Next(iterObject.Handle);
35-
if (item == IntPtr.Zero)
35+
using var item = Runtime.PyIter_Next(iterObject);
36+
if (item.IsNull())
3637
{
3738
Runtime.CheckExceptionOccurred();
3839
iterObject.Dispose();
3940
break;
4041
}
4142

42-
yield return (T)new PyObject(item).AsManagedObject(typeof(T));
43+
yield return item.MoveToPyObject().As<T>();
4344
}
4445
}
4546
}

src/runtime/CollectionWrappers/ListWrapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public T this[int index]
1414
{
1515
get
1616
{
17-
var item = Runtime.PyList_GetItem(pyObject.Reference, index);
17+
var item = Runtime.PyList_GetItem(pyObject, index);
1818
var pyItem = new PyObject(item);
1919
return pyItem.As<T>();
2020
}
2121
set
2222
{
2323
var pyItem = value.ToPython();
24-
var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem.Handle);
24+
var result = Runtime.PyList_SetItem(pyObject, index, new NewReference(pyItem).Steal());
2525
if (result == -1)
2626
Runtime.CheckExceptionOccurred();
2727
}
@@ -39,7 +39,7 @@ public void Insert(int index, T item)
3939

4040
var pyItem = item.ToPython();
4141

42-
var result = Runtime.PyList_Insert(pyObject.Reference, index, pyItem.Handle);
42+
int result = Runtime.PyList_Insert(pyObject, index, pyItem);
4343
if (result == -1)
4444
Runtime.CheckExceptionOccurred();
4545
}

src/runtime/CollectionWrappers/SequenceWrapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public int Count
2020
Runtime.CheckExceptionOccurred();
2121
}
2222

23-
return (int)size;
23+
return checked((int)size);
2424
}
2525
}
2626

@@ -38,7 +38,7 @@ public void Clear()
3838
{
3939
if (IsReadOnly)
4040
throw new NotImplementedException();
41-
var result = Runtime.PySequence_DelSlice(pyObject.Handle, 0, Count);
41+
int result = Runtime.PySequence_DelSlice(pyObject, 0, Count);
4242
if (result == -1)
4343
{
4444
Runtime.CheckExceptionOccurred();
@@ -49,7 +49,7 @@ public bool Contains(T item)
4949
{
5050
//not sure if IEquatable is implemented and this will work!
5151
foreach (var element in this)
52-
if (element.Equals(item)) return true;
52+
if (object.Equals(element, item)) return true;
5353

5454
return false;
5555
}
@@ -77,7 +77,7 @@ protected bool removeAt(int index)
7777
if (index >= Count || index < 0)
7878
return false;
7979

80-
var result = Runtime.PySequence_DelItem(pyObject.Handle, index);
80+
int result = Runtime.PySequence_DelItem(pyObject, index);
8181

8282
if (result == 0)
8383
return true;
@@ -91,7 +91,7 @@ protected int indexOf(T item)
9191
var index = 0;
9292
foreach (var element in this)
9393
{
94-
if (element.Equals(item)) return index;
94+
if (object.Equals(element, item)) return index;
9595
index++;
9696
}
9797

src/runtime/runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ internal static bool PyList_Check(BorrowedReference ob)
15361536

15371537
internal static NewReference PyList_New(nint size) => Delegates.PyList_New(size);
15381538

1539-
private static BorrowedReference PyList_GetItem(BorrowedReference pointer, IntPtr index) => Delegates.PyList_GetItem(pointer, index);
1539+
internal static BorrowedReference PyList_GetItem(BorrowedReference pointer, nint index) => Delegates.PyList_GetItem(pointer, index);
15401540

15411541
internal static int PyList_SetItem(BorrowedReference pointer, nint index, StolenReference value) => Delegates.PyList_SetItem(pointer, index, value);
15421542

0 commit comments

Comments
 (0)