8000 respond to PR comments · pythonnet/pythonnet@c43446e · GitHub
[go: up one dir, main page]

Skip to content

Commit c43446e

Browse files
committed
respond to PR comments
1 parent 1600fdc commit c43446e

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/runtime/CollectionWrappers/IterableWrapper.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@ namespace Python.Runtime.CollectionWrappers
66
{
77
internal class IterableWrapper<T> : IEnumerable<T>
88
{
9-
protected PyObject iterObject;
109
protected PyObject pyObject;
1110

1211
public IterableWrapper(PyObject pyObj)
1312
{
1413
pyObject = pyObj;
15-
iterObject = new PyObject(Runtime.PyObject_GetIter(pyObj.Handle));
14+
}
15+
16+
private void propagateIterationException()
17+
{
18+
var err = Runtime.PyErr_Occurred();
19+
if (err != null && err != Exceptions.StopIteration)
20+
{
21+
Runtime.CheckExceptionOccurred();
22+
}
1623
}
1724

1825
IEnumerator IEnumerable.GetEnumerator()
1926
{
27+
if (pyObject == null) yield break;
28+
PyObject iterObject = new PyObject(Runtime.PyObject_GetIter(pyObject.Handle));
2029
IntPtr item;
30+
2131
while ((item = Runtime.PyIter_Next(iterObject.Handle)) != IntPtr.Zero)
2232
{
2333
object obj = null;
@@ -30,10 +40,14 @@ IEnumerator IEnumerable.GetEnumerator()
3040
Runtime.XDecref(item);
3141
yield return obj;
3242
}
43+
44+
propagateIterationException();
3345
}
3446

3547
public IEnumerator<T> GetEnumerator()
3648
{
49+
if (pyObject == null) yield break;
50+
PyObject iterObject = new PyObject(Runtime.PyObject_GetIter(pyObject.Handle));
3751
IntPtr item;
3852
while ((item = Runtime.PyIter_Next(iterObject.Handle)) != IntPtr.Zero)
3953
{
@@ -47,6 +61,8 @@ public IEnumerator<T> GetEnumerator()
4761
Runtime.XDecref(item);
4862
yield return (T)obj;
4963
}
64+
65+
propagateIterationException();
5066
}
5167
}
5268
}

src/runtime/CollectionWrappers/ListWrapper.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ public T this[int index]
1414
{
1515
get
1616
{
17-
IntPtr item = Runtime.PySequence_GetItem(pyObject.Handle, index);
18-
object obj;
17+
var item = Runtime.PyList_GetItem(pyObject.Handle, index);
18+
var pyItem = new PyObject(item);
1919

20-
if (!Converter.ToManaged(item, typeof(T), out obj, true))
21-
{
22-
Runtime.XDecref(item);
20+
if (!Converter.ToManaged(pyItem.Handle, typeof(T), out object obj, true))
2321
Runtime.CheckExceptionOccurred();
24-
}
2522

2623
return (T)obj;
2724
}
@@ -31,10 +28,10 @@ public T this[int index]
3128
if (pyItem == IntPtr.Zero)
3229
throw new Exception("failed to set item");
3330

34-
var result = Runtime.PySequence_SetItem(pyObject.Handle, index, pyItem);
31+
var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem);
3532
Runtime.XDecref(pyItem);
3633
if (result == -1)
37-
throw new Exception("failed to set item");
34+
Runtime.CheckExceptionOccurred();
3835
}
3936
}
4037

@@ -46,7 +43,7 @@ public int IndexOf(T item)
4643
public void Insert(int index, T item)
4744
{
4845
if (IsReadOnly)
49-
throw new NotImplementedException();
46+
throw new InvalidOperationException("Collection is read-only");
5047

5148
IntPtr pyItem = Converter.ToPython(item, typeof(T));
5249
if (pyItem == IntPtr.Zero)
@@ -55,7 +52,7 @@ public void Insert(int index, T item)
5552
var result = Runtime.PyList_Insert(pyObject.Reference, index, pyItem);
5653
Runtime.XDecref(pyItem);
5754
if (result == -1)
58-
throw new Exception("failed to insert item");
55+
Runtime.CheckExceptionOccurred();
5956
}
6057

6158
public void RemoveAt(int index)

src/runtime/CollectionWrappers/SequenceWrapper.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ public int Count
1414
{
1515
get
1616
{
17-
return (int)Runtime.PySequence_Size(pyObject.Handle);
17+
var size = Runtime.PySequence_Size(pyObject.Handle);
18+
if (size == -1)
19+
{
20+
Runtime.CheckExceptionOccurred();
21+
throw new Exception("Unable to get sequence size!");
22+
}
23+
24+
return (int)size;
1825
}
1926
}
2027

@@ -34,7 +41,10 @@ public void Clear()
3441
throw new NotImplementedException();
3542
var result = Runtime.PySequence_DelSlice(pyObject.Handle, 0, Count);
3643
if (result == -1)
44+
{
45+
Runtime.CheckExceptionOccurred();
3746
throw new Exception("failed to clear sequence");
47+
}
3848
}
3949

4050
public bool Contains(T item)
@@ -46,7 +56,7 @@ public bool Contains(T item)
4656
return false;
4757
}
4858

49-
protected T getItem(int index)
59+
private T getItem(int index)
5060
{
5161
IntPtr item = Runtime.PySequence_GetItem(pyObject.Handle, index);
5262
object obj;

0 commit comments

Comments
 (0)
0