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(); var typePtr = new NewReference(type.Reference); var strPtr = new NewReference(str.Reference); var tbPtr = new NewReference(Runtime.Runtime.None.Reference); Runtime.Runtime.PyErr_NormalizeException(ref typePtr, ref strPtr, ref tbPtr); using var typeObj = typePtr.MoveToPyObject(); using var strObj = strPtr.MoveToPyObject(); using var tbObj = tbPtr.MoveToPyObject(); // the type returned from PyErr_NormalizeException should not be the same type since a new // exception was raised by initializing the exception Assert.IsFalse(PythonReferenceComparer.Instance.Equals(type, typeObj)); // 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()); } }
public void MoveToPyObject_SetsNull() { var dict = new PyDict(); NewReference reference = Runtime.PyDict_Items(dict.Handle); try { Assert.IsFalse(reference.IsNull()); using (reference.MoveToPyObject()) Assert.IsTrue(reference.IsNull()); } finally { reference.Dispose(); } }