Skip to content

Commit 73ccede

Browse files
committed
py/obj: allow disabling traceback allocation
Since the existing code handles `NULL` `traceback_data` correctly, it would avoid heap allocation in builds that don't access traceback data.
1 parent c718974 commit 73ccede

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,11 @@ typedef double mp_float_t;
14031403
#define MICROPY_PY_SYS_TRACEBACKLIMIT (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING)
14041404
#endif
14051405

1406+
// Whether to disable traceback allocation
1407+
#ifndef MICROPY_PY_SYS_TRACEBACK_DISABLE
1408+
#define MICROPY_PY_SYS_TRACEBACK_DISABLE 0
1409+
#endif
1410+
14061411
// Whether the sys module supports attribute delegation
14071412
// This is enabled automatically when needed by other features
14081413
#ifndef MICROPY_PY_SYS_ATTR_DELEGATION

py/obj.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,11 @@ bool mp_obj_is_exception_type(mp_obj_t self_in);
856856
bool mp_obj_is_exception_instance(mp_obj_t self_in);
857857
bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
858858
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
859+
#if MICROPY_PY_SYS_TRACEBACK_DISABLE
860+
static inline void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {}
861+
#else
859862
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
863+
#endif
860864
void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
861865
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
862866
mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);

py/objexcept.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
569569
self->traceback_data = NULL;
570570
}
571571

572+
#if !MICROPY_PY_SYS_TRACEBACK_DISABLE
572573
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {
573574
mp_obj_exception_t *self = get_native_exception(self_in);
574575

@@ -631,6 +632,7 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qs
631632
tb_data[1] = line;
632633
tb_data[2] = block;
633634
}
635+
#endif // !MICROPY_PY_SYS_TRACEBACK_DISABLE
634636

635637
void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values) {
636638
mp_obj_exception_t *self = get_native_exception(self_in);

0 commit comments

Comments
 (0)