Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove non-standard sys.print_exception() #10332

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

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
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
30 changes: 4 additions & 26 deletions py/modsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include "py/builtin.h"
#include "py/objexcept.h"
#include "py/objlist.h"
#include "py/objmodule.h"
#include "py/objtuple.h"
Expand Down Expand Up @@ -170,29 +171,7 @@ static mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);

static mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
// CIRCUITPY-CHANGE
#if CIRCUITPY_WARNINGS
warnings_warn(&mp_type_FutureWarning, MP_ERROR_TEXT("%q moved from %q to %q"), MP_QSTR_print_exception, MP_QSTR_sys, MP_QSTR_traceback);
#endif

#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
void *stream_obj = &mp_sys_stdout_obj;
if (n_args > 1) {
mp_get_stream_raise(args[1], MP_STREAM_OP_WRITE);
stream_obj = MP_OBJ_TO_PTR(args[1]);
}

mp_print_t print = {stream_obj, mp_stream_write_adaptor};
mp_obj_print_exception(&print, args[0]);
#else
(void)n_args;
mp_obj_print_exception(&mp_plat_print, args[0]);
#endif

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
// CIRCUITPY-CHANGE: Removed print_exception because it isn't in CPython.

#if MICROPY_PY_SYS_EXC_INFO
static mp_obj_t mp_sys_exc_info(void) {
Expand All @@ -209,7 +188,8 @@ static mp_obj_t mp_sys_exc_info(void) {
t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
t->items[1] = cur_exc;
// CIRCUITPY-CHANGE: has traceback obj
t->items[2] = mp_obj_exception_get_traceback_obj(cur_exc);
mp_obj_exception_t *native_exc = mp_obj_exception_get_native(cur_exc);
t->items[2] = native_exc->traceback;
return MP_OBJ_FROM_PTR(t);
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
Expand Down Expand Up @@ -347,8 +327,6 @@ static const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
/*
* Extensions to CPython
*/

{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
#if MICROPY_PY_SYS_ATEXIT
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
#endif
Expand Down
125 changes: 0 additions & 125 deletions py/objexcept.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include <stdio.h>

#include "py/objlist.h"
// CIRCUITPY-CHANGE
#include "py/objnamedtuple.h"
#include "py/objstr.h"
#include "py/objtuple.h"
#include "py/objtype.h"
Expand Down Expand Up @@ -729,126 +727,3 @@ void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values
*values = self->traceback->data;
}
}

// CIRCUITPY-CHANGE: here until end
#if MICROPY_PY_SYS_EXC_INFO
static const mp_obj_namedtuple_type_t code_type_obj = {
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_code),
.n_fields = 15,
.fields = {
MP_QSTR_co_argcount,
MP_QSTR_co_kwonlyargcount,
MP_QSTR_co_nlocals,
MP_QSTR_co_stacksize,
MP_QSTR_co_flags,
MP_QSTR_co_code,
MP_QSTR_co_consts,
MP_QSTR_co_names,
MP_QSTR_co_varnames,
MP_QSTR_co_freevars,
MP_QSTR_co_cellvars,
MP_QSTR_co_filename,
MP_QSTR_co_name,
MP_QSTR_co_firstlineno,
MP_QSTR_co_lnotab,
},
};

static mp_obj_t code_make_new(qstr file, qstr block) {
mp_obj_t elems[15] = {
mp_obj_new_int(0), // co_argcount
mp_obj_new_int(0), // co_kwonlyargcount
mp_obj_new_int(0), // co_nlocals
mp_obj_new_int(0), // co_stacksize
mp_obj_new_int(0), // co_flags
mp_obj_new_bytearray(0, NULL), // co_code
mp_obj_new_tuple(0, NULL), // co_consts
mp_obj_new_tuple(0, NULL), // co_names
mp_obj_new_tuple(0, NULL), // co_varnames
mp_obj_new_tuple(0, NULL), // co_freevars
mp_obj_new_tuple(0, NULL), // co_cellvars
MP_OBJ_NEW_QSTR(file), // co_filename
MP_OBJ_NEW_QSTR(block), // co_name
mp_obj_new_int(1), // co_firstlineno
mp_obj_new_bytearray(0, NULL), // co_lnotab
};

return namedtuple_make_new((const mp_obj_type_t *)&code_type_obj, 15, 0, elems);
}

static const mp_obj_namedtuple_type_t frame_type_obj = {
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_frame),
.n_fields = 8,
.fields = {
MP_QSTR_f_back,
MP_QSTR_f_builtins,
MP_QSTR_f_code,
MP_QSTR_f_globals,
MP_QSTR_f_lasti,
MP_QSTR_f_lineno,
MP_QSTR_f_locals,
MP_QSTR_f_trace,
},
};

static mp_obj_t frame_make_new(mp_obj_t f_code, int f_lineno) {
mp_obj_t elems[8] = {
mp_const_none, // f_back
mp_obj_new_dict(0), // f_builtins
f_code, // f_code
mp_obj_new_dict(0), // f_globals
mp_obj_new_int(0), // f_lasti
mp_obj_new_int(f_lineno), // f_lineno
mp_obj_new_dict(0), // f_locals
mp_const_none, // f_trace
};

return namedtuple_make_new((const mp_obj_type_t *)&frame_type_obj, 8, 0, elems);
}

static const mp_obj_namedtuple_type_t traceback_type_obj = {
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_traceback),
.n_fields = 4,
.fields = {
MP_QSTR_tb_frame,
MP_QSTR_tb_lasti,
MP_QSTR_tb_lineno,
MP_QSTR_tb_next,
},
};

static mp_obj_t traceback_from_values(size_t *values, mp_obj_t tb_next) {
int lineno = values[1];

mp_obj_t elems[4] = {
frame_make_new(code_make_new(values[0], values[2]), lineno),
mp_obj_new_int(0),
mp_obj_new_int(lineno),
tb_next,
};

return namedtuple_make_new((const mp_obj_type_t *)&traceback_type_obj, 4, 0, elems);
};

mp_obj_t mp_obj_exception_get_traceback_obj(mp_obj_t self_in) {
mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in);

if (!mp_obj_is_exception_instance(self)) {
return mp_const_none;
}

size_t n, *values;
mp_obj_exception_get_traceback(self, &n, &values);
if (n == 0) {
return mp_const_none;
}

mp_obj_t tb_next = mp_const_none;

for (size_t i = 0; i < n; i += 3) {
tb_next = traceback_from_values(&values[i], tb_next);
}

return tb_next;
}
#endif
Loading