Skip to content

Commit 945db38

Browse files
committed
respond to PR review
1 parent cff4f4f commit 945db38

File tree

10 files changed

+30
-12
lines changed

10 files changed

+30
-12
lines changed

src/runtime/pyansistring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public PyAnsiString(IntPtr ptr) : base(ptr)
1919

2020
private static IntPtr FromObject(PyObject o)
2121
{
22-
if (!IsStringType(o))
22+
if (o == null || !IsStringType(o))
2323
{
2424
throw new ArgumentException("object is not a string");
2525
}

src/runtime/pydict.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public PyDict() : base(Runtime.PyDict_New())
4848
/// </remarks>
4949
public PyDict(PyObject o) : base(o.obj)
5050
{
51+
Runtime.XIncref(o.obj);
5152
if (!IsDictType(o))
5253
{
5354
throw new ArgumentException("object is not a dict");
5455
}
55-
Runtime.XIncref(o.obj);
5656
}
5757

5858

src/runtime/pyfloat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public PyFloat(double value) : base(FromDouble(value))
4747

4848
private static IntPtr FromObject(PyObject o)
4949
{
50-
if (!IsFloatType(o))
50+
if (o == null || !IsFloatType(o))
5151
{
5252
throw new ArgumentException("object is not a float");
5353
}

src/runtime/pyint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public PyInt(PyObject o) : base(FromObject(o))
3737

3838
private static IntPtr FromObject(PyObject o)
3939
{
40-
if (!IsIntType(o))
40+
if (o == null || !IsIntType(o))
4141
{
4242
throw new ArgumentException("object is not an int");
4343
}

src/runtime/pyiter.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public PyIter(IntPtr ptr) : base(ptr)
2727

2828
private static IntPtr FromObject(PyObject iterable)
2929
{
30+
if (iterable == null)
31+
{
32+
throw new NullReferenceException();
33+
}
3034
IntPtr val = Runtime.PyObject_GetIter(iterable.obj);
3135
if (val == IntPtr.Zero)
3236
{
@@ -35,14 +39,27 @@ private static IntPtr FromObject(PyObject iterable)
3539
return val;
3640
}
3741

42+
3843
/// <summary>
39-
/// PyIter Constructor
44+
/// PyIter factory function.
4045
/// </summary>
4146
/// <remarks>
42-
/// Creates a Python iterator from an iterable. Like doing "iter(iterable)" in python.
47+
/// Create a new PyIter from a given iterable. Like doing "iter(iterable)" in python.
4348
/// </remarks>
44-
public PyIter(PyObject iterable) : base(FromObject(iterable))
49+
/// <param name="iterable"></param>
50+
/// <returns></returns>
51+
public static PyIter GetIter(PyObject iterable)
4552
{
53+
if (iterable == null)
54+
{
55+
throw new NullReferenceException();
56+
}
57+
IntPtr val = Runtime.PyObject_GetIter(iterable.obj);
58+
if (val == IntPtr.Zero)
59+
{
60+
throw new PythonException();
61+
}
62+
return new PyIter(val);
4663
}
4764

4865
protected override void Dispose(bool disposing)

src/runtime/pylist.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal PyList(BorrowedReference reference) : base(reference) { }
3030

3131
private static IntPtr FromObject(PyObject o)
3232
{
33-
if (!IsListType(o))
33+
if (o == null || !IsListType(o))
3434
{
3535
throw new ArgumentException("object is not a list");
3636
}
@@ -76,6 +76,7 @@ private static IntPtr FromArray(PyObject[] items)
7676
int r = Runtime.PyList_SetItem(val, i, ptr);
7777
if (r < 0)
7878
{
79+
Runtime.Py_DecRef(val);
7980
throw new PythonException();
8081
}
8182
}

src/runtime/pylong.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public PyLong(PyObject o) : base(FromObject(o))
3737

3838
private static IntPtr FromObject(PyObject o)
3939
{
40-
if (!IsLongType(o))
40+
if (o == null || !IsLongType(o))
4141
{
4242
throw new ArgumentException("object is not a long");
4343
}

src/runtime/pyobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ public PyObject GetIterator()
699699
/// </remarks>
700700
public IEnumerator GetEnumerator()
701701
{
702-
return new PyIter(this);
702+
return PyIter.GetIter(this);
703703
}
704704

705705

src/runtime/pystring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public PyString(IntPtr ptr) : base(ptr)
2828

2929
private static IntPtr FromObject(PyObject o)
3030
{
31-
if (!IsStringType(o))
31+
if (o == null || !IsStringType(o))
3232
{
3333
throw new ArgumentException("object is not a string");
3434
}

src/runtime/pytuple.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal PyTuple(BorrowedReference reference) : base(reference) { }
3333

3434
private static IntPtr FromObject(PyObject o)
3535
{
36-
if (!IsTupleType(o))
36+
if (o == null || !IsTupleType(o))
3737
{
3838
throw new ArgumentException("object is not a tuple");
3939
}

0 commit comments

Comments
 (0)