Skip to content

Commit 8d74e1c

Browse files
committed
Fix issues
1 parent 0c6a7e2 commit 8d74e1c

File tree

3 files changed

+5
-22
lines changed

3 files changed

+5
-22
lines changed

src/runtime/pyiter.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@ public PyIter(IntPtr ptr) : base(ptr)
2525
{
2626
}
2727

28-
private static IntPtr FromObject(PyObject iterable)
29-
{
30-
if (iterable == null)
31-
{
32-
throw new NullReferenceException();
33-
}
34-
IntPtr val = Runtime.PyObject_GetIter(iterable.obj);
35-
if (val == IntPtr.Zero)
36-
{
37-
throw new PythonException();
38-
}
39-
return val;
40-
}
41-
42-
4328
/// <summary>
4429
/// PyIter factory function.
4530
/// </summary>
@@ -52,13 +37,10 @@ public static PyIter GetIter(PyObject iterable)
5237
{
5338
if (iterable == null)
5439
{
55-
throw new NullReferenceException();
40+
throw new ArgumentNullException();
5641
}
5742
IntPtr val = Runtime.PyObject_GetIter(iterable.obj);
58-
if (val == IntPtr.Zero)
59-
{
60-
throw new PythonException();
61-
}
43+
PythonException.ThrowIfIsNull(val);
6244
return new PyIter(val);
6345
}
6446

src/runtime/pythonexception.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,11 @@ internal static void ThrowIfIsNull(BorrowedReference reference)
261261
}
262262
}
263263

264-
public static void ThrowIfIsNotZero(int value)
264+
public static void ThrowIfIsNotZero(int value, Action onBeforeThrow = null)
265265
{
266266
if (value != 0)
267267
{
268+
onBeforeThrow?.Invoke();
268269
throw new PythonException();
269270
}
270271
}

src/runtime/pytuple.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static IntPtr FromArray(PyObject[] items)
7474
IntPtr ptr = items[i].obj;
7575
Runtime.XIncref(ptr);
7676
int res = Runtime.PyTuple_SetItem(val, i, ptr);
77-
PythonException.ThrowIfIsNotZero(res);
77+
PythonException.ThrowIfIsNotZero(res, () => Runtime.Py_DecRef(val));
7878
}
7979
return val;
8080
}

0 commit comments

Comments
 (0)