summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2020-02-28 12:45:36 +1100
committerDamien George <damien.p.george@gmail.com>2020-02-28 12:45:36 +1100
commit1993c8cf9af930cd6ff2f28390ac3a8a09d5b297 (patch)
tree84dbe9d016c9cc362c6deae0869b37cae4b64c6c
parent54a54f5872ae489547f84600aab8471e6e83d715 (diff)
downloadmicropython-1993c8cf9af930cd6ff2f28390ac3a8a09d5b297.tar.gz
micropython-1993c8cf9af930cd6ff2f28390ac3a8a09d5b297.zip
py/builtinevex: Support passing in a bytearray/buffer to eval/exec.
CPython allows this and it's a simple generalisation of the existing code which just supported str/bytes. Fixes issue #5704.
-rw-r--r--py/builtinevex.c9
-rw-r--r--tests/basics/builtin_eval_buffer.py12
-rw-r--r--tests/basics/builtin_exec_buffer.py12
3 files changed, 29 insertions, 4 deletions
diff --git a/py/builtinevex.c b/py/builtinevex.c
index a4bf7d924a..4e61205485 100644
--- a/py/builtinevex.c
+++ b/py/builtinevex.c
@@ -136,17 +136,18 @@ STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_i
}
#endif
- size_t str_len;
- const char *str = mp_obj_str_get_data(args[0], &str_len);
+ // Extract the source code.
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
// create the lexer
// MP_PARSE_SINGLE_INPUT is used to indicate a file input
mp_lexer_t *lex;
if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
- lex = mp_lexer_new_from_file(str);
+ lex = mp_lexer_new_from_file(bufinfo.buf);
parse_input_kind = MP_PARSE_FILE_INPUT;
} else {
- lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
+ lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, bufinfo.buf, bufinfo.len, 0);
}
return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
diff --git a/tests/basics/builtin_eval_buffer.py b/tests/basics/builtin_eval_buffer.py
new file mode 100644
index 0000000000..9eaf28204d
--- /dev/null
+++ b/tests/basics/builtin_eval_buffer.py
@@ -0,0 +1,12 @@
+# test builtin eval with a buffer (bytearray/memoryview) input
+
+try:
+ eval
+ bytearray
+ memoryview
+except:
+ print("SKIP")
+ raise SystemExit
+
+print(eval(bytearray(b'1 + 1')))
+print(eval(memoryview(b'2 + 2')))
diff --git a/tests/basics/builtin_exec_buffer.py b/tests/basics/builtin_exec_buffer.py
new file mode 100644
index 0000000000..a875cfb1fa
--- /dev/null
+++ b/tests/basics/builtin_exec_buffer.py
@@ -0,0 +1,12 @@
+# test builtin exec with a buffer (bytearray/memoryview) input
+
+try:
+ exec
+ bytearray
+ memoryview
+except:
+ print("SKIP")
+ raise SystemExit
+
+exec(bytearray(b'print(1)'))
+exec(memoryview(b'print(2)'))