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

Skip to content

Call PyErr_NormalizeException for exceptions #1265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ details about the cause of the failure
- Indexers can now be used with interface objects
- Fixed a bug where indexers could not be used if they were inherited
- Made it possible to use `__len__` also on `ICollection<>` interface objects
- Fixed issue when calling PythonException.Format where another exception would be raise for unnormalized exceptions
- Made it possible to call `ToString`, `GetHashCode`, and `GetType` on inteface objects
- Fixed objects returned by enumerating `PyObject` being disposed too soon
- Incorrectly using a non-generic type with type parameters now produces a helpful Python error instead of throwing NullReferenceException
Expand Down
53 changes: 52 additions & 1 deletion src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,60 @@ public void TestPythonExceptionFormatNoTraceback()
}
catch (PythonException ex)
{
// ImportError/ModuleNotFoundError do not have a traceback when not running in a script
// ImportError/ModuleNotFoundError do not have a traceback when not running in a script
Assert.AreEqual(ex.StackTrace, ex.Format());
}
}

[Test]
public void TestPythonExceptionFormatNormalized()
{
try
{
PythonEngine.Exec("a=b\n");
}
catch (PythonException ex)
{
Assert.AreEqual("Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\nNameError: name 'b' is not defined\n", ex.Format());
}
}

[Test]
public void TestPythonException_PyErr_NormalizeException()
{
using (Py.GIL())
{
using (PythonEngine engine = new PythonEngine())
{
var scope = Py.CreateScope();
scope.Exec(@"
class TestException(NameError):
def __init__(self, val):
super().__init__(val)
x = int(val)");

if (scope.TryGet("TestException", out PyObject type))
{
PyObject str = "dummy string".ToPython();
IntPtr typePtr = type.Handle;
IntPtr strPtr = str.Handle;
IntPtr tbPtr = Runtime.Runtime.None.Handle;
Runtime.Runtime.XIncref(typePtr);
Runtime.Runtime.XIncref(strPtr);
Runtime.Runtime.XIncref(tbPtr);
Runtime.Runtime.PyErr_NormalizeException(ref typePtr, ref strPtr, ref tbPtr);

using(PyObject typeObj = new PyObject(typePtr), strObj = new PyObject(strPtr), tbObj = new PyObject(tbPtr))
{
// the type returned from PyErr_NormalizeException should not be the same type since a new
// exception was raised by initializing the exception
Assert.AreNotEqual(type.Handle, typePtr);
// the message should now be the string from the throw exception during normalization
Assert.AreEqual("invalid literal for int() with base 10: 'dummy string'", strObj.ToString());
}
}
}
}
}
}
}
18 changes: 12 additions & 6 deletions src/runtime/pythonexception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,18 @@ public string Format()
{
if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
{
Runtime.XIncref(_pyType);
Runtime.XIncref(_pyValue);
Runtime.XIncref(_pyTB);
using (PyObject pyType = new PyObject(_pyType))
using (PyObject pyValue = new PyObject(_pyValue))
using (PyObject pyTB = new PyObject(_pyTB))
IntPtr tb = _pyTB;
IntPtr type = _pyType;
IntPtr value = _pyValue;

Runtime.XIncref(type);
Runtime.XIncref(value);
Runtime.XIncref(tb);
Runtime.PyErr_NormalizeException(ref type, ref value, ref tb);

using (PyObject pyType = new PyObject(type))
using (PyObject pyValue = new PyObject(value))
using (PyObject pyTB = new PyObject(tb))
using (PyObject tb_mod = PythonEngine.ImportModule("traceback"))
{
var buffer = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
internal static extern int PyErr_GivenExceptionMatches(IntPtr ob, IntPtr val);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void PyErr_NormalizeException(IntPtr ob, IntPtr val, IntPtr tb);
internal static extern void PyErr_NormalizeException(ref IntPtr ob, ref IntPtr val, ref IntPtr tb);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyErr_Occurred();
Expand Down