2727#include "py/stream.h"
2828#include "py/runtime.h"
2929
30+ #include "shared-module/traceback/__init__.h"
31+
3032//| """Traceback Module
3133//|
3234//| This module provides a standard interface to print stack traces of programs.
5052//| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed.
5153//| :param io.FileIO file: If file is omitted or `None`, the output goes to `sys.stderr`; otherwise it should be an open
5254//| file or file-like object to receive the output.
53- //| :param bool chain: If `True` then chained exceptions will be printed.
55+ //| :param bool chain: If `True` then chained exceptions will be printed (note: not yet implemented) .
5456//|
5557//| """
5658//| ...
@@ -69,10 +71,10 @@ STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_arg
6971 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
7072 mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
7173
72- mp_obj_t exc = args [ARG_value ].u_obj ;
73- if (!mp_obj_is_exception_instance (exc )) {
74+ if (!mp_obj_is_exception_instance (args [ARG_value ].u_obj )) {
7475 mp_raise_TypeError (translate ("invalid exception" ));
7576 }
77+ mp_obj_exception_t exc = * (mp_obj_exception_t * )MP_OBJ_TO_PTR (args [ARG_value ].u_obj );
7678
7779 mp_print_t print = mp_plat_print ;
7880 if (args [ARG_file ].u_obj != mp_const_none ) {
@@ -91,67 +93,19 @@ STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_arg
9193 if (!mp_obj_get_int_maybe (args [ARG_limit ].u_obj , & limit )) {
9294 mp_raise_TypeError (translate ("limit should be an int" ));
9395 }
94- print_tb = ! (limit = = 0 );
96+ print_tb = (limit ! = 0 );
9597 }
9698
9799 if (args [ARG_tb ].u_obj != mp_const_none && print_tb ) {
98100 if (!mp_obj_is_type (args [ARG_tb ].u_obj , & mp_type_traceback )) {
99101 mp_raise_TypeError (translate ("invalid traceback" ));
100102 }
101- mp_obj_traceback_t * tb = MP_OBJ_TO_PTR (args [ARG_tb ].u_obj );
102- size_t n = (tb -> data ) ? tb -> len : 0 ;
103- size_t * values = (tb -> data ) ? tb -> data : NULL ;
104- if (n > 0 ) {
105- assert (n % 3 == 0 );
106- // Decompress the format strings
107- const compressed_string_t * traceback = MP_ERROR_TEXT ("Traceback (most recent call last):\n" );
108- char decompressed [decompress_length (traceback )];
109- decompress (traceback , decompressed );
110- #if MICROPY_ENABLE_SOURCE_LINE
111- const compressed_string_t * frame = MP_ERROR_TEXT (" File \"%q\", line %d" );
112- #else
113- const compressed_string_t * frame = MP_ERROR_TEXT (" File \"%q\"" );
114- #endif
115- char decompressed_frame [decompress_length (frame )];
116- decompress (frame , decompressed_frame );
117- const compressed_string_t * block_fmt = MP_ERROR_TEXT (", in %q\n" );
118- char decompressed_block [decompress_length (block_fmt )];
119- decompress (block_fmt , decompressed_block );
120-
121- // Set traceback formatting
122- // Default: Print full traceback
123- int i = n - 3 , j ;
124- if (limit > 0 ) {
125- // Print upto limit traceback
126- // from caller's frame
127- limit = n - (limit * 3 );
128- } else if (limit < 0 ) {
129- // Print upto limit traceback
130- // from last
131- i = 0 , limit = 3 + (limit * 3 );
132- }
133-
134- // Print the traceback
135- mp_print_str (& print , decompressed );
136- for (; i >= limit ; i -= 3 ) {
137- j = (i < 0 ) ? - i : i ;
138- #if MICROPY_ENABLE_SOURCE_LINE
139- mp_printf (& print , decompressed_frame , values [j ], (int )values [j + 1 ]);
140- #else
141- mp_printf (& print , decompressed_frame , values [j ]);
142- #endif
143- // The block name can be NULL if it's unknown
144- qstr block = values [j + 2 ];
145- if (block == MP_QSTRnull ) {
146- mp_print_str (& print , "\n" );
147- } else {
148- mp_printf (& print , decompressed_block , block );
149- }
150- }
151- }
103+ exc .traceback = MP_OBJ_TO_PTR (args [ARG_tb ].u_obj );
104+ } else {
105+ exc .traceback = NULL ;
152106 }
153- mp_obj_print_helper ( & print , exc , PRINT_EXC );
154- mp_print_str ( & print , "\n" );
107+
108+ shared_module_traceback_print_exception ( & exc , & print , limit );
155109 return mp_const_none ;
156110}
157111STATIC MP_DEFINE_CONST_FUN_OBJ_KW (traceback_print_exception_obj , 3 , traceback_print_exception );
0 commit comments