From 62ad189a65e5e3b4cb6c64c9ea2d493f2c06fffd Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Wed, 29 Jan 2014 21:51:51 +0000
Subject: [PATCH] py: Add compile option to enable/disable source line numbers.

---
 py/emitbc.c         | 2 ++
 py/mpconfig.h       | 6 ++++++
 py/obj.c            | 4 ++++
 unix/mpconfigport.h | 1 +
 4 files changed, 13 insertions(+)

diff --git a/py/emitbc.c b/py/emitbc.c
index 6d9523fe5..d74b06525 100644
--- a/py/emitbc.c
+++ b/py/emitbc.c
@@ -249,6 +249,7 @@ static void emit_bc_set_stack_size(emit_t *emit, int size) {
 
 static void emit_bc_set_source_line(emit_t *emit, int source_line) {
     //printf("source: line %d -> %d  offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->byte_code_offset);
+#if MICROPY_ENABLE_SOURCE_LINE
     if (source_line > emit->last_source_line) {
         uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
         uint lines_to_skip = source_line - emit->last_source_line;
@@ -257,6 +258,7 @@ static void emit_bc_set_source_line(emit_t *emit, int source_line) {
         emit->last_source_line_offset = emit->byte_code_offset;
         emit->last_source_line = source_line;
     }
+#endif
 }
 
 static void emit_bc_load_id(emit_t *emit, qstr qstr) {
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 97e4b1387..38b74b733 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -76,6 +76,12 @@
 typedef long long mp_longint_impl_t;
 #endif
 
+// Whether to include information in the byte code to determine source
+// line number (increases RAM usage, but doesn't slow byte code execution)
+#ifndef MICROPY_ENABLE_SOURCE_LINE
+#define MICROPY_ENABLE_SOURCE_LINE (0)
+#endif
+
 // Whether to support float and complex types
 #ifndef MICROPY_ENABLE_FLOAT
 #define MICROPY_ENABLE_FLOAT (0)
diff --git a/py/obj.c b/py/obj.c
index 76a4d8f9b..ce555efa6 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -57,7 +57,11 @@ void mp_obj_print_exception(mp_obj_t exc) {
         if (n > 0) {
             printf("Traceback (most recent call last):\n");
             for (int i = n - 3; i >= 0; i -= 3) {
+#if MICROPY_ENABLE_SOURCE_LINE
                 printf("  File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
+#else
+                printf("  File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2]));
+#endif
             }
         }
     }
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 10abb8ce7..5eba0df61 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -12,6 +12,7 @@
 #define MICROPY_DEBUG_PRINTERS      (1)
 #define MICROPY_ENABLE_REPL_HELPERS (1)
 #define MICROPY_ENABLE_LEXER_UNIX   (1)
+#define MICROPY_ENABLE_SOURCE_LINE  (1)
 #define MICROPY_ENABLE_FLOAT        (1)
 #define MICROPY_LONGINT_IMPL        (MICROPY_LONGINT_IMPL_LONGLONG)
 
-- 
GitLab