From 3ccce89b831bdccd1feaa41ea23bc5d3ea963b32 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 20 Feb 2020 21:29:30 +1100 Subject: [PATCH] py/objarray: Turn on MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE for memoryview. And add corresponding tests. Fixes #5674 (comparison of memoryview against bytes). --- py/objarray.c | 1 + tests/basics/memoryview1.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/py/objarray.c b/py/objarray.c index a6e6fca1c9..760eff8cb1 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -573,6 +573,7 @@ const mp_obj_type_t mp_type_bytearray = { #if MICROPY_PY_BUILTINS_MEMORYVIEW const mp_obj_type_t mp_type_memoryview = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, .name = MP_QSTR_memoryview, .make_new = memoryview_make_new, .getiter = array_iterator_new, diff --git a/tests/basics/memoryview1.py b/tests/basics/memoryview1.py index b5314f3e99..1bfeabdfdc 100644 --- a/tests/basics/memoryview1.py +++ b/tests/basics/memoryview1.py @@ -107,3 +107,21 @@ try: memoryview(b'a').noexist except AttributeError: print('AttributeError') + +# equality +print(memoryview(b'abc') == b'abc') +print(memoryview(b'abc') != b'abc') +print(memoryview(b'abc') == b'xyz') +print(memoryview(b'abc') != b'xyz') +print(b'abc' == memoryview(b'abc')) +print(b'abc' != memoryview(b'abc')) +print(b'abc' == memoryview(b'xyz')) +print(b'abc' != memoryview(b'xyz')) +print(memoryview(b'abcdef')[2:4] == b'cd') +print(memoryview(b'abcdef')[2:4] != b'cd') +print(memoryview(b'abcdef')[2:4] == b'xy') +print(memoryview(b'abcdef')[2:4] != b'xy') +print(b'cd' == memoryview(b'abcdef')[2:4]) +print(b'cd' != memoryview(b'abcdef')[2:4]) +print(b'xy' == memoryview(b'abcdef')[2:4]) +print(b'xy' != memoryview(b'abcdef')[2:4])