Skip to content

Commit

Permalink
Minor fix for null python traceback on clr bubbled exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Oct 26, 2023
1 parent 738527d commit 54ea2ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/runtime/ClrBubbledException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,25 @@ 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;
}
}

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)
{
Expand All @@ -58,5 +67,7 @@ public override string ToString()
var str = description.ToString();
return str;
}

private bool HasPythonStacktrace => !string.IsNullOrEmpty(PythonTraceback);
}
}
3 changes: 1 addition & 2 deletions src/runtime/PythonException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 54ea2ad

Please sign in to comment.