summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-06-06 17:28:32 +0100
committerDamien George <damien.p.george@gmail.com>2016-06-06 17:28:32 +0100
commitb1533c43669c037d04c87d1756355cdc8edd5e0a (patch)
tree10dad92829abdc70036ca2398ac2dce7ce00ff60
parent2bf6eb9fe2e7e2acd1ce361dd8276cb8f047f8fe (diff)
downloadmicropython-b1533c43669c037d04c87d1756355cdc8edd5e0a.tar.gz
micropython-b1533c43669c037d04c87d1756355cdc8edd5e0a.zip
py/parse: Treat constants that start with underscore as private.
Assignments of the form "_id = const(value)" are treated as private (following a similar CPython convention) and code is no longer emitted for the assignment to a global variable. See issue #2111.
-rw-r--r--py/parse.c11
-rw-r--r--tests/micropython/const.py12
-rw-r--r--tests/micropython/const.py.exp3
3 files changed, 26 insertions, 0 deletions
diff --git a/py/parse.c b/py/parse.c
index 7da484c497..1ec995cd8f 100644
--- a/py/parse.c
+++ b/py/parse.c
@@ -461,6 +461,8 @@ STATIC const mp_rom_map_elem_t mp_constants_table[] = {
STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
#endif
+STATIC void push_result_rule(parser_t *parser, size_t src_line, const rule_t *rule, size_t num_args);
+
#if MICROPY_COMP_CONST_FOLDING
STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args) {
// this code does folding of arbitrary integer expressions, eg 1 + 2 * 3 + 4
@@ -587,6 +589,15 @@ STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args
assert(elem->value == MP_OBJ_NULL);
elem->value = MP_OBJ_NEW_SMALL_INT(value);
+ // If the constant starts with an underscore then treat it as a private
+ // variable and don't emit any code to store the value to the id.
+ if (qstr_str(id)[0] == '_') {
+ pop_result(parser); // pop const(value)
+ pop_result(parser); // pop id
+ push_result_rule(parser, 0, rules[RULE_pass_stmt], 0); // replace with "pass"
+ return true;
+ }
+
// replace const(value) with value
pop_result(parser);
push_result_node(parser, pn_value);
diff --git a/tests/micropython/const.py b/tests/micropython/const.py
index 457365c50a..09717fd147 100644
--- a/tests/micropython/const.py
+++ b/tests/micropython/const.py
@@ -9,3 +9,15 @@ def f():
print(X, Y + 1)
f()
+
+_X = const(12)
+_Y = const(_X + 34)
+
+print(_X, _Y)
+
+class A:
+ Z = const(1)
+ _Z = const(2)
+ print(Z, _Z)
+
+print(hasattr(A, 'Z'), hasattr(A, '_Z'))
diff --git a/tests/micropython/const.py.exp b/tests/micropython/const.py.exp
index c447aaf8c1..ece6a5cb2e 100644
--- a/tests/micropython/const.py.exp
+++ b/tests/micropython/const.py.exp
@@ -1,2 +1,5 @@
123 580
123 580
+12 46
+1 2
+True False