Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ public void InnerIsEmptyWithNoCause()
Assert.IsNull(ex.InnerException);
}

[Test]
public void TestErrorSetWithoutValue()
{
using var _ = Py.GIL();
// what PyErr_SetNone does, as C extensions use it: the error's value stays NULL until it is normalized
Runtime.Runtime.PyErr_SetObject(Exceptions.ValueError.Reference, BorrowedReference.Null);

var ex = Assert.Throws<PythonException>(() => PythonException.ThrowLastAsClrException());

Assert.AreEqual("ValueError", ex.Type.Name);
Assert.IsTrue(Runtime.Runtime.PyErr_Occurred().IsNull);
}

[Test]
public void TestPythonExceptionFormat()
{
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/PythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ internal static PythonException FetchCurrentRaw()

try
{
var normalizedValue = new NewReference(value.Borrow());
// an error set without a value, as PyErr_SetNone does, only gets one once normalized
var normalizedValue = new NewReference(value.BorrowNullable(), canBeNull: true);
Runtime.PyErr_NormalizeException(type: ref type, val: ref normalizedValue, tb: ref traceback);

return FromPyErr(typeRef: type.Borrow(), valRef: value.Borrow(), nValRef: normalizedValue.Borrow(), tbRef: traceback.BorrowNullable(), out dispatchInfo);
return FromPyErr(typeRef: type.Borrow(), valRef: value.IsNull() ? normalizedValue.Borrow() : value.Borrow(), nValRef: normalizedValue.Borrow(), tbRef: traceback.BorrowNullable(), out dispatchInfo);
}
finally
{
Expand Down
Loading