FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use the return value to checking failure for save a C call. · amos402/pythonnet@850f0b0 · GitHub

Repository navigation

Commit 850f0b0

Browse files
committed
Use the return value to checking failure for save a C call.
Also make sure throw a exception even C level didn't set one.
1 parent c3e8fa1 commit 850f0b0

4 files changed

Lines changed: 53 additions & 13 deletions

File tree

‎src/runtime/pyansistring.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public PyAnsiString(PyObject o)
4545
public PyAnsiString(string s)
4646
{
4747
obj = Runtime.PyString_FromString(s);
48-
Runtime.CheckExceptionOccurred();
48+
if (obj == IntPtr.Zero)
49+
{
50+
throw new PythonException();
51+
}
4952
}
5053

5154

‎src/runtime/pyfloat.cs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public PyFloat(PyObject o)
5151
public PyFloat(double value)
5252
{
5353
obj = Runtime.PyFloat_FromDouble(value);
54-
Runtime.CheckExceptionOccurred();
54+
if (obj == IntPtr.Zero)
55+
{
56+
throw new PythonException();
57+
}
5558
}
5659

5760

@@ -66,7 +69,10 @@ public PyFloat(string value)
6669
using (var s = new PyString(value))
6770
{
6871
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
69-
Runtime.CheckExceptionOccurred();
72+
if (obj == IntPtr.Zero)
73+
{
74+
throw new PythonException();
75+
}
7076
}
7177
}
7278

@@ -94,7 +100,10 @@ public static bool IsFloatType(PyObject value)
94100
public static PyFloat AsFloat(PyObject value)
95101
{
96102
IntPtr op = Runtime.PyNumber_Float(value.obj);
97-
Runtime.CheckExceptionOccurred();
103+
if (op == IntPtr.Zero)
104+
{
105+
throw new PythonException();
106+
}
98107
return new PyFloat(op);
99108
}
100109
}

‎src/runtime/pyint.cs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public PyInt(PyObject o)
5151
public PyInt(int value)
5252
{
5353
obj = Runtime.PyInt_FromInt32(value);
54-
Runtime.CheckExceptionOccurred();
54+
if (obj == IntPtr.Zero)
55+
{
56+
throw new PythonException();
57+
}
5558
}
5659

5760

@@ -78,7 +81,10 @@ public PyInt(uint value) : base(IntPtr.Zero)
7881
public PyInt(long value) : base(IntPtr.Zero)
7982
{
8083
obj = Runtime.PyInt_FromInt64(value);
81-
Runtime.CheckExceptionOccurred();
84+
if (obj == IntPtr.Zero)
85+
{
86+
throw new PythonException();
87+
}
8288
}
8389

8490

@@ -92,7 +98,10 @@ public PyInt(long value) : base(IntPtr.Zero)
9298
public PyInt(ulong value) : base(IntPtr.Zero)
9399
{
94100
obj = Runtime.PyInt_FromInt64((long)value);
95-
Runtime.CheckExceptionOccurred();
101+
if (obj == IntPtr.Zero)
102+
{
103+
throw new PythonException();
104+
}
96105
}
97106

98107

@@ -151,7 +160,10 @@ public PyInt(sbyte value) : this((int)value)
151160
public PyInt(string value)
152161
{
153162
obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
154-
Runtime.CheckExceptionOccurred();
163+
if (obj == IntPtr.Zero)
164+
{
165+
throw new PythonException();
166+
}
155167
}
156168

157169

‎src/runtime/pytuple.cs‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public PyTuple(PyObject o)
5151
public PyTuple()
5252
{
5353
obj = Runtime.PyTuple_New(0);
54-
Runtime.CheckExceptionOccurred();
54+
if (obj == IntPtr.Zero)
55+
{
56+
throw new PythonException();
57+
}
5558
}
5659

5760

@@ -68,12 +71,22 @@ public PyTuple(PyObject[] items)
6871
{
6972
int count = items.Length;
7073
obj = Runtime.PyTuple_New(count);
71-
for (var i = 0; i < count; i++)
74+
if (obj == IntPtr.Zero)
75+
{
76+
throw new PythonException();
77+
}
78+
for (int i = 0; i < count; i++)
7279
{
7380
IntPtr ptr = items[i].obj;
7481
Runtime.XIncref(ptr);
75-
Runtime.PyTuple_SetItem(obj, i, ptr);
76-
Runtime.CheckExceptionOccurred();
82+
if (Runtime.PyTuple_SetItem(obj, i, ptr) != 0)
83+
{
84+
for (int j = i; j >= 0; j--)
85+
{
86+
Runtime.XDecref(items[j].obj);
87+
}
88+
throw new PythonException();
89+
}
7790
}
7891
}
7992

@@ -101,7 +114,10 @@ public static bool IsTupleType(PyObject value)
101114
public static PyTuple AsTuple(PyObject value)
102115
{
103116
IntPtr op = Runtime.PySequence_Tuple(value.obj);
104-
Runtime.CheckExceptionOccurred();
117+
if (op == IntPtr.Zero)
118+
{
119+
throw new PythonException();
120+
}
105121
return new PyTuple(op);
106122
}
107123
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL