10000 cleanup · pythonnet/pythonnet@080dbc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 080dbc3

Browse files
committed
cleanup
1 parent e8cc3d8 commit 080dbc3

File tree

3 files changed

+7
-68
lines changed

3 files changed

+7
-68
lines changed

src/runtime/CollectionWrappers/IterableWrapper.cs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,11 @@ internal class IterableWrapper<T> : IEnumerable<T>
1111
public IterableWrapper(PyObject pyObj)
1212
{
1313
if (pyObj == null)
14-
throw new PythonException();
14+
throw new ArgumentNullException();
1515
pyObject = pyObj;
1616
}
1717

18-
private void propagateIterationException()
19-
{
20-
var err = Runtime.PyErr_Occurred();
21-
if (err == null) return;
22-
23-
//remove StopIteration exceptions
24-
if (0 != Runtime.PyErr_ExceptionMatches(Exceptions.StopIteration))
25-
{
26-
Runtime.PyErr_Clear();
27-
return;
28-
}
29-
30-
Runtime.CheckExceptionOccurred();
31-
}
32-
33-
IEnumerator IEnumerable.GetEnumerator()
34-
{
35-
PyObject iterObject = null;
36-
using (Py.GIL())
37-
{
38-
iterObject = new PyObject(Runtime.PyObject_GetIter(pyObject.Handle));
39-
}
40-
41-
while (true)
42-
{
43-
IntPtr item = IntPtr.Zero;
44-
using (Py.GIL())
45-
{
46-
item = Runtime.PyIter_Next(iterObject.Handle);
47-
}
48-
if (item == IntPtr.Zero) break;
49-
50-
object obj = null;
51-
if (!Converter.ToManaged(item, typeof(object), out obj, true))
52-
{
53-
Runtime.XDecref(item);
54-
Runtime.CheckExceptionOccurred();
55-
}
56-
57-
Runtime.XDecref(item);
58-
yield return obj;
59-
}
60-
61-
propagateIterationException();
62-
}
18+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
6319

6420
public IEnumerator<T> GetEnumerator()
6521
{
@@ -88,8 +44,6 @@ public IEnumerator<T> GetEnumerator()
8844
Runtime.XDecref(item);
8945
yield return (T)obj;
9046
}
91-
92-
propagateIterationException();
9347
}
9448
}
9549
}

src/runtime/CollectionWrappers/ListWrapper.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,12 @@ public T this[int index]
1616
{
1717
var item = Runtime.PyList_GetItem(pyObject.Reference, index);
1818
var pyItem = new PyObject(item);
19-
20-
if (!Converter.ToManaged(pyItem.Handle, typeof 8000 (T), out object obj, true))
21-
Runtime.CheckExceptionOccurred();
22-
23-
return (T)obj;
19+
return pyItem.As<T>();
2420
}
2521
set
2622
{
27-
IntPtr pyItem = Converter.ToPython(value, typeof(T));
28-
if (pyItem == IntPtr.Zero)
29-
{
30-
throw new InvalidCastException(
31-
"cannot cast " + value.ToString() + "to type: " + typeof(T).ToString(),
32-
new PythonException());
33-
}
34-
35-
var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem);
23+
var pyItem = value.ToPython();
24+
var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem.Handle);
3625
if (result == -1)
3726
Runtime.CheckExceptionOccurred();
3827
}
@@ -48,12 +37,9 @@ public void Insert(int index, T item)
4837
if (IsReadOnly)
4938
throw new InvalidOperationException("Collection is read-only");
5039

51-
IntPtr pyItem = Converter.ToPython(item, typeof(T));
52-
if (pyItem == IntPtr.Zero)
53-
throw new PythonException();
40+
var pyItem = item.ToPython();
5441

55-
var result = Runtime.PyList_Insert(pyObject.Reference, index, pyItem);
56-
Runtime.XDecref(pyItem);
42+
var result = Runtime.PyList_Insert(pyObject.Reference, index, pyItem.Handle);
5743
if (result == -1)
5844
Runtime.CheckExceptionOccurred();
5945
}

src/runtime/CollectionWrappers/SequenceWrapper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public int Count
1818
if (size == -1)
1919
{
2020
Runtime.CheckExceptionOccurred();
21-
throw new Exception("Unable to get sequence size!");
2221
}
2322

2423
return (int)size;

0 commit comments

Comments
 (0)
0