From 57365d855734142deb030ebcd00c10efcedf554b Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 12 May 2021 17:02:06 +0200 Subject: py/objarray: Prohibit comparison of mismatching types. Array equality is defined as each element being equal but to keep code size down MicroPython implements a binary comparison. This can only be used correctly for elements with the same binary layout though so turn it into an NotImplementedError when comparing types for which the binary comparison yielded incorrect results: types with different sizes, and floating point numbers because nan != nan. --- tests/basics/array_micropython.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests/basics/array_micropython.py') diff --git a/tests/basics/array_micropython.py b/tests/basics/array_micropython.py index 6b3dc7a93..44dc1d83d 100644 --- a/tests/basics/array_micropython.py +++ b/tests/basics/array_micropython.py @@ -17,3 +17,15 @@ print(a[0]) a = array.array('P') a.append(1) print(a[0]) + +# comparison between mismatching binary layouts is not implemented +typecodes = ["b", "h", "i", "l", "q", "P", "O", "S", "f", "d"] +for a in typecodes: + for b in typecodes: + if a == b and a not in ["f", "d"]: + continue + try: + array.array(a) == array.array(b) + print('FAIL') + except NotImplementedError: + pass -- cgit v1.2.3