diff --git a/CHANGELOG.md b/CHANGELOG.md index 766258c5d..ea0f1f7bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ and other `PyObject` derived types when called from Python. details about the cause of the failure - `clr.AddReference` no longer adds ".dll" implicitly - `PyIter(PyObject)` constructor replaced with static `PyIter.GetIter(PyObject)` method +- Python runtime can no longer be shut down if the Python error indicator is set, as it would have unpredictable behavior - BREAKING: Return values from .NET methods that return an interface are now automatically wrapped in that interface. This is a breaking change for users that rely on being able to access members that are part of the implementation class, but not the diff --git a/src/runtime/PythonEngine.cs b/src/runtime/PythonEngine.cs index 1e82446cb..598f07912 100644 --- a/src/runtime/PythonEngine.cs +++ b/src/runtime/PythonEngine.cs @@ -351,6 +351,12 @@ public static void Shutdown() { return; } + if (Exceptions.ErrorOccurred()) + { + throw new InvalidOperationException( + "Python error indicator is set", + innerException: PythonException.PeekCurrentOrNull(out _)); + } // If the shutdown handlers trigger a domain unload, // don't call shutdown again. AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload; diff --git a/src/runtime/PythonException.cs b/src/runtime/PythonException.cs index 813d0e586..092ffe90f 100644 --- a/src/runtime/PythonException.cs +++ b/src/runtime/PythonException.cs @@ -75,6 +75,23 @@ internal static PythonException FetchCurrentRaw() => FetchCurrentOrNullRaw() ?? throw new InvalidOperationException("No exception is set"); + internal static Exception? PeekCurrentOrNull(out ExceptionDispatchInfo? dispatchInfo) + { + using var _ = new Py.GILState(); + + Runtime.PyErr_Fetch(out var type, out var value, out var traceback); + Runtime.PyErr_Restore( + new NewReference(type, canBeNull: true).StealNullable(), + new NewReference(value, canBeNull: true).StealNullable(), + new NewReference(traceback, canBeNull: true).StealNullable()); + + var err = FetchCurrentOrNull(out dispatchInfo); + + Runtime.PyErr_Restore(type.StealNullable(), value.StealNullable(), traceback.StealNullable()); + + return err; + } + internal static Exception? FetchCurrentOrNull(out ExceptionDispatchInfo? dispatchInfo) { dispatchInfo = null;