2014-10-23 08:34:35 -04:00
|
|
|
# test memoryview
|
2017-02-15 10:11:16 -05:00
|
|
|
try:
|
|
|
|
memoryview
|
|
|
|
except:
|
|
|
|
print("SKIP")
|
2017-06-10 13:03:01 -04:00
|
|
|
raise SystemExit
|
2021-04-23 15:26:42 -04:00
|
|
|
try:
|
|
|
|
import uarray as array
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import array
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2014-10-23 08:34:35 -04:00
|
|
|
|
|
|
|
# test reading from bytes
|
|
|
|
b = b'1234'
|
|
|
|
m = memoryview(b)
|
|
|
|
print(len(m))
|
|
|
|
print(m[0], m[1], m[-1])
|
|
|
|
print(list(m))
|
|
|
|
|
|
|
|
# test writing to bytes
|
|
|
|
try:
|
|
|
|
m[0] = 1
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2017-02-27 00:09:57 -05:00
|
|
|
try:
|
|
|
|
m[0:2] = b'00'
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2014-10-23 08:34:35 -04:00
|
|
|
|
|
|
|
# test writing to bytearray
|
|
|
|
b = bytearray(b)
|
|
|
|
m = memoryview(b)
|
|
|
|
m[0] = 1
|
|
|
|
print(b)
|
|
|
|
print(list(m))
|
|
|
|
|
|
|
|
# test slice
|
|
|
|
m = memoryview(b'1234')
|
|
|
|
print(list(m[1:]))
|
|
|
|
print(list(m[1:-1]))
|
|
|
|
|
2015-03-25 19:10:09 -04:00
|
|
|
# this tests get_buffer of memoryview
|
|
|
|
m = memoryview(bytearray(2))
|
|
|
|
print(bytearray(m))
|
2016-12-21 01:51:42 -05:00
|
|
|
print(list(memoryview(memoryview(b'1234')))) # read-only memoryview
|
2015-03-25 19:10:09 -04:00
|
|
|
|
2014-10-23 08:34:35 -04:00
|
|
|
a = array.array('i', [1, 2, 3, 4])
|
|
|
|
m = memoryview(a)
|
|
|
|
print(list(m))
|
|
|
|
print(list(m[1:-1]))
|
|
|
|
m[2] = 6
|
|
|
|
print(a)
|
2021-04-22 20:55:39 -04:00
|
|
|
|
|
|
|
# invalid attribute
|
|
|
|
try:
|
|
|
|
memoryview(b'a').noexist
|
|
|
|
except AttributeError:
|
|
|
|
print('AttributeError')
|
2020-02-20 05:29:30 -05:00
|
|
|
|
|
|
|
# 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])
|