Skip to content

Commit 6e4cf9d

Browse files
committed
Refactor Exception checking on tested classes
1 parent 977ee96 commit 6e4cf9d

File tree

5 files changed

+22
-88
lines changed

5 files changed

+22
-88
lines changed

src/runtime/pyansistring.cs

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

5451

src/runtime/pyfloat.cs

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

6057

@@ -69,10 +66,7 @@ public PyFloat(string value)
6966
using (var s = new PyString(value))
7067
{
7168
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
72-
if (obj == IntPtr.Zero)
73-
{
74-
throw new PythonException();
75-
}
69+
Runtime.CheckExceptionOccurred();
7670
}
7771
}
7872

@@ -100,10 +94,7 @@ public static bool IsFloatType(PyObject value)
10094
public static PyFloat AsFloat(PyObject value)
10195
{
10296
IntPtr op = Runtime.PyNumber_Float(value.obj);
103-
if (op == IntPtr.Zero)
104-
{
105-
throw new PythonException();
106-
}
97+
Runtime.CheckExceptionOccurred();
10798
return new PyFloat(op);
10899
}
109100
}

src/runtime/pyint.cs

+6-24
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public PyInt(PyObject o)
5151
public PyInt(int value)
5252
{
5353
obj = Runtime.PyInt_FromInt32(value);
54-
if (obj == IntPtr.Zero)
55-
{
56-
throw new PythonException();
57-
}
54+
Runtime.CheckExceptionOccurred();
5855
}
5956

6057

@@ -68,10 +65,7 @@ public PyInt(int value)
6865
public PyInt(uint value) : base(IntPtr.Zero)
6966
{
7067
obj = Runtime.PyInt_FromInt64(value);
71-
if (obj == IntPtr.Zero)
72-
{
73-
throw new PythonException();
74-
}
68+
Runtime.CheckExceptionOccurred();
7569
}
7670

7771

@@ -84,10 +78,7 @@ public PyInt(uint value) : base(IntPtr.Zero)
8478
public PyInt(long value) : base(IntPtr.Zero)
8579
{
8680
obj = Runtime.PyInt_FromInt64(value);
87-
if (obj == IntPtr.Zero)
88-
{
89-
throw new PythonException();
90-
}
81+
Runtime.CheckExceptionOccurred();
9182
}
9283

9384

@@ -101,10 +92,7 @@ public PyInt(long value) : base(IntPtr.Zero)
10192
public PyInt(ulong value) : base(IntPtr.Zero)
10293
{
10394
obj = Runtime.PyInt_FromInt64((long)value);
104-
if (obj == IntPtr.Zero)
105-
{
106-
throw new PythonException();
107-
}
95+
Runtime.CheckExceptionOccurred();
10896
}
10997

11098

@@ -163,10 +151,7 @@ public PyInt(sbyte value) : this((int)value)
163151
public PyInt(string value)
164152
{
165153
obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
166-
if (obj == IntPtr.Zero)
167-
{
168-
throw new PythonException();
169-
}
154+
Runtime.CheckExceptionOccurred();
170155
}
171156

172157

@@ -193,10 +178,7 @@ public static bool IsIntType(PyObject value)
193178
public static PyInt AsInt(PyObject value)
194179
{
195180
IntPtr op = Runtime.PyNumber_Int(value.obj);
196-
if (op == IntPtr.Zero)
197-
{
198-
throw new PythonException();
199-
}
181+
Runtime.CheckExceptionOccurred();
200182
return new PyInt(op);
201183
}
202184

src/runtime/pylong.cs

+11-44
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public PyLong(PyObject o)
5151
public PyLong(int value)
5252
{
5353
obj = Runtime.PyLong_FromLong(value);
54-
if (obj == IntPtr.Zero)
55-
{
56-
throw new PythonException();
57-
}
54+
Runtime.CheckExceptionOccurred();
5855
}
5956

6057

@@ -68,10 +65,7 @@ public PyLong(int value)
6865
public PyLong(uint value)
6966
{
7067
obj = Runtime.PyLong_FromLong(value);
71-
if (obj == IntPtr.Zero)
72-
{
73-
throw new PythonException();
74-
}
68+
Runtime.CheckExceptionOccurred();
7569
}
7670

7771

@@ -84,10 +78,7 @@ public PyLong(uint value)
8478
public PyLong(long value)
8579
{
8680
obj = Runtime.PyLong_FromLongLong(value);
87-
if (obj == IntPtr.Zero)
88-
{
89-
throw new PythonException();
90-
}
81+
Runtime.CheckExceptionOccurred();
9182
}
9283

9384

@@ -101,10 +92,7 @@ public PyLong(long value)
10192
public PyLong(ulong value)
10293
{
10394
obj = Runtime.PyLong_FromUnsignedLongLong(value);
104-
if (obj == IntPtr.Zero)
105-
{
106-
throw new PythonException();
107-
}
95+
Runtime.CheckExceptionOccurred();
10896
}
10997

11098

@@ -117,10 +105,7 @@ public PyLong(ulong value)
117105
public PyLong(short value)
118106
{
119107
obj = Runtime.PyLong_FromLong(value);
120-
if (obj == IntPtr.Zero)
121-
{
122-
throw new PythonException();
123-
}
108+
Runtime.CheckExceptionOccurred();
124109
}
125110

126111

@@ -134,10 +119,7 @@ public PyLong(short value)
134119
public PyLong(ushort value)
135120
{
136121
obj = Runtime.PyLong_FromLong(value);
137-
if (obj == IntPtr.Zero)
138-
{
139-
throw new PythonException();
140-
}
122+
Runtime.CheckExceptionOccurred();
141123
}
142124

143125

@@ -150,10 +132,7 @@ public PyLong(ushort value)
150132
public PyLong(byte value)
151133
{
152134
obj = Runtime.PyLong_FromLong(value);
153-
if (obj == IntPtr.Zero)
154-
{
155-
throw new PythonException();
156-
}
135+
Runtime.CheckExceptionOccurred();
157136
}
158137

159138

@@ -167,10 +146,7 @@ public PyLong(byte value)
167146
public PyLong(sbyte value)
168147
{
169148
obj = Runtime.PyLong_FromLong(value);
170-
if (obj == IntPtr.Zero)
171-
{
172-
throw new PythonException();
173-
}
149+
Runtime.CheckExceptionOccurred();
174150
}
175151

176152

@@ -183,10 +159,7 @@ public PyLong(sbyte value)
183159
public PyLong(double value)
184160
{
185161
obj = Runtime.PyLong_FromDouble(value);
186-
if (obj == IntPtr.Zero)
187-
{
188-
throw new PythonException();
189-
}
162+
Runtime.CheckExceptionOccurred();
190163
}
191164

192165

@@ -199,10 +172,7 @@ public PyLong(double value)
199172
public PyLong(string value)
200173
{
201174
obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
202-
if (obj == IntPtr.Zero)
203-
{
204-
throw new PythonException();
205-
}
175+
Runtime.CheckExceptionOccurred();
206176
}
207177

208178

@@ -229,10 +199,7 @@ public static bool IsLongType(PyObject value)
229199
public static PyLong AsLong(PyObject value)
230200
{
231201
IntPtr op = Runtime.PyNumber_Long(value.obj);
232-
if (op == IntPtr.Zero)
233-
{
234-
throw new PythonException();
235-
}
202+
Runtime.CheckExceptionOccurred();
236203
return new PyLong(op);
237204
}
238205

src/runtime/pystring.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public PyString(PyObject o)
5454
public PyString(string s)
5555
{
5656
obj = Runtime.PyUnicode_FromUnicode(s, s.Length);
57-
if (obj == IntPtr.Zero)
58-
{
59-
throw new PythonException();
60-
}
57+
Runtime.CheckExceptionOccurred();
6158
}
6259

6360

0 commit comments

Comments
 (0)