Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix exception stacktrace formatting
Ensure that the stacktrace list is joined into a single string and
adjust the formatting slightly such that the .NET stacktrace is
separated from the one from Python.

Fixes #1359.
  • Loading branch information
filmor committed Jan 29, 2021
commit 1819ba9836eeb36c88ffafa76e0c710a251d11f2
29 changes: 15 additions & 14 deletions src/runtime/pythonexception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class PythonException : System.Exception, IDisposable
private IntPtr _pyType = IntPtr.Zero;
private IntPtr _pyValue = IntPtr.Zero;
private IntPtr _pyTB = IntPtr.Zero;
private string _tb = "";
private string _message = "";
private string _pythonTypeName = "";
private readonly string _tb = "";
private readonly string _message = "";
private readonly string _pythonTypeName = "";
private bool disposed = false;
private bool _finalized = false;

Expand Down Expand Up @@ -46,14 +46,18 @@ public PythonException()
}
if (_pyTB != IntPtr.Zero)
{
using (PyObject tb_module = PythonEngine.ImportModule("traceback"))
{
Runtime.XIncref(_pyTB);
using (var pyTB = new PyObject(_pyTB))
{
_tb = tb_module.InvokeMethod("format_tb", pyTB).ToString();
}
using PyObject tb_module = PythonEngine.ImportModule("traceback");

Runtime.XIncref(_pyTB);
using var pyTB = new PyObject(_pyTB);

using var tbList = tb_module.InvokeMethod("format_tb", pyTB);

var sb = new StringBuilder();
foreach (var line in tbList) {
sb.Append(line.ToString());
}
_tb = sb.ToString();
Comment thread
filmor marked this conversation as resolved.
}
PythonEngine.ReleaseLock(gs);
}
Expand Down Expand Up @@ -136,10 +140,7 @@ public override string Message
/// <remarks>
/// A string representing the python exception stack trace.
/// </remarks>
public override string StackTrace
{
get { return _tb + base.StackTrace; }
}
public override string StackTrace => $"{_tb}===\n{base.StackTrace}";

/// <summary>
/// Python error type name.
Expand Down