8000 Call PyErr_NormalizeException for exceptions by slide · Pull Request #1265 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix test for exception during normalization
  • Loading branch information
slide committed Dec 14, 2020
commit c28951f086dbc845a4b1b498b4bae52aa2ceef90
38 changes: 28 additions & 10 deletions src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,40 @@ public void TestPythonExceptionFormatNormalized()
}

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

raise TestException('foo')
");
}
catch(PythonException ex)
{
Assert.AreEqual("Traceback (most recent call last):\n File \"<string>\", line 7, in <module>\n File \"<string>\", line 5, in __init__\nValueError: invalid literal for int() with base 10: 'foo'\n", ex.Format());
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());
}
}
}
}
}
}
Expand Down
0