Skip to content

Commit ce61a6f

Browse files
committed
Reverse Python traceback to match C# stacktrace order
1 parent 5ae34ba commit ce61a6f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/runtime/pythonexception.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Runtime.CompilerServices;
34
using System.Text;
45

@@ -44,6 +45,7 @@ public PythonException()
4445
}
4546
_message = type + " : " + message;
4647
}
48+
4749
if (_pyTB != IntPtr.Zero)
4850
{
4951
using PyObject tb_module = PythonEngine.ImportModule("traceback");
@@ -54,7 +56,9 @@ public PythonException()
5456
using var tbList = tb_module.InvokeMethod("format_tb", pyTB);
5557

5658
var sb = new StringBuilder();
57-
foreach (var line in tbList) {
59+
// Reverse Python's traceback list to match the order used in C#
60+
// stacktraces
61+
foreach (var line in tbList.Reverse()) {
5862
sb.Append(line.ToString());
5963
}
6064
_tb = sb.ToString();

0 commit comments

Comments
 (0)