Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Next Next commit
Remove unnecessary CheckExceptionOccurred calls
  • Loading branch information
amos402 committed Jul 2, 2020
commit 03a401ef22f2110eac9b66929cc5d53042f51f82
9 changes: 6 additions & 3 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ public PyObject MoveToPyObject()
this.pointer = IntPtr.Zero;
return result;
}

/// <summary>
/// Removes this reference to a Python object, and sets it to <c>null</c>.
/// </summary>
public void Dispose()
{
if (!this.IsNull())
Runtime.XDecref(this.pointer);
this.pointer = IntPtr.Zero;
if (this.IsNull())
{
return;
}
Runtime.XDecref(this.pointer);
Comment thread
amos402 marked this conversation as resolved.
Outdated
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
goto type_error;
}
double dd = Runtime.PyFloat_AsDouble(op);
Runtime.CheckExceptionOccurred();
if (dd == -1.0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Knowing the funny behavior of floating-point comparisons, it would be good to have two test cases for this conversion passing and failing.

{
Runtime.CheckExceptionOccurred();
}
Runtime.XDecref(op);
if (dd > Single.MaxValue || dd < Single.MinValue)
{
Expand All @@ -777,7 +780,10 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
goto type_error;
}
double d = Runtime.PyFloat_AsDouble(op);
Runtime.CheckExceptionOccurred();
if (d == -1.0)
{
Runtime.CheckExceptionOccurred();
}
Runtime.XDecref(op);
result = d;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/pyansistring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public PyAnsiString(PyObject o)
public PyAnsiString(string s)
{
obj = Runtime.PyString_FromString(s);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand Down
6 changes: 3 additions & 3 deletions src/runtime/pyfloat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PyFloat(PyObject o)
public PyFloat(double value)
{
obj = Runtime.PyFloat_FromDouble(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -66,7 +66,7 @@ public PyFloat(string value)
using (var s = new PyString(value))
{
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ public static bool IsFloatType(PyObject value)
public static PyFloat AsFloat(PyObject value)
{
IntPtr op = Runtime.PyNumber_Float(value.obj);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(op);
return new PyFloat(op);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/runtime/pyint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PyInt(PyObject o)
public PyInt(int value)
{
obj = Runtime.PyInt_FromInt32(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -65,7 +65,7 @@ public PyInt(int value)
public PyInt(uint value)
{
obj = Runtime.PyInt_FromInt64(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -78,7 +78,7 @@ public PyInt(uint value)
public PyInt(long value)
{
obj = Runtime.PyInt_FromInt64(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -92,7 +92,7 @@ public PyInt(long value)
public PyInt(ulong value)
{
obj = Runtime.PyInt_FromInt64((long)value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand Down Expand Up @@ -151,7 +151,7 @@ public PyInt(sbyte value) : this((int)value)
public PyInt(string value)
{
obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -178,7 +178,7 @@ public static bool IsIntType(PyObject value)
public static PyInt AsInt(PyObject value)
{
IntPtr op = Runtime.PyNumber_Int(value.obj);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(op);
return new PyInt(op);
}

Expand Down
22 changes: 11 additions & 11 deletions src/runtime/pylong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PyLong(PyObject o)
public PyLong(int value)
{
obj = Runtime.PyLong_FromLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -65,7 +65,7 @@ public PyLong(int value)
public PyLong(uint value)
{
obj = Runtime.PyLong_FromLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -78,7 +78,7 @@ public PyLong(uint value)
public PyLong(long value)
{
obj = Runtime.PyLong_FromLongLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -92,7 +92,7 @@ public PyLong(long value)
public PyLong(ulong value)
{
obj = Runtime.PyLong_FromUnsignedLongLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -105,7 +105,7 @@ public PyLong(ulong value)
public PyLong(short value)
{
obj = Runtime.PyLong_FromLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -119,7 +119,7 @@ public PyLong(short value)
public PyLong(ushort value)
{
obj = Runtime.PyLong_FromLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -132,7 +132,7 @@ public PyLong(ushort value)
public PyLong(byte value)
{
obj = Runtime.PyLong_FromLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -146,7 +146,7 @@ public PyLong(byte value)
public PyLong(sbyte value)
{
obj = Runtime.PyLong_FromLong(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -159,7 +159,7 @@ public PyLong(sbyte value)
public PyLong(double value)
{
obj = Runtime.PyLong_FromDouble(value);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -172,7 +172,7 @@ public PyLong(double value)
public PyLong(string value)
{
obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand All @@ -199,7 +199,7 @@ public static bool IsLongType(PyObject value)
public static PyLong AsLong(PyObject value)
{
IntPtr op = Runtime.PyNumber_Long(value.obj);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(op);
return new PyLong(op);
}

Expand Down
33 changes: 8 additions & 25 deletions src/runtime/pyscope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ internal PyScope(IntPtr ptr, PyScopeManager manager)
obj = ptr;
//Refcount of the variables not increase
variables = Runtime.PyModule_GetDict(obj);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(variables);

Runtime.PyDict_SetItemString(
int res = Runtime.PyDict_SetItemString(
variables, "__builtins__",
Runtime.PyEval_GetBuiltins()
);
PythonException.ThrowIfIsNotZero(res);
this.Name = this.Get<string>("__name__");
}

Expand Down Expand Up @@ -237,7 +238,7 @@ public PyObject Execute(PyObject script, PyDict locals = null)
Check();
IntPtr _locals = locals == null ? variables : locals.obj;
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, variables, _locals);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(ptr);
if (ptr == Runtime.PyNone)
{
Runtime.XDecref(ptr);
Expand Down Expand Up @@ -282,15 +283,8 @@ public PyObject Eval(string code, PyDict locals = null)
NewReference reference = Runtime.PyRun_String(
code, flag, variables, _locals
);
try
{
Runtime.CheckExceptionOccurred();
return reference.MoveToPyObject();
}
finally
{
reference.Dispose();
}
PythonException.ThrowIfIsNull(reference);
return reference.MoveToPyObject();
}

/// <summary>
Expand Down Expand Up @@ -327,19 +321,8 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
NewReference reference = Runtime.PyRun_String(
code, flag, _globals, _locals
);

try
{
Runtime.CheckExceptionOccurred();
if (!reference.IsNone())
{
throw new PythonException();
}
}
finally
{
reference.Dispose();
}
PythonException.ThrowIfIsNull(reference);
reference.Dispose();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/pystring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public PyString(PyObject o)
public PyString(string s)
{
obj = Runtime.PyUnicode_FromUnicode(s, s.Length);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(obj);
}


Expand Down
23 changes: 7 additions & 16 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public static void EndAllowThreads(IntPtr ts)
public static PyObject ImportModule(string name)
{
IntPtr op = Runtime.PyImport_ImportModule(name);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(op);
return new PyObject(op);
}

Expand All @@ -480,7 +480,7 @@ public static PyObject ImportModule(string name)
public static PyObject ReloadModule(PyObject module)
{
IntPtr op = Runtime.PyImport_ReloadModule(module.Handle);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(op);
return new PyObject(op);
}

Expand All @@ -495,17 +495,17 @@ public static PyObject ReloadModule(PyObject module)
public static PyObject ModuleFromString(string name, string code)
{
IntPtr c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(c);
IntPtr m = Runtime.PyImport_ExecCodeModule(name, c);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(m);
return new PyObject(m);
}

public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
{
var flag = (int)mode;
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
Runtime.CheckExceptionOccurred();
PythonException.ThrowIfIsNull(ptr);
return new PyObject(ptr);
}

Expand Down Expand Up @@ -587,17 +587,8 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
NewReference result = Runtime.PyRun_String(
code, (IntPtr)flag, globals.Value, locals.Value
);

try
{
Runtime.CheckExceptionOccurred();

return result.MoveToPyObject();
}
finally
{
result.Dispose();
}
PythonException.ThrowIfIsNull(result);
return result.MoveToPyObject();
}
finally
{
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/pythonexception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ public static void ThrowIfIsNull(IntPtr ob)
}
}

internal static void ThrowIfIsNull(in NewReference reference)
Comment thread
amos402 marked this conversation as resolved.
Outdated
{
if (reference.IsNull())
{
throw new PythonException();
}
}

public static void ThrowIfIsNotZero(int value)
{
if (value != 0)
Expand Down
Loading