From ca9068e0efc5e53ff7042ef68ad4a4ec9ff91915 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 May 2023 23:14:44 +1000 Subject: [PATCH] py/objarray: Disallow memoryview addition. Following CPython. This is important for subsequent commits to work correctly. Signed-off-by: Damien George --- py/objarray.c | 6 ++++++ tests/basics/op_error_memoryview.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/py/objarray.c b/py/objarray.c index 0d9411d7c..a3adb255e 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -292,6 +292,12 @@ STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in); switch (op) { case MP_BINARY_OP_ADD: { + #if MICROPY_PY_BUILTINS_MEMORYVIEW + if (lhs->base.type == &mp_type_memoryview) { + return MP_OBJ_NULL; // op not supported + } + #endif + // allow to add anything that has the buffer protocol (extension to CPython) mp_buffer_info_t lhs_bufinfo; mp_buffer_info_t rhs_bufinfo; diff --git a/tests/basics/op_error_memoryview.py b/tests/basics/op_error_memoryview.py index 233f7f9ab..485370428 100644 --- a/tests/basics/op_error_memoryview.py +++ b/tests/basics/op_error_memoryview.py @@ -6,8 +6,18 @@ except: raise SystemExit # unsupported binary operators +try: + memoryview(b"") + b"" +except TypeError: + print("TypeError") + +try: + memoryview(b"") + memoryview(b"") +except TypeError: + print("TypeError") + try: m = memoryview(bytearray()) m += bytearray() except TypeError: - print('TypeError') + print("TypeError")