1# test memoryview 2try: 3 memoryview 4except: 5 print("SKIP") 6 raise SystemExit 7try: 8 import uarray as array 9except ImportError: 10 try: 11 import array 12 except ImportError: 13 print("SKIP") 14 raise SystemExit 15 16# test reading from bytes 17b = b'1234' 18m = memoryview(b) 19print(len(m)) 20print(m[0], m[1], m[-1]) 21print(list(m)) 22 23# test writing to bytes 24try: 25 m[0] = 1 26except TypeError: 27 print("TypeError") 28try: 29 m[0:2] = b'00' 30except TypeError: 31 print("TypeError") 32 33# test writing to bytearray 34b = bytearray(b) 35m = memoryview(b) 36m[0] = 1 37print(b) 38print(list(m)) 39 40# test slice 41m = memoryview(b'1234') 42print(list(m[1:])) 43print(list(m[1:-1])) 44 45# this tests get_buffer of memoryview 46m = memoryview(bytearray(2)) 47print(bytearray(m)) 48print(list(memoryview(memoryview(b'1234')))) # read-only memoryview 49 50a = array.array('i', [1, 2, 3, 4]) 51m = memoryview(a) 52print(list(m)) 53print(list(m[1:-1])) 54m[2] = 6 55print(a) 56 57# invalid attribute 58try: 59 memoryview(b'a').noexist 60except AttributeError: 61 print('AttributeError') 62 63# equality 64print(memoryview(b'abc') == b'abc') 65print(memoryview(b'abc') != b'abc') 66print(memoryview(b'abc') == b'xyz') 67print(memoryview(b'abc') != b'xyz') 68print(b'abc' == memoryview(b'abc')) 69print(b'abc' != memoryview(b'abc')) 70print(b'abc' == memoryview(b'xyz')) 71print(b'abc' != memoryview(b'xyz')) 72print(memoryview(b'abcdef')[2:4] == b'cd') 73print(memoryview(b'abcdef')[2:4] != b'cd') 74print(memoryview(b'abcdef')[2:4] == b'xy') 75print(memoryview(b'abcdef')[2:4] != b'xy') 76print(b'cd' == memoryview(b'abcdef')[2:4]) 77print(b'cd' != memoryview(b'abcdef')[2:4]) 78print(b'xy' == memoryview(b'abcdef')[2:4]) 79print(b'xy' != memoryview(b'abcdef')[2:4]) 80