Skip to content

Commit c1ffab7

Browse files
committed
Reduce code duplication in traceback module
1 parent bfea694 commit c1ffab7

File tree

3 files changed

+34
-63
lines changed

3 files changed

+34
-63
lines changed

py/obj.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
143143
}
144144

145145
// helper function to print an exception with traceback
146-
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
146+
void mp_obj_print_exception_with_limit(const mp_print_t *print, mp_obj_t exc, mp_int_t limit) {
147147
if (mp_obj_is_exception_instance(exc) && stack_ok()) {
148148
size_t n, *values;
149149
mp_obj_exception_get_traceback(exc, &n, &values);
@@ -156,16 +156,38 @@ void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
156156
#endif
157157
const compressed_string_t *block_fmt = MP_ERROR_TEXT(", in %q\n");
158158

159+
// Set traceback formatting
160+
// Default: Print full traceback
161+
limit = limit * 3;
162+
mp_int_t i = n - 3, j;
163+
if (limit > 0) {
164+
// Print upto limit traceback
165+
// entries from caller's frame
166+
if ((unsigned)limit > n) {
167+
limit = n;
168+
}
169+
limit = n - limit;
170+
} else if (limit < 0) {
171+
// Print upto limit traceback
172+
// entries from last
173+
if ((unsigned)-limit > n) {
174+
limit = -n;
175+
}
176+
i = 0, limit = limit + 3;
177+
}
178+
159179
// Print the traceback
160180
mp_cprintf(print, MP_ERROR_TEXT("Traceback (most recent call last):\n"));
161-
for (int i = n - 3; i >= 0; i -= 3) {
181+
182+
for (; i >= limit; i -= 3) {
183+
j = (i < 0) ? -i : i;
162184
#if MICROPY_ENABLE_SOURCE_LINE
163-
mp_cprintf(print, frame, values[i], (int)values[i + 1]);
185+
mp_cprintf(print, frame, values[j], (int)values[j + 1]);
164186
#else
165-
mp_printf(print, frame, values[i]);
187+
mp_cprintf(print, frame, values[j]);
166188
#endif
167-
// the block name can be NULL if it's unknown
168-
qstr block = values[i + 2];
189+
// The block name can be NULL if it's unknown
190+
qstr block = values[j + 2];
169191
if (block == MP_QSTRnull) {
170192
mp_print_str(print, "\n");
171193
} else {
@@ -178,6 +200,10 @@ void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
178200
mp_print_str(print, "\n");
179201
}
180202

203+
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
204+
mp_obj_print_exception_with_limit(print, exc, 0);
205+
}
206+
181207
bool PLACE_IN_ITCM(mp_obj_is_true)(mp_obj_t arg) {
182208
if (arg == mp_const_false) {
183209
return 0;

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type
888888
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
889889
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
890890
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
891+
void mp_obj_print_exception_with_limit(const mp_print_t *print, mp_obj_t exc, mp_int_t limit);
891892

892893
bool mp_obj_is_true(mp_obj_t arg);
893894
bool mp_obj_is_callable(mp_obj_t o_in);

shared-module/traceback/__init__.c

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,5 @@
2727
#include "shared-module/traceback/__init__.h"
2828

2929
void shared_module_traceback_print_exception(mp_obj_exception_t *exc, mp_print_t *print, mp_int_t limit) {
30-
// Print traceback
31-
if (exc->traceback != NULL) {
32-
size_t n = exc->traceback->len;
33-
size_t *values = exc->traceback->data;
34-
if (n > 0) {
35-
assert(n % 3 == 0);
36-
// Decompress the format strings
37-
const compressed_string_t *traceback = MP_ERROR_TEXT("Traceback (most recent call last):\n");
38-
#if MICROPY_ENABLE_SOURCE_LINE
39-
const compressed_string_t *frame = MP_ERROR_TEXT(" File \"%q\", line %d");
40-
#else
41-
const compressed_string_t *frame = MP_ERROR_TEXT(" File \"%q\"");
42-
#endif
43-
const compressed_string_t *block_fmt = MP_ERROR_TEXT(", in %q\n");
44-
45-
// Set traceback formatting
46-
// Default: Print full traceback
47-
limit = limit * 3;
48-
mp_int_t i = n - 3, j;
49-
if (limit > 0) {
50-
// Print upto limit traceback
51-
// entries from caller's frame
52-
if ((unsigned)limit > n) {
53-
limit = n;
54-
}
55-
limit = n - limit;
56-
} else if (limit < 0) {
57-
// Print upto limit traceback
58-
// entries from last
59-
if ((unsigned)-limit > n) {
60-
limit = -n;
61-
}
62-
i = 0, limit = limit + 3;
63-
}
64-
65-
// Print the traceback
66-
mp_cprintf(print, traceback);
67-
for (; i >= limit; i -= 3) {
68-
j = (i < 0) ? -i : i;
69-
#if MICROPY_ENABLE_SOURCE_LINE
70-
mp_cprintf(print, frame, values[j], (int)values[j + 1]);
71-
#else
72-
mp_printf(print, frame, values[j]);
73-
#endif
74-
// The block name can be NULL if it's unknown
75-
qstr block = values[j + 2];
76-
if (block == MP_QSTRnull) {
77-
mp_print_str(print, "\n");
78-
} else {
79-
mp_printf(print, block_fmt, block);
80-
}
81-
}
82-
}
83-
}
84-
// Print exception
85-
mp_obj_print_helper(print, exc, PRINT_EXC);
86-
mp_print_str(print, "\n");
30+
mp_obj_print_exception_with_limit(print, exc, limit);
8731
}

0 commit comments

Comments
 (0)