27
27
#include "py/stream.h"
28
28
#include "py/runtime.h"
29
29
30
+ #include "shared-module/traceback/__init__.h"
31
+
30
32
//| """Traceback Module
31
33
//|
32
34
//| This module provides a standard interface to print stack traces of programs.
50
52
//| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed.
51
53
//| :param io.FileIO file: If file is omitted or `None`, the output goes to `sys.stderr`; otherwise it should be an open
52
54
//| 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) .
54
56
//|
55
57
//| """
56
58
//| ...
@@ -69,10 +71,10 @@ STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_arg
69
71
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
70
72
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
71
73
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 )) {
74
75
mp_raise_TypeError (translate ("invalid exception" ));
75
76
}
77
+ mp_obj_exception_t exc = * (mp_obj_exception_t * )MP_OBJ_TO_PTR (args [ARG_value ].u_obj );
76
78
77
79
mp_print_t print = mp_plat_print ;
78
80
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
91
93
if (!mp_obj_get_int_maybe (args [ARG_limit ].u_obj , & limit )) {
92
94
mp_raise_TypeError (translate ("limit should be an int" ));
93
95
}
94
- print_tb = ! (limit = = 0 );
96
+ print_tb = (limit ! = 0 );
95
97
}
96
98
97
99
if (args [ARG_tb ].u_obj != mp_const_none && print_tb ) {
98
100
if (!mp_obj_is_type (args [ARG_tb ].u_obj , & mp_type_traceback )) {
99
101
mp_raise_TypeError (translate ("invalid traceback" ));
100
102
}
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 ;
152
106
}
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 );
155
109
return mp_const_none ;
156
110
}
157
111
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (traceback_print_exception_obj , 3 , traceback_print_exception );
0 commit comments