py: Change exception traceback data to use size_t instead of mp_uint_t.

The traceback array stores qstrs and line numbers.  qstrs are typed as
size_t, and line numbers should safely fit in size_t as well.
This commit is contained in:
Damien George 2016-01-02 22:04:12 +00:00
parent ae4865efa1
commit 3d2daa2d03
5 changed files with 17 additions and 17 deletions

View File

@ -86,7 +86,7 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
// helper function to print an exception with traceback // helper function to print an exception with traceback
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) { void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
if (mp_obj_is_exception_instance(exc)) { if (mp_obj_is_exception_instance(exc)) {
mp_uint_t n, *values; size_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values); mp_obj_exception_get_traceback(exc, &n, &values);
if (n > 0) { if (n > 0) {
assert(n % 3 == 0); assert(n % 3 == 0);

View File

@ -680,8 +680,8 @@ bool mp_obj_is_exception_type(mp_obj_t self_in);
bool mp_obj_is_exception_instance(mp_obj_t self_in); bool mp_obj_is_exception_instance(mp_obj_t self_in);
bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type); bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
void mp_obj_exception_clear_traceback(mp_obj_t self_in); void mp_obj_exception_clear_traceback(mp_obj_t self_in);
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block); void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values); void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in); mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in); mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);

View File

@ -346,9 +346,9 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
offset += sizeof(void *) - 1; offset += sizeof(void *) - 1;
offset &= ~(sizeof(void *) - 1); offset &= ~(sizeof(void *) - 1);
if ((mp_emergency_exception_buf_size - offset) > (sizeof(mp_uint_t) * 3)) { if ((mp_emergency_exception_buf_size - offset) > (sizeof(o->traceback_data[0]) * 3)) {
// We have room to store some traceback. // We have room to store some traceback.
o->traceback_data = (mp_uint_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset); o->traceback_data = (size_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
o->traceback_alloc = (MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]); o->traceback_alloc = (MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
o->traceback_len = 0; o->traceback_len = 0;
} }
@ -429,14 +429,14 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
self->traceback_data = NULL; self->traceback_data = NULL;
} }
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block) { void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {
GET_NATIVE_EXCEPTION(self, self_in); GET_NATIVE_EXCEPTION(self, self_in);
// append this traceback info to traceback data // append this traceback info to traceback data
// if memory allocation fails (eg because gc is locked), just return // if memory allocation fails (eg because gc is locked), just return
if (self->traceback_data == NULL) { if (self->traceback_data == NULL) {
self->traceback_data = m_new_maybe(mp_uint_t, 3); self->traceback_data = m_new_maybe(size_t, 3);
if (self->traceback_data == NULL) { if (self->traceback_data == NULL) {
return; return;
} }
@ -444,7 +444,7 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line,
self->traceback_len = 0; self->traceback_len = 0;
} else if (self->traceback_len + 3 > self->traceback_alloc) { } else if (self->traceback_len + 3 > self->traceback_alloc) {
// be conservative with growing traceback data // be conservative with growing traceback data
mp_uint_t *tb_data = m_renew_maybe(mp_uint_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3, true); size_t *tb_data = m_renew_maybe(size_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3, true);
if (tb_data == NULL) { if (tb_data == NULL) {
return; return;
} }
@ -452,14 +452,14 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line,
self->traceback_alloc += 3; self->traceback_alloc += 3;
} }
mp_uint_t *tb_data = &self->traceback_data[self->traceback_len]; size_t *tb_data = &self->traceback_data[self->traceback_len];
self->traceback_len += 3; self->traceback_len += 3;
tb_data[0] = (mp_uint_t)file; tb_data[0] = file;
tb_data[1] = (mp_uint_t)line; tb_data[1] = line;
tb_data[2] = (mp_uint_t)block; tb_data[2] = block;
} }
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values) { void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values) {
GET_NATIVE_EXCEPTION(self, self_in); GET_NATIVE_EXCEPTION(self, self_in);
if (self->traceback_data == NULL) { if (self->traceback_data == NULL) {

View File

@ -33,7 +33,7 @@ typedef struct _mp_obj_exception_t {
mp_obj_base_t base; mp_obj_base_t base;
mp_uint_t traceback_alloc : (BITS_PER_WORD / 2); mp_uint_t traceback_alloc : (BITS_PER_WORD / 2);
mp_uint_t traceback_len : (BITS_PER_WORD / 2); mp_uint_t traceback_len : (BITS_PER_WORD / 2);
mp_uint_t *traceback_data; size_t *traceback_data;
mp_obj_tuple_t *args; mp_obj_tuple_t *args;
} mp_obj_exception_t; } mp_obj_exception_t;

View File

@ -1311,9 +1311,9 @@ unwind_loop:
qstr block_name = mp_decode_uint(&ip); qstr block_name = mp_decode_uint(&ip);
qstr source_file = mp_decode_uint(&ip); qstr source_file = mp_decode_uint(&ip);
#endif #endif
mp_uint_t bc = code_state->ip - code_state->code_info - code_info_size; size_t bc = code_state->ip - code_state->code_info - code_info_size;
mp_uint_t source_line = 1; size_t source_line = 1;
mp_uint_t c; size_t c;
while ((c = *ip)) { while ((c = *ip)) {
mp_uint_t b, l; mp_uint_t b, l;
if ((c & 0x80) == 0) { if ((c & 0x80) == 0) {