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

Skip to content

Commit 07f1657

Browse files
committed
fixed new reference uses, that are not allowed in C#
1 parent 581f695 commit 07f1657

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/runtime/CollectionWrappers/IterableWrapper.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,23 @@ public IterableWrapper(PyObject pyObj)
1919

2020
public IEnumerator<T> GetEnumerator()
2121
{
22-
PyObject iterObject;
22+
PyIter iterObject;
2323
using (Py.GIL())
2424
{
25-
var iter = Runtime.PyObject_GetIter(pyObject.Reference);
26-
PythonException.ThrowIfIsNull(iter);
27-
iterObject = iter.MoveToPyObject();
25+
iterObject = PyIter.GetIter(pyObject);
2826
}
2927

30-
using (iterObject)
28+
using var _ = iterObject;
3129
while (true)
3230
{
33-
using (Py.GIL())
34-
{
35-
using var item = Runtime.PyIter_Next(iterObject);
36-
if (item.IsNull())
37-
{
38-
Runtime.CheckExceptionOccurred();
39-
iterObject.Dispose();
40-
break;
41-
}
31+
using var GIL = Py.GIL();
4232

43-
yield return item.MoveToPyObject().As<T>();
33+
if (!iterObject.MoveNext())
34+
{
35+
iterObject.Dispose();
36+
break;
4437
}
38+
yield return iterObject.Current.As<T>();
4539
}
4640
}
4741
}

src/runtime/runtime_state.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ public static IEnumerable<IntPtr> PyGCGetObjects()
134134
{
135135
using var gc = PyModule.Import("gc");
136136
using var get_objects = gc.GetAttr("get_objects");
137-
using var objs = PyObject_CallObject(get_objects, args: null);
138-
nint length = PyList_Size(objs.BorrowOrThrow());
137+
using var objs = new PyObject(PyObject_CallObject(get_objects, args: null).StealOrThrow());
138+
nint length = PyList_Size(objs);
139139
if (length < 0) throw PythonException.ThrowLastAsClrException();
140140
for (nint i = 0; i < length; i++)
141141
{
142-
var obj = PyList_GetItem(objs.Borrow(), i);
142+
BorrowedReference obj = PyList_GetItem(objs, i);
143143
yield return obj.DangerousGetAddress();
144144
}
145145
}

0 commit comments

Comments
 (0)