diff options
author | Damien George <damien.p.george@gmail.com> | 2015-12-08 21:05:14 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-12-08 21:05:14 +0000 |
commit | 33ac0fd09f15377f571c3d2455802cf7a8ac09a8 (patch) | |
tree | 48ed1a0c11a2fdc8c2256b5f42a6b38eede9301b | |
parent | bbe2e22fcb3bf83b03ad25856e29995987e1fc40 (diff) | |
download | micropython-33ac0fd09f15377f571c3d2455802cf7a8ac09a8.tar.gz micropython-33ac0fd09f15377f571c3d2455802cf7a8ac09a8.zip |
py: Don't try to optimise for+range when args are not simple expressions.
Addresses issue #1693.
-rw-r--r-- | py/compile.c | 13 | ||||
-rw-r--r-- | tests/basics/for_range.py | 58 |
2 files changed, 71 insertions, 0 deletions
diff --git a/py/compile.c b/py/compile.c index 5c2fce6866..841b8f90c0 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1421,6 +1421,19 @@ STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { optimize = false; } } + // arguments must be able to be compiled as standard expressions + if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) { + int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start); + if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) { + optimize = false; + } + } + if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) { + int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end); + if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) { + optimize = false; + } + } } if (optimize) { compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]); diff --git a/tests/basics/for_range.py b/tests/basics/for_range.py new file mode 100644 index 0000000000..ddff5ebd44 --- /dev/null +++ b/tests/basics/for_range.py @@ -0,0 +1,58 @@ +# test for+range, mostly to check optimisation of this pair + +# apply args using * +for x in range(*(1, 3)): + print(x) +for x in range(1, *(6, 2)): + print(x) + +# apply args using ** +try: + for x in range(**{'end':1}): + print(x) +except TypeError: + print('TypeError') +try: + for x in range(0, **{'end':1}): + print(x) +except TypeError: + print('TypeError') +try: + for x in range(0, 1, **{'step':1}): + print(x) +except TypeError: + print('TypeError') + +# keyword args +try: + for x in range(end=1): + print(x) +except TypeError: + print('TypeError') +try: + for x in range(0, end=1): + print(x) +except TypeError: + print('TypeError') +try: + for x in range(0, 1, step=1): + print(x) +except TypeError: + print('TypeError') + +# argument is a comprehension +try: + for x in range(0 for i in []): + print(x) +except TypeError: + print('TypeError') +try: + for x in range(0, (0 for i in [])): + print(x) +except TypeError: + print('TypeError') +try: + for x in range(0, 1, (0 for i in [])): + print(x) +except TypeError: + print('TypeError') |