diff --git a/CHANGELOG.md b/CHANGELOG.md index 2420d40ea..1442075ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/embed_tests/TestPythonException.cs b/src/embed_tests/TestPythonException.cs index 000c32ca3..3ab0f8106 100644 --- a/src/embed_tests/TestPythonException.cs +++ b/src/embed_tests/TestPythonException.cs @@ -86,9 +86,54 @@ 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 \"\", line 1, in \nNameError: name 'b' is not defined\n", ex.Format()); + } + } + + [Test] + public void TestPythonException_PyErr_NormalizeException() + { + using (var scope = Py.CreateScope()) + { + scope.Exec(@" +class TestException(NameError): + def __init__(self, val): + super().__init__(val) + x = int(val)"); + Assert.IsTrue(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()); + } + } + } } } diff --git a/src/runtime/pythonexception.cs b/src/runtime/pythonexception.cs index 97a80bc76..eff33d699 100644 --- a/src/runtime/pythonexception.cs +++ b/src/runtime/pythonexception.cs @@ -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(); diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 9204f04b6..f80db04b6 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -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(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy