File tree Expand file tree Collapse file tree 10 files changed +30
-12
lines changed Expand file tree Collapse file tree 10 files changed +30
-12
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public PyAnsiString(IntPtr ptr) : base(ptr)
19
19
20
20
private static IntPtr FromObject ( PyObject o )
21
21
{
22
- if ( ! IsStringType ( o ) )
22
+ if ( o == null || ! IsStringType ( o ) )
23
23
{
24
24
throw new ArgumentException ( "object is not a string" ) ;
25
25
}
Original file line number Diff line number Diff line change @@ -48,11 +48,11 @@ public PyDict() : base(Runtime.PyDict_New())
48
48
/// </remarks>
49
49
public PyDict ( PyObject o ) : base ( o . obj )
50
50
{
51
+ Runtime . XIncref ( o . obj ) ;
51
52
if ( ! IsDictType ( o ) )
52
53
{
53
54
throw new ArgumentException ( "object is not a dict" ) ;
54
55
}
55
- Runtime . XIncref ( o . obj ) ;
56
56
}
57
57
58
58
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ public PyFloat(double value) : base(FromDouble(value))
47
47
48
48
private static IntPtr FromObject ( PyObject o )
49
49
{
50
- if ( ! IsFloatType ( o ) )
50
+ if ( o == null || ! IsFloatType ( o ) )
51
51
{
52
52
throw new ArgumentException ( "object is not a float" ) ;
53
53
}
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public PyInt(PyObject o) : base(FromObject(o))
37
37
38
38
private static IntPtr FromObject ( PyObject o )
39
39
{
40
- if ( ! IsIntType ( o ) )
40
+ if ( o == null || ! IsIntType ( o ) )
41
41
{
42
42
throw new ArgumentException ( "object is not an int" ) ;
43
43
}
Original file line number Diff line number Diff line change @@ -27,6 +27,10 @@ public PyIter(IntPtr ptr) : base(ptr)
27
27
28
28
private static IntPtr FromObject ( PyObject iterable )
29
29
{
30
+ if ( iterable == null )
31
+ {
32
+ throw new NullReferenceException ( ) ;
33
+ }
30
34
IntPtr val = Runtime . PyObject_GetIter ( iterable . obj ) ;
31
35
if ( val == IntPtr . Zero )
32
36
{
@@ -35,14 +39,27 @@ private static IntPtr FromObject(PyObject iterable)
35
39
return val ;
36
40
}
37
41
42
+
38
43
/// <summary>
39
- /// PyIter Constructor
44
+ /// PyIter factory function.
40
45
/// </summary>
41
46
/// <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.
43
48
/// </remarks>
44
- public PyIter ( PyObject iterable ) : base ( FromObject ( iterable ) )
49
+ /// <param name="iterable"></param>
50
+ /// <returns></returns>
51
+ public static PyIter GetIter ( PyObject iterable )
45
52
{
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 ) ;
46
63
}
47
64
48
65
protected override void Dispose ( bool disposing )
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ internal PyList(BorrowedReference reference) : base(reference) { }
30
30
31
31
private static IntPtr FromObject ( PyObject o )
32
32
{
33
- if ( ! IsListType ( o ) )
33
+ if ( o == null || ! IsListType ( o ) )
34
34
{
35
35
throw new ArgumentException ( "object is not a list" ) ;
36
36
}
@@ -76,6 +76,7 @@ private static IntPtr FromArray(PyObject[] items)
76
76
int r = Runtime . PyList_SetItem ( val , i , ptr ) ;
77
77
if ( r < 0 )
78
78
{
79
+ Runtime . Py_DecRef ( val ) ;
79
80
throw new PythonException ( ) ;
80
81
}
81
82
}
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public PyLong(PyObject o) : base(FromObject(o))
37
37
38
38
private static IntPtr FromObject ( PyObject o )
39
39
{
40
- if ( ! IsLongType ( o ) )
40
+ if ( o == null || ! IsLongType ( o ) )
41
41
{
42
42
throw new ArgumentException ( "object is not a long" ) ;
43
43
}
Original file line number Diff line number Diff line change @@ -699,7 +699,7 @@ public PyObject GetIterator()
699
699
/// </remarks>
700
700
public IEnumerator GetEnumerator ( )
701
701
{
702
- return new PyIter ( this ) ;
702
+ return PyIter . GetIter ( this ) ;
703
703
}
704
704
705
705
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ public PyString(IntPtr ptr) : base(ptr)
28
28
29
29
private static IntPtr FromObject ( PyObject o )
30
30
{
31
- if ( ! IsStringType ( o ) )
31
+ if ( o == null || ! IsStringType ( o ) )
32
32
{
33
33
throw new ArgumentException ( "object is not a string" ) ;
34
34
}
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ internal PyTuple(BorrowedReference reference) : base(reference) { }
33
33
34
34
private static IntPtr FromObject ( PyObject o )
35
35
{
36
- if ( ! IsTupleType ( o ) )
36
+ if ( o == null || ! IsTupleType ( o ) )
37
37
{
38
38
throw new ArgumentException ( "object is not a tuple" ) ;
39
39
}
You can’t perform that action at this time.
0 commit comments