From b427d6ae8686f4a9520509cb73ab9f1327ceadc1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 26 Aug 2014 23:35:57 +0100 Subject: [PATCH] py: Fix line number printing for file with 1 line. With a file with 1 line (and an error on that line), used to show the line as number 0. Now shows it correctly as line number 1. But, when line numbers are disabled, it now prints line number 1 for any line that has an error (instead of 0 as previously). This might end up being confusing, but requires extra RAM and/or hack logic to make it print something special in the case of no line numbers. --- py/emitglue.c | 2 +- py/vm.c | 46 +++++++++++++++++++++------------------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/py/emitglue.c b/py/emitglue.c index 5916586aea..99cd3f3832 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -107,7 +107,7 @@ void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void #ifdef WRITE_CODE FILE *fp_write_code = fopen("out-code", "wb"); - fwrite(fun_data, len, 1, fp_write_code); + fwrite(fun_data, fun_len, 1, fp_write_code); fclose(fp_write_code); #endif #endif diff --git a/py/vm.c b/py/vm.c index c0116bbeb9..8c59b9c3aa 100644 --- a/py/vm.c +++ b/py/vm.c @@ -925,33 +925,29 @@ exception_handler: mp_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24); qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24); qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24); - mp_uint_t source_line = 0; mp_uint_t bc = code_state->ip - code_info - code_info_size; //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]); - const byte* ci = code_info + 12; - if (*ci) { - source_line = 1; - mp_uint_t c; - while ((c = *ci)) { - mp_uint_t b, l; - if ((c & 0x80) == 0) { - // 0b0LLBBBBB encoding - b = c & 0x1f; - l = c >> 5; - ci += 1; - } else { - // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte) - b = c & 0xf; - l = ((c << 4) & 0x700) | ci[1]; - ci += 2; - } - if (bc >= b) { - bc -= b; - source_line += l; - } else { - // found source line corresponding to bytecode offset - break; - } + mp_uint_t source_line = 1; + mp_uint_t c; + for (const byte *ci = code_info + 12; (c = *ci);) { + mp_uint_t b, l; + if ((c & 0x80) == 0) { + // 0b0LLBBBBB encoding + b = c & 0x1f; + l = c >> 5; + ci += 1; + } else { + // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte) + b = c & 0xf; + l = ((c << 4) & 0x700) | ci[1]; + ci += 2; + } + if (bc >= b) { + bc -= b; + source_line += l; + } else { + // found source line corresponding to bytecode offset + break; } } mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);