Commit 5f9b1052 authored by Damien George's avatar Damien George

py/runtime: Fix builtin compile() in "single" mode so it prints exprs.

As per CPython behaviour, compile(stmt, "file", "single") should create
code which prints to stdout (via __repl_print__) the results of any
expressions in stmt.
Signed-off-by: default avatarDamien George <damien@micropython.org>
parent 448319a7
...@@ -1473,7 +1473,7 @@ mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_i ...@@ -1473,7 +1473,7 @@ mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_i
if (nlr_push(&nlr) == 0) { if (nlr_push(&nlr) == 0) {
qstr source_name = lex->source_name; qstr source_name = lex->source_name;
mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind); mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind);
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false); mp_obj_t module_fun = mp_compile(&parse_tree, source_name, parse_input_kind == MP_PARSE_SINGLE_INPUT);
mp_obj_t ret; mp_obj_t ret;
if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) { if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) {
......
# test compile builtin # test compile builtin
def have_compile(): try:
try:
compile compile
return True except NameError:
except NameError: print("SKIP")
return False raise SystemExit
def test(): def test():
global x global x
...@@ -26,8 +25,9 @@ def test(): ...@@ -26,8 +25,9 @@ def test():
exec(c, {}, {"x":3}) exec(c, {}, {"x":3})
# single/eval mode # single/eval mode
exec(compile('print(1 + 1)', 'file', 'single')) exec(compile("if 1: 10 + 1\n", "file", "single"))
print(eval(compile('1 + 1', 'file', 'eval'))) exec(compile("print(10 + 2)", "file", "single"))
print(eval(compile("10 + 3", "file", "eval")))
# bad mode # bad mode
try: try:
...@@ -42,8 +42,4 @@ def test(): ...@@ -42,8 +42,4 @@ def test():
print("NameError") print("NameError")
print(x) # check 'x' still exists as a global print(x) # check 'x' still exists as a global
if have_compile(): test()
test()
else:
print("SKIP")
raise SystemExit
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment