8000 py/modsys.c: Add sys._exc_traceback. by DavidEGrayson · Pull Request #11244 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py/modsys.c: Add sys._exc_traceback. #11244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions docs/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ Functions
present in pre-built firmware (due to it affecting performance). The relevant
configuration option is *MICROPY_PY_SYS_SETTRACE*.

.. function:: _exc_traceback(exc)

Retrieves traceback information from an exception object, including
the filename, line number, and code block name for every code location
on the call stack when the exception was thrown.

.. admonition:: Difference to CPython
:class: attention

This function is a MicroPython extension intended to provide similar
functionality to the ``__traceback__`` attribute of exception
objects in CPython.

.. admonition:: Unstable
:class: attention

This function directly exposes the internal traceback data used by
MicroPython. Future versions might introduce incompatible changes to
the format.



Constants
---------

Expand Down
23 changes: 23 additions & 0 deletions py/modsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);

#if MICROPY_PY_SYS_EXC_TRACEBACK
STATIC mp_obj_t mp_sys_exc_traceback(mp_obj_t exc) {
if (!mp_obj_is_exception_instance(exc)) {
mp_raise_TypeError(MP_ERROR_TEXT("not an exception"));
}
size_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values);
// Assumption: n is a multiple of 3.
mp_obj_t obj = mp_obj_new_list(n, NULL);
mp_obj_list_t *list = MP_OBJ_TO_PTR(obj);
for (size_t i = 0; i < list->len; i += 3) {
list->items[i + 0] = MP_OBJ_NEW_QSTR(values[i + 0]); // filename
list->items[i + 1] = MP_OBJ_NEW_SMALL_INT(values[i + 1]); // line
list->items[i + 2] = MP_OBJ_NEW_QSTR(values[i + 2]); // block
}
return obj;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_exc_traceback_obj, mp_sys_exc_traceback);
#endif

#if MICROPY_PY_SYS_EXC_INFO
STATIC mp_obj_t mp_sys_exc_info(void) {
mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
Expand Down Expand Up @@ -277,6 +297,9 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
*/

{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
#if MICROPY_PY_SYS_EXC_TRACEBACK
{ MP_ROM_QSTR(MP_QSTR__exc_traceback), MP_ROM_PTR(&mp_sys_exc_traceback_obj) },
#endif
#if MICROPY_PY_SYS_ATEXIT
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
#endif
Expand Down
5 changes: 5 additions & 0 deletions py/mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,11 @@ typedef double mp_float_t;
#define MICROPY_PY_SYS_MODULES (1)
#endif

// Whether to provide "sys._exc_traceback" function.
#ifndef MICROPY_PY_SYS_EXC_TRACEBACK
#define MICROPY_PY_SYS_EXC_TRACEBACK (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
#endif

// Whether to provide "sys.exc_info" function
// Avoid enabling this, this function is Python2 heritage
#ifndef MICROPY_PY_SYS_EXC_INFO
Expand Down
0