@@ -166,15 +166,7 @@ mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, siz
166
166
167
167
// Populate the exception object
168
168
o_exc -> base .type = type ;
169
-
170
- // Try to allocate memory for the traceback, with fallback to emergency traceback object
171
- o_exc -> traceback = m_new_obj_maybe (mp_obj_traceback_t );
172
- if (o_exc -> traceback == NULL ) {
173
- o_exc -> traceback = & MP_STATE_VM (mp_emergency_traceback_obj );
174
- }
175
-
176
- // Populate the traceback object
177
- * o_exc -> traceback = mp_const_empty_traceback_obj ;
169
+ o_exc -> traceback = (mp_obj_traceback_t * )& mp_const_empty_traceback_obj ;
178
170
179
171
mp_obj_tuple_t * o_tuple ;
180
172
if (n_args == 0 ) {
@@ -228,7 +220,7 @@ void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
228
220
// store/delete attribute
229
221
if (attr == MP_QSTR___traceback__ ) {
230
222
if (dest [1 ] == mp_const_none ) {
231
- self -> traceback -> data = NULL ;
223
+ self -> traceback = ( mp_obj_traceback_t * ) & mp_const_empty_traceback_obj ;
232
224
} else {
233
225
if (!mp_obj_is_type (dest [1 ], & mp_type_traceback )) {
234
226
mp_raise_TypeError (MP_ERROR_TEXT ("invalid traceback" ));
@@ -244,7 +236,7 @@ void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
244
236
} else if (attr == MP_QSTR_value && self -> base .type == & mp_type_StopIteration ) {
245
237
dest [0 ] = mp_obj_exception_get_value (self_in );
246
238
} else if (attr == MP_QSTR___traceback__ ) {
247
- dest [0 ] = (self -> traceback -> data ) ? MP_OBJ_FROM_PTR (self -> traceback ) : mp_const_none ;
239
+ dest [0 ] = (self -> traceback ) ? MP_OBJ_FROM_PTR (self -> traceback ) : mp_const_none ;
248
240
#if MICROPY_CPYTHON_COMPAT
249
241
} else if (mp_obj_is_subclass_fast (MP_OBJ_FROM_PTR (self -> base .type ), MP_OBJ_FROM_PTR (& mp_type_OSError ))) {
250
242
if (attr == MP_QSTR_errno ) {
@@ -552,14 +544,23 @@ bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type) {
552
544
553
545
void mp_obj_exception_clear_traceback (mp_obj_t self_in ) {
554
546
mp_obj_exception_t * self = get_native_exception (self_in );
555
- // just set the traceback to the null object
547
+ // just set the traceback to the empty traceback object
556
548
// we don't want to call any memory management functions here
557
- self -> traceback -> data = NULL ;
549
+ self -> traceback = ( mp_obj_traceback_t * ) & mp_const_empty_traceback_obj ;
558
550
}
559
551
560
552
void mp_obj_exception_add_traceback (mp_obj_t self_in , qstr file , size_t line , qstr block ) {
561
553
mp_obj_exception_t * self = get_native_exception (self_in );
562
554
555
+ // Try to allocate memory for the traceback, with fallback to emergency traceback object
556
+
557
+ if (self -> traceback == NULL || self -> traceback == (mp_obj_traceback_t * )& mp_const_empty_traceback_obj ) {
558
+ self -> traceback = m_new_obj_maybe (mp_obj_traceback_t );
559
+ if (self -> traceback == NULL ) {
560
+ self -> traceback = & MP_STATE_VM (mp_emergency_traceback_obj );
561
+ }
562
+ }
563
+
563
564
// append this traceback info to traceback data
564
565
// if memory allocation fails (eg because gc is locked), just return
565
566
@@ -613,7 +614,7 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qs
613
614
void mp_obj_exception_get_traceback (mp_obj_t self_in , size_t * n , size_t * * values ) {
614
615
mp_obj_exception_t * self = get_native_exception (self_in );
615
616
616
- if (self -> traceback -> data == NULL ) {
617
+ if (self -> traceback == NULL ) {
617
618
* n = 0 ;
618
619
* values = NULL ;
619
620
} else {
0 commit comments