Skip to content

Commit 90fb4dd

Browse files
committed
Print most recent call last as CPython does.
1 parent a893877 commit 90fb4dd

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/backtrace.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@ each_backtrace(mrb_state *mrb, mrb_int ciidx, mrb_code *pc0, each_backtrace_func
7070
}
7171
}
7272

73-
/* mrb_print_backtrace
74-
75-
function to retrieve backtrace information from the exception.
76-
note that if you call method after the exception, call stack will be
77-
overwritten. So invoke these functions just after detecting exceptions.
78-
*/
79-
8073
#ifndef MRB_DISABLE_STDIO
8174

8275
static void
@@ -89,8 +82,8 @@ print_backtrace(mrb_state *mrb, mrb_value backtrace)
8982
fprintf(stream, "trace:\n");
9083

9184
n = RARRAY_LEN(backtrace);
92-
for (i = 0; i < n; i++) {
93-
mrb_value entry = RARRAY_PTR(backtrace)[i];
85+
for (i=0; n--; i++) {
86+
mrb_value entry = RARRAY_PTR(backtrace)[n];
9487

9588
if (mrb_string_p(entry)) {
9689
fprintf(stream, "\t[%d] %.*s\n", i, (int)RSTRING_LEN(entry), RSTRING_PTR(entry));
@@ -103,7 +96,7 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
10396
{
10497
FILE *stream = stderr;
10598
struct backtrace_location *bt;
106-
mrb_int n, i;
99+
int n, i;
107100

108101
bt = (struct backtrace_location*)mrb_data_check_get_ptr(mrb, packed, &bt_type);
109102
if (bt == NULL) {
@@ -112,9 +105,9 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
112105
n = (mrb_int)RDATA(packed)->flags;
113106

114107
fprintf(stream, "trace:\n");
115-
for (i = 0; i < n; i++) {
108+
for (i = 0; n--; i++) {
116109
int ai = mrb_gc_arena_save(mrb);
117-
struct backtrace_location *entry = &bt[i];
110+
struct backtrace_location *entry = &bt[n];
118111
if (entry->filename == NULL) continue;
119112
fprintf(stream, "\t[%d] %s:%d", (int)i, entry->filename, entry->lineno);
120113
if (entry->method_id != 0) {
@@ -128,6 +121,11 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
128121
}
129122
}
130123

124+
/* mrb_print_backtrace
125+
126+
function to retrieve backtrace information from the last exception.
127+
*/
128+
131129
MRB_API void
132130
mrb_print_backtrace(mrb_state *mrb)
133131
{

0 commit comments

Comments
 (0)