1 """Unit tests for the memoryview
3 XXX We need more tests! Some tests are in test_bytes
11 from test
import test_support
14 class AbstractMemoryTests
:
15 source_bytes
= b
"abcdef"
19 return self
.source_bytes
23 return filter(None, [self
.ro_type
, self
.rw_type
])
25 def check_getitem_with_type(self
, tp
):
26 item
= self
.getitem_type
28 oldrefcount
= sys
.getrefcount(b
)
30 self
.assertEquals(m
[0], item(b
"a"))
31 self
.assertIsInstance(m
[0], bytes
)
32 self
.assertEquals(m
[5], item(b
"f"))
33 self
.assertEquals(m
[-1], item(b
"f"))
34 self
.assertEquals(m
[-6], item(b
"a"))
36 self
.assertRaises(IndexError, lambda: m
[6])
37 self
.assertRaises(IndexError, lambda: m
[-7])
38 self
.assertRaises(IndexError, lambda: m
[sys
.maxsize
])
39 self
.assertRaises(IndexError, lambda: m
[-sys
.maxsize
])
41 self
.assertRaises(TypeError, lambda: m
[None])
42 self
.assertRaises(TypeError, lambda: m
[0.0])
43 self
.assertRaises(TypeError, lambda: m
["a"])
45 self
.assertEquals(sys
.getrefcount(b
), oldrefcount
)
47 def test_getitem(self
):
48 for tp
in self
._types
:
49 self
.check_getitem_with_type(tp
)
52 for tp
in self
._types
:
55 self
.assertEqual(list(m
), [m
[i
] for i
in range(len(m
))])
58 for tp
in self
._types
:
61 self
.assertIsInstance(m
.__repr
__(), str)
63 def test_setitem_readonly(self
):
66 b
= self
.ro_type(self
._source
)
67 oldrefcount
= sys
.getrefcount(b
)
71 self
.assertRaises(TypeError, setitem
, b
"a")
72 self
.assertRaises(TypeError, setitem
, 65)
73 self
.assertRaises(TypeError, setitem
, memoryview(b
"a"))
75 self
.assertEquals(sys
.getrefcount(b
), oldrefcount
)
77 def test_setitem_writable(self
):
81 b
= self
.rw_type(self
._source
)
82 oldrefcount
= sys
.getrefcount(b
)
85 self
._check
_contents
(tp
, b
, b
"0bcdef")
87 self
._check
_contents
(tp
, b
, b
"012def")
89 self
._check
_contents
(tp
, b
, b
"012def")
91 self
._check
_contents
(tp
, b
, b
"abcdef")
93 # Overlapping copies of a view into itself
95 self
._check
_contents
(tp
, b
, b
"cdedef")
98 self
._check
_contents
(tp
, b
, b
"ababcf")
100 def setitem(key
, value
):
103 self
.assertRaises(IndexError, setitem
, 6, b
"a")
104 self
.assertRaises(IndexError, setitem
, -7, b
"a")
105 self
.assertRaises(IndexError, setitem
, sys
.maxsize
, b
"a")
106 self
.assertRaises(IndexError, setitem
, -sys
.maxsize
, b
"a")
107 # Wrong index/slice types
108 self
.assertRaises(TypeError, setitem
, 0.0, b
"a")
109 self
.assertRaises(TypeError, setitem
, (0,), b
"a")
110 self
.assertRaises(TypeError, setitem
, "a", b
"a")
111 # Trying to resize the memory object
112 self
.assertRaises(ValueError, setitem
, 0, b
"")
113 self
.assertRaises(ValueError, setitem
, 0, b
"ab")
114 self
.assertRaises(ValueError, setitem
, slice(1,1), b
"a")
115 self
.assertRaises(ValueError, setitem
, slice(0,2), b
"a")
118 self
.assertEquals(sys
.getrefcount(b
), oldrefcount
)
120 def test_tobytes(self
):
121 for tp
in self
._types
:
122 m
= self
._view
(tp(self
._source
))
124 # This calls self.getitem_type() on each separate byte of b"abcdef"
126 self
.getitem_type(c
) for c
in b
"abcdef")
127 self
.assertEquals(b
, expected
)
128 self
.assertIsInstance(b
, bytes
)
130 def test_tolist(self
):
131 for tp
in self
._types
:
132 m
= self
._view
(tp(self
._source
))
134 self
.assertEquals(l
, map(ord, b
"abcdef"))
136 def test_compare(self
):
137 # memoryviews can compare for equality with other objects
138 # having the buffer interface.
139 for tp
in self
._types
:
140 m
= self
._view
(tp(self
._source
))
141 for tp_comp
in self
._types
:
142 self
.assertTrue(m
== tp_comp(b
"abcdef"))
143 self
.assertFalse(m
!= tp_comp(b
"abcdef"))
144 self
.assertFalse(m
== tp_comp(b
"abcde"))
145 self
.assertTrue(m
!= tp_comp(b
"abcde"))
146 self
.assertFalse(m
== tp_comp(b
"abcde1"))
147 self
.assertTrue(m
!= tp_comp(b
"abcde1"))
148 self
.assertTrue(m
== m
)
149 self
.assertTrue(m
== m
[:])
150 self
.assertTrue(m
[0:6] == m
[:])
151 self
.assertFalse(m
[0:5] == m
)
153 # Comparison with objects which don't support the buffer API
154 self
.assertFalse(m
== u
"abcdef")
155 self
.assertTrue(m
!= u
"abcdef")
156 self
.assertFalse(u
"abcdef" == m
)
157 self
.assertTrue(u
"abcdef" != m
)
159 # Unordered comparisons are unimplemented, and therefore give
160 # arbitrary results (they raise a TypeError in py3k)
162 def check_attributes_with_type(self
, tp
):
163 m
= self
._view
(tp(self
._source
))
164 self
.assertEquals(m
.format
, self
.format
)
165 self
.assertIsInstance(m
.format
, str)
166 self
.assertEquals(m
.itemsize
, self
.itemsize
)
167 self
.assertEquals(m
.ndim
, 1)
168 self
.assertEquals(m
.shape
, (6,))
169 self
.assertEquals(len(m
), 6)
170 self
.assertEquals(m
.strides
, (self
.itemsize
,))
171 self
.assertEquals(m
.suboffsets
, None)
174 def test_attributes_readonly(self
):
177 m
= self
.check_attributes_with_type(self
.ro_type
)
178 self
.assertEquals(m
.readonly
, True)
180 def test_attributes_writable(self
):
183 m
= self
.check_attributes_with_type(self
.rw_type
)
184 self
.assertEquals(m
.readonly
, False)
186 # Disabled: unicode uses the old buffer API in 2.x
188 #def test_getbuffer(self):
189 ## Test PyObject_GetBuffer() on a memoryview object.
190 #for tp in self._types:
191 #b = tp(self._source)
192 #oldrefcount = sys.getrefcount(b)
194 #oldviewrefcount = sys.getrefcount(m)
195 #s = unicode(m, "utf-8")
196 #self._check_contents(tp, b, s.encode("utf-8"))
197 #self.assertEquals(sys.getrefcount(m), oldviewrefcount)
199 #self.assertEquals(sys.getrefcount(b), oldrefcount)
202 for tp
in self
._types
:
203 if not isinstance(tp
, type):
204 # If tp is a factory rather than a plain type, skip
212 # Create a reference cycle through a memoryview object
213 b
= MySource(tp(b
'abc'))
220 # The cycle must be broken
222 self
.assertTrue(wr() is None, wr())
225 # Variations on source objects for the buffer: bytes-like objects, then arrays
227 # NOTE: support for multi-dimensional objects is unimplemented.
229 class BaseBytesMemoryTests(AbstractMemoryTests
):
236 # Disabled: array.array() does not support the new buffer API in 2.x
238 #class BaseArrayMemoryTests(AbstractMemoryTests):
240 #rw_type = lambda self, b: array.array('i', map(ord, b))
241 #getitem_type = lambda self, b: array.array('i', map(ord, b)).tostring()
242 #itemsize = array.array('i').itemsize
245 #def test_getbuffer(self):
246 ## XXX Test should be adapted for non-byte buffers
249 #def test_tolist(self):
250 ## XXX NotImplementedError: tolist() only supports byte views
254 # Variations on indirection levels: memoryview, slice of memoryview,
255 # slice of slice of memoryview.
256 # This is important to test allocation subtleties.
258 class BaseMemoryviewTests
:
259 def _view(self
, obj
):
260 return memoryview(obj
)
262 def _check_contents(self
, tp
, obj
, contents
):
263 self
.assertEquals(obj
, tp(contents
))
265 class BaseMemorySliceTests
:
266 source_bytes
= b
"XabcdefY"
268 def _view(self
, obj
):
272 def _check_contents(self
, tp
, obj
, contents
):
273 self
.assertEquals(obj
[1:7], tp(contents
))
276 for tp
in self
._types
:
277 m
= memoryview(tp(self
._source
))
278 oldrefcount
= sys
.getrefcount(m
)
280 self
.assertEquals(sys
.getrefcount(m
), oldrefcount
)
282 class BaseMemorySliceSliceTests
:
283 source_bytes
= b
"XabcdefY"
285 def _view(self
, obj
):
289 def _check_contents(self
, tp
, obj
, contents
):
290 self
.assertEquals(obj
[1:7], tp(contents
))
293 # Concrete test classes
295 class BytesMemoryviewTest(unittest
.TestCase
,
296 BaseMemoryviewTests
, BaseBytesMemoryTests
):
298 def test_constructor(self
):
299 for tp
in self
._types
:
300 ob
= tp(self
._source
)
301 self
.assertTrue(memoryview(ob
))
302 self
.assertTrue(memoryview(object=ob
))
303 self
.assertRaises(TypeError, memoryview
)
304 self
.assertRaises(TypeError, memoryview
, ob
, ob
)
305 self
.assertRaises(TypeError, memoryview
, argument
=ob
)
306 self
.assertRaises(TypeError, memoryview
, ob
, argument
=True)
308 #class ArrayMemoryviewTest(unittest.TestCase,
309 #BaseMemoryviewTests, BaseArrayMemoryTests):
311 #def test_array_assign(self):
312 ## Issue #4569: segfault when mutating a memoryview with itemsize != 1
313 #a = array.array('i', range(10))
315 #new_a = array.array('i', range(9, -1, -1))
317 #self.assertEquals(a, new_a)
320 class BytesMemorySliceTest(unittest
.TestCase
,
321 BaseMemorySliceTests
, BaseBytesMemoryTests
):
324 #class ArrayMemorySliceTest(unittest.TestCase,
325 #BaseMemorySliceTests, BaseArrayMemoryTests):
328 class BytesMemorySliceSliceTest(unittest
.TestCase
,
329 BaseMemorySliceSliceTests
, BaseBytesMemoryTests
):
332 #class ArrayMemorySliceSliceTest(unittest.TestCase,
333 #BaseMemorySliceSliceTests, BaseArrayMemoryTests):
338 test_support
.run_unittest(__name__
)
340 if __name__
== "__main__":