/// <summary> /// When called after SetError, sets the cause of the error. /// </summary> /// <param name="cause">The cause of the current error</param> public static void SetCause(Exception cause) { var currentException = PythonException.FetchCurrentRaw(); currentException.Normalize(); using var causeInstance = Converter.ToPython(cause); Runtime.PyException_SetCause(currentException.Value!.Reference, causeInstance.Steal()); currentException.Restore(); }
//==================================================================== // Internal helper methods for common error handling scenarios. //==================================================================== /// <summary> /// Raises a <see cref="TypeError"/> and attaches any existing exception as its cause. /// </summary> /// <param name="message">The exception message</param> /// <returns><c>null</c></returns> internal static NewReference RaiseTypeError(string message) { var cause = PythonException.FetchCurrentOrNullRaw(); cause?.Normalize(); Exceptions.SetError(Exceptions.TypeError, message); if (cause is null) return default; var typeError = PythonException.FetchCurrentRaw(); typeError.Normalize(); Runtime.PyException_SetCause( typeError.Value!, new NewReference(cause.Value!).Steal()); typeError.Restore(); return default; }