Skip to content

Commit 03a401e

Browse files
committed
Remove unnecessary CheckExceptionOccurred calls
1 parent 7a9dcfa commit 03a401e

File tree

11 files changed

+63
-72
lines changed

11 files changed

+63
-72
lines changed

src/runtime/NewReference.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ public PyObject MoveToPyObject()
2727
this.pointer = IntPtr.Zero;
2828
return result;
2929
}
30+
3031
/// <summary>
3132
/// Removes this reference to a Python object, and sets it to <c>null</c>.
3233
/// </summary>
3334
public void Dispose()
3435
{
35-
if (!this.IsNull())
36-
Runtime.XDecref(this.pointer);
37-
this.pointer = IntPtr.Zero;
36+
if (this.IsNull())
37+
{
38+
return;
39+
}
40+
Runtime.XDecref(this.pointer);
3841
}
3942

4043
/// <summary>

src/runtime/converter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,10 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
758758
goto type_error;
759759
}
760760
double dd = Runtime.PyFloat_AsDouble(op);
761-
Runtime.CheckExceptionOccurred();
761+
if (dd == -1.0)
762+
{
763+
Runtime.CheckExceptionOccurred();
764+
}
762765
Runtime.XDecref(op);
763766
if (dd > Single.MaxValue || dd < Single.MinValue)
764767
{
@@ -777,7 +780,10 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
777780
goto type_error;
778781
}
779782
double d = Runtime.PyFloat_AsDouble(op);
780-
Runtime.CheckExceptionOccurred();
783+
if (d == -1.0)
784+
{
785+
Runtime.CheckExceptionOccurred();
786+
}
781787
Runtime.XDecref(op);
782788
result = d;
783789
return true;

src/runtime/pyansistring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public PyAnsiString(PyObject o)
4545
public PyAnsiString(string s)
4646
{
4747
obj = Runtime.PyString_FromString(s);
48-
Runtime.CheckExceptionOccurred();
48+
PythonException.ThrowIfIsNull(obj);
4949
}
5050

5151

src/runtime/pyfloat.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public PyFloat(PyObject o)
5151
public PyFloat(double value)
5252
{
5353
obj = Runtime.PyFloat_FromDouble(value);
54-
Runtime.CheckExceptionOccurred();
54+
PythonException.ThrowIfIsNull(obj);
5555
}
5656

5757

@@ -66,7 +66,7 @@ public PyFloat(string value)
6666
using (var s = new PyString(value))
6767
{
6868
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
69-
Runtime.CheckExceptionOccurred();
69+
PythonException.ThrowIfIsNull(obj);
7070
}
7171
}
7272

@@ -94,7 +94,7 @@ public static bool IsFloatType(PyObject value)
9494
public static PyFloat AsFloat(PyObject value)
9595
{
9696
IntPtr op = Runtime.PyNumber_Float(value.obj);
97-
Runtime.CheckExceptionOccurred();
97+
PythonException.ThrowIfIsNull(op);
9898
return new PyFloat(op);
9999
}
100100
}

src/runtime/pyint.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public PyInt(PyObject o)
5151
public PyInt(int value)
5252
{
5353
obj = Runtime.PyInt_FromInt32(value);
54-
Runtime.CheckExceptionOccurred();
54+
PythonException.ThrowIfIsNull(obj);
5555
}
5656

5757

@@ -65,7 +65,7 @@ public PyInt(int value)
6565
public PyInt(uint value)
6666
{
6767
obj = Runtime.PyInt_FromInt64(value);
68-
Runtime.CheckExceptionOccurred();
68+
PythonException.ThrowIfIsNull(obj);
6969
}
7070

7171

@@ -78,7 +78,7 @@ public PyInt(uint value)
7878
public PyInt(long value)
7979
{
8080
obj = Runtime.PyInt_FromInt64(value);
81-
Runtime.CheckExceptionOccurred();
81+
PythonException.ThrowIfIsNull(obj);
8282
}
8383

8484

@@ -92,7 +92,7 @@ public PyInt(long value)
9292
public PyInt(ulong value)
9393
{
9494
obj = Runtime.PyInt_FromInt64((long)value);
95-
Runtime.CheckExceptionOccurred();
95+
PythonException.ThrowIfIsNull(obj);
9696
}
9797

9898

@@ -151,7 +151,7 @@ public PyInt(sbyte value) : this((int)value)
151151
public PyInt(string value)
152152
{
153153
obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
154-
Runtime.CheckExceptionOccurred();
154+
PythonException.ThrowIfIsNull(obj);
155155
}
156156

157157

@@ -178,7 +178,7 @@ public static bool IsIntType(PyObject value)
178178
public static PyInt AsInt(PyObject value)
179179
{
180180
IntPtr op = Runtime.PyNumber_Int(value.obj);
181-
Runtime.CheckExceptionOccurred();
181+
PythonException.ThrowIfIsNull(op);
182182
return new PyInt(op);
183183
}
184184

src/runtime/pylong.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public PyLong(PyObject o)
5151
public PyLong(int value)
5252
{
5353
obj = Runtime.PyLong_FromLong(value);
54-
Runtime.CheckExceptionOccurred();
54+
PythonException.ThrowIfIsNull(obj);
5555
}
5656

5757

@@ -65,7 +65,7 @@ public PyLong(int value)
6565
public PyLong(uint value)
6666
{
6767
obj = Runtime.PyLong_FromLong(value);
68-
Runtime.CheckExceptionOccurred();
68+
PythonException.ThrowIfIsNull(obj);
6969
}
7070

7171

@@ -78,7 +78,7 @@ public PyLong(uint value)
7878
public PyLong(long value)
7979
{
8080
obj = Runtime.PyLong_FromLongLong(value);
81-
Runtime.CheckExceptionOccurred();
81+
PythonException.ThrowIfIsNull(obj);
8282
}
8383

8484

@@ -92,7 +92,7 @@ public PyLong(long value)
9292
public PyLong(ulong value)
9393
{
9494
obj = Runtime.PyLong_FromUnsignedLongLong(value);
95-
Runtime.CheckExceptionOccurred();
95+
PythonException.ThrowIfIsNull(obj);
9696
}
9797

9898

@@ -105,7 +105,7 @@ public PyLong(ulong value)
105105
public PyLong(short value)
106106
{
107107
obj = Runtime.PyLong_FromLong(value);
108-
Runtime.CheckExceptionOccurred();
108+
PythonException.ThrowIfIsNull(obj);
109109
}
110110

111111

@@ -119,7 +119,7 @@ public PyLong(short value)
119119
public PyLong(ushort value)
120120
{
121121
obj = Runtime.PyLong_FromLong(value);
122-
Runtime.CheckExceptionOccurred();
122+
PythonException.ThrowIfIsNull(obj);
123123
}
124124

125125

@@ -132,7 +132,7 @@ public PyLong(ushort value)
132132
public PyLong(byte value)
133133
{
134134
obj = Runtime.PyLong_FromLong(value);
135-
Runtime.CheckExceptionOccurred();
135+
PythonException.ThrowIfIsNull(obj);
136136
}
137137

138138

@@ -146,7 +146,7 @@ public PyLong(byte value)
146146
public PyLong(sbyte value)
147147
{
148148
obj = Runtime.PyLong_FromLong(value);
149-
Runtime.CheckExceptionOccurred();
149+
PythonException.ThrowIfIsNull(obj);
150150
}
151151

152152

@@ -159,7 +159,7 @@ public PyLong(sbyte value)
159159
public PyLong(double value)
160160
{
161161
obj = Runtime.PyLong_FromDouble(value);
162-
Runtime.CheckExceptionOccurred();
162+
PythonException.ThrowIfIsNull(obj);
163163
}
164164

165165

@@ -172,7 +172,7 @@ public PyLong(double value)
172172
public PyLong(string value)
173173
{
174174
obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
175-
Runtime.CheckExceptionOccurred();
175+
PythonException.ThrowIfIsNull(obj);
176176
}
177177

178178

@@ -199,7 +199,7 @@ public static bool IsLongType(PyObject value)
199199
public static PyLong AsLong(PyObject value)
200200
{
201201
IntPtr op = Runtime.PyNumber_Long(value.obj);
202-
Runtime.CheckExceptionOccurred();
202+
PythonException.ThrowIfIsNull(op);
203203
return new PyLong(op);
204204
}
205205

src/runtime/pyscope.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ internal PyScope(IntPtr ptr, PyScopeManager manager)
6666
obj = ptr;
6767
//Refcount of the variables not increase
6868
variables = Runtime.PyModule_GetDict(obj);
69-
Runtime.CheckExceptionOccurred();
69+
PythonException.ThrowIfIsNull(variables);
7070

71-
Runtime.PyDict_SetItemString(
71+
int res = Runtime.PyDict_SetItemString(
7272
variables, "__builtins__",
7373
Runtime.PyEval_GetBuiltins()
7474
);
75+
PythonException.ThrowIfIsNotZero(res);
7576
this.Name = this.Get<string>("__name__");
7677
}
7778

@@ -237,7 +238,7 @@ public PyObject Execute(PyObject script, PyDict locals = null)
237238
Check();
238239
IntPtr _locals = locals == null ? variables : locals.obj;
239240
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, variables, _locals);
240-
Runtime.CheckExceptionOccurred();
241+
PythonException.ThrowIfIsNull(ptr);
241242
if (ptr == Runtime.PyNone)
242243
{
243244
Runtime.XDecref(ptr);
@@ -282,15 +283,8 @@ public PyObject Eval(string code, PyDict locals = null)
282283
NewReference reference = Runtime.PyRun_String(
283284
code, flag, variables, _locals
284285
);
285-
try
286-
{
287-
Runtime.CheckExceptionOccurred();
288-
return reference.MoveToPyObject();
289-
}
290-
finally
291-
{
292-
reference.Dispose();
293-
}
286+
PythonException.ThrowIfIsNull(reference);
287+
return reference.MoveToPyObject();
294288
}
295289

296290
/// <summary>
@@ -327,19 +321,8 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
327321
NewReference reference = Runtime.PyRun_String(
328322
code, flag, _globals, _locals
329323
);
330-
331-
try
332-
{
333-
Runtime.CheckExceptionOccurred();
334-
if (!reference.IsNone())
335-
{
336-
throw new PythonException();
337-
}
338-
}
339-
finally
340-
{
341-
reference.Dispose();
342-
}
324+
PythonException.ThrowIfIsNull(reference);
325+
reference.Dispose();
343326
}
344327

345328
/// <summary>

src/runtime/pystring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public PyString(PyObject o)
5454
public PyString(string s)
5555
{
5656
obj = Runtime.PyUnicode_FromUnicode(s, s.Length);
57-
Runtime.CheckExceptionOccurred();
57+
PythonException.ThrowIfIsNull(obj);
5858
}
5959

6060

src/runtime/pythonengine.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public static void EndAllowThreads(IntPtr ts)
465465
public static PyObject ImportModule(string name)
466466
{
467467
IntPtr op = Runtime.PyImport_ImportModule(name);
468-
Runtime.CheckExceptionOccurred();
468+
PythonException.ThrowIfIsNull(op);
469469
return new PyObject(op);
470470
}
471471

@@ -480,7 +480,7 @@ public static PyObject ImportModule(string name)
480480
public static PyObject ReloadModule(PyObject module)
481481
{
482482
IntPtr op = Runtime.PyImport_ReloadModule(module.Handle);
483-
Runtime.CheckExceptionOccurred();
483+
PythonException.ThrowIfIsNull(op);
484484
return new PyObject(op);
485485
}
486486

@@ -495,17 +495,17 @@ public static PyObject ReloadModule(PyObject module)
495495
public static PyObject ModuleFromString(string name, string code)
496496
{
497497
IntPtr c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
498-
Runtime.CheckExceptionOccurred();
498+
PythonException.ThrowIfIsNull(c);
499499
IntPtr m = Runtime.PyImport_ExecCodeModule(name, c);
500-
Runtime.CheckExceptionOccurred();
500+
PythonException.ThrowIfIsNull(m);
501501
return new PyObject(m);
502502
}
503503

504504
public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
505505
{
506506
var flag = (int)mode;
507507
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
508-
Runtime.CheckExceptionOccurred();
508+
PythonException.ThrowIfIsNull(ptr);
509509
return new PyObject(ptr);
510510
}
511511

@@ -587,17 +587,8 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
587587
NewReference result = Runtime.PyRun_String(
588588
code, (IntPtr)flag, globals.Value, locals.Value
589589
);
590-
591-
try
592-
{
593-
Runtime.CheckExceptionOccurred();
594-
595-
return result.MoveToPyObject();
596-
}
597-
finally
598-
{
599-
result.Dispose();
600-
}
590+
PythonException.ThrowIfIsNull(result);
591+
return result.MoveToPyObject();
601592
}
602593
finally
603594
{

src/runtime/pythonexception.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ public static void ThrowIfIsNull(IntPtr ob)
253253
}
254254
}
255255

256+
internal static void ThrowIfIsNull(in NewReference reference)
257+
{
258+
if (reference.IsNull())
259+
{
260+
throw new PythonException();
261+
}
262+
}
263+
256264
public static void ThrowIfIsNotZero(int value)
257265
{
258266
if (value != 0)

0 commit comments

Comments
 (0)