Skip to content

Commit

Permalink
explicitly raise an instanceless exception for testing in Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmsu committed Oct 3, 2023
1 parent 171cee4 commit b3a012a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ public void ExceptionDecodedNoInstance()
{
PyObjectConversions.RegisterDecoder(new InstancelessExceptionDecoder());
using var scope = Py.CreateScope();
var error = Assert.Throws<ValueErrorWrapper>(() => PythonEngine.Exec(
$"[].__iter__().__next__()"));
var error = Assert.Throws<ValueErrorWrapper>(() => PythonEngine.Exec(@$"
from {typeof(TestPythonException).Namespace} import {nameof(TestPythonException)}
{nameof(TestPythonException)}.{nameof(TestPythonException.RaiseInstanceless)}()"));
Assert.AreEqual(TestExceptionMessage, error.Message);
}

Expand Down
6 changes: 6 additions & 0 deletions src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,11 @@ public void TestPythonException_Normalize_ThrowsWhenErrorSet()
Assert.Throws<InvalidOperationException>(() => pythonException.Normalize());
Exceptions.Clear();
}

public static void RaiseInstanceless()
{
var stopIteration = new PyType(Exceptions.StopIteration);
throw new PythonException(stopIteration, value: null, traceback: null);
}
}
}
2 changes: 1 addition & 1 deletion src/runtime/PythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private static string TracebackToString(PyObject traceback)
/// <summary>Restores python error.</summary>
internal void Restore()
{
NewReference type = Type.NewReferenceOrNull();
NewReference type = new NewReference(Type);
NewReference value = Value.NewReferenceOrNull();
NewReference traceback = Traceback.NewReferenceOrNull();

Expand Down

0 comments on commit b3a012a

Please sign in to comment.