From 5f9b105244bb9f605f3ca157cd421967c665bd6e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 21 Aug 2020 10:30:09 +1000 Subject: [PATCH] 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: Damien George --- py/runtime.c | 2 +- tests/basics/builtin_compile.py | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/py/runtime.c b/py/runtime.c index 38da2f4538..c12271f4e2 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -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) { qstr source_name = lex->source_name; 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; if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) { diff --git a/tests/basics/builtin_compile.py b/tests/basics/builtin_compile.py index bf272aca15..a2f2cbe550 100644 --- a/tests/basics/builtin_compile.py +++ b/tests/basics/builtin_compile.py @@ -1,11 +1,10 @@ # test compile builtin -def have_compile(): - try: - compile - return True - except NameError: - return False +try: + compile +except NameError: + print("SKIP") + raise SystemExit def test(): global x @@ -26,8 +25,9 @@ def test(): exec(c, {}, {"x":3}) # single/eval mode - exec(compile('print(1 + 1)', 'file', 'single')) - print(eval(compile('1 + 1', 'file', 'eval'))) + exec(compile("if 1: 10 + 1\n", "file", "single")) + exec(compile("print(10 + 2)", "file", "single")) + print(eval(compile("10 + 3", "file", "eval"))) # bad mode try: @@ -42,8 +42,4 @@ def test(): print("NameError") print(x) # check 'x' still exists as a global -if have_compile(): - test() -else: - print("SKIP") - raise SystemExit +test()