diff --git a/src/runtime/ClrBubbledException.cs b/src/runtime/ClrBubbledException.cs index 9a9bb45f9..836840067 100644 --- a/src/runtime/ClrBubbledException.cs +++ b/src/runtime/ClrBubbledException.cs @@ -35,7 +35,12 @@ public override string StackTrace { get { - return PythonTraceback + "Underlying exception stack trace:" + Environment.NewLine + InnerException.StackTrace; + if (HasPythonStacktrace) + { + return PythonTraceback + "Underlying exception stack trace:" + Environment.NewLine + InnerException.StackTrace; + } + + return InnerException.StackTrace; } } @@ -43,8 +48,12 @@ public override string ToString() { StringBuilder description = new StringBuilder(); description.AppendFormat("{0}: {1}{2}", InnerException.GetType().Name, Message, Environment.NewLine); - description.AppendFormat(" --> {0}", PythonTraceback); - description.AppendFormat(" --- End of Python traceback ---{0}", Environment.NewLine); + + if (HasPythonStacktrace) + { + description.AppendFormat(" --> {0}", PythonTraceback); + description.AppendFormat(" --- End of Python traceback ---{0}", Environment.NewLine); + } if (InnerException.InnerException != null) { @@ -58,5 +67,7 @@ public override string ToString() var str = description.ToString(); return str; } + + private bool HasPythonStacktrace => !string.IsNullOrEmpty(PythonTraceback); } } diff --git a/src/runtime/PythonException.cs b/src/runtime/PythonException.cs index 2b40b95d6..942f49013 100644 --- a/src/runtime/PythonException.cs +++ b/src/runtime/PythonException.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using System.Runtime.Serialization; @@ -188,7 +187,7 @@ private static Exception FromPyErr(BorrowedReference typeRef, BorrowedReference if (!(exception is null)) { using var _ = new Py.GILState(); - return new ClrBubbledException(exception, TracebackToString(traceback)); + return new ClrBubbledException(exception, !(traceback is null) ? TracebackToString(traceback) : ""); } using var cause = Runtime.PyException_GetCause(nValRef);