blob: 7bccf8b61b85d205804ec51fbc92b19b2fd6a227 [file] [log] [blame]
Christian Heimes7b6fc8e2007-11-08 02:28:11 +00001"""Unit tests for the memoryview
2
3XXX We need more tests! Some tests are in test_bytes
4"""
5
6import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007import test.support
Antoine Pitrou616d2852008-08-19 22:09:34 +00008import sys
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +00009import gc
10import weakref
Antoine Pitrouc3b39242009-01-03 16:59:18 +000011import array
Christian Heimes7b6fc8e2007-11-08 02:28:11 +000012
Antoine Pitrou616d2852008-08-19 22:09:34 +000013
Antoine Pitrouc3b39242009-01-03 16:59:18 +000014class AbstractMemoryTests:
15 source_bytes = b"abcdef"
Antoine Pitrou616d2852008-08-19 22:09:34 +000016
Antoine Pitrouc3b39242009-01-03 16:59:18 +000017 @property
18 def _source(self):
19 return self.source_bytes
20
21 @property
22 def _types(self):
23 return filter(None, [self.ro_type, self.rw_type])
Antoine Pitrou616d2852008-08-19 22:09:34 +000024
25 def check_getitem_with_type(self, tp):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000026 item = self.getitem_type
27 b = tp(self._source)
Antoine Pitrou616d2852008-08-19 22:09:34 +000028 oldrefcount = sys.getrefcount(b)
29 m = self._view(b)
Ezio Melottib3aedd42010-11-20 19:04:17 +000030 self.assertEqual(m[0], item(b"a"))
Ezio Melottie9615932010-01-24 19:26:24 +000031 self.assertIsInstance(m[0], bytes)
Ezio Melottib3aedd42010-11-20 19:04:17 +000032 self.assertEqual(m[5], item(b"f"))
33 self.assertEqual(m[-1], item(b"f"))
34 self.assertEqual(m[-6], item(b"a"))
Antoine Pitrou616d2852008-08-19 22:09:34 +000035 # Bounds checking
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])
40 # Type checking
41 self.assertRaises(TypeError, lambda: m[None])
42 self.assertRaises(TypeError, lambda: m[0.0])
43 self.assertRaises(TypeError, lambda: m["a"])
44 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +000045 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +000046
Antoine Pitrouc3b39242009-01-03 16:59:18 +000047 def test_getitem(self):
48 for tp in self._types:
49 self.check_getitem_with_type(tp)
Antoine Pitrou616d2852008-08-19 22:09:34 +000050
Raymond Hettinger159eac92009-06-23 20:38:54 +000051 def test_iter(self):
52 for tp in self._types:
53 b = tp(self._source)
54 m = self._view(b)
55 self.assertEqual(list(m), [m[i] for i in range(len(m))])
56
Antoine Pitrou616d2852008-08-19 22:09:34 +000057 def test_setitem_readonly(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000058 if not self.ro_type:
59 return
60 b = self.ro_type(self._source)
Antoine Pitrou616d2852008-08-19 22:09:34 +000061 oldrefcount = sys.getrefcount(b)
62 m = self._view(b)
63 def setitem(value):
64 m[0] = value
65 self.assertRaises(TypeError, setitem, b"a")
66 self.assertRaises(TypeError, setitem, 65)
67 self.assertRaises(TypeError, setitem, memoryview(b"a"))
68 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +000069 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +000070
71 def test_setitem_writable(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000072 if not self.rw_type:
73 return
74 tp = self.rw_type
75 b = self.rw_type(self._source)
Antoine Pitrou616d2852008-08-19 22:09:34 +000076 oldrefcount = sys.getrefcount(b)
77 m = self._view(b)
Antoine Pitrouc3b39242009-01-03 16:59:18 +000078 m[0] = tp(b"0")
79 self._check_contents(tp, b, b"0bcdef")
80 m[1:3] = tp(b"12")
81 self._check_contents(tp, b, b"012def")
82 m[1:1] = tp(b"")
83 self._check_contents(tp, b, b"012def")
84 m[:] = tp(b"abcdef")
85 self._check_contents(tp, b, b"abcdef")
Antoine Pitrou616d2852008-08-19 22:09:34 +000086
87 # Overlapping copies of a view into itself
88 m[0:3] = m[2:5]
Antoine Pitrouc3b39242009-01-03 16:59:18 +000089 self._check_contents(tp, b, b"cdedef")
90 m[:] = tp(b"abcdef")
Antoine Pitrou616d2852008-08-19 22:09:34 +000091 m[2:5] = m[0:3]
Antoine Pitrouc3b39242009-01-03 16:59:18 +000092 self._check_contents(tp, b, b"ababcf")
Antoine Pitrou616d2852008-08-19 22:09:34 +000093
94 def setitem(key, value):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000095 m[key] = tp(value)
Antoine Pitrou616d2852008-08-19 22:09:34 +000096 # Bounds checking
97 self.assertRaises(IndexError, setitem, 6, b"a")
98 self.assertRaises(IndexError, setitem, -7, b"a")
99 self.assertRaises(IndexError, setitem, sys.maxsize, b"a")
100 self.assertRaises(IndexError, setitem, -sys.maxsize, b"a")
101 # Wrong index/slice types
102 self.assertRaises(TypeError, setitem, 0.0, b"a")
103 self.assertRaises(TypeError, setitem, (0,), b"a")
104 self.assertRaises(TypeError, setitem, "a", b"a")
105 # Trying to resize the memory object
106 self.assertRaises(ValueError, setitem, 0, b"")
107 self.assertRaises(ValueError, setitem, 0, b"ab")
108 self.assertRaises(ValueError, setitem, slice(1,1), b"a")
109 self.assertRaises(ValueError, setitem, slice(0,2), b"a")
110
111 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +0000112 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000113
Antoine Pitroue0793ba2010-09-01 21:14:16 +0000114 def test_delitem(self):
115 for tp in self._types:
116 b = tp(self._source)
117 m = self._view(b)
118 with self.assertRaises(TypeError):
119 del m[1]
120 with self.assertRaises(TypeError):
121 del m[1:4]
122
Antoine Pitrou616d2852008-08-19 22:09:34 +0000123 def test_tobytes(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000124 for tp in self._types:
125 m = self._view(tp(self._source))
126 b = m.tobytes()
127 # This calls self.getitem_type() on each separate byte of b"abcdef"
128 expected = b"".join(
129 self.getitem_type(bytes([c])) for c in b"abcdef")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000130 self.assertEqual(b, expected)
Ezio Melottie9615932010-01-24 19:26:24 +0000131 self.assertIsInstance(b, bytes)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000132
133 def test_tolist(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000134 for tp in self._types:
135 m = self._view(tp(self._source))
136 l = m.tolist()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000137 self.assertEqual(l, list(b"abcdef"))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000138
139 def test_compare(self):
140 # memoryviews can compare for equality with other objects
141 # having the buffer interface.
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000142 for tp in self._types:
143 m = self._view(tp(self._source))
144 for tp_comp in self._types:
145 self.assertTrue(m == tp_comp(b"abcdef"))
146 self.assertFalse(m != tp_comp(b"abcdef"))
147 self.assertFalse(m == tp_comp(b"abcde"))
148 self.assertTrue(m != tp_comp(b"abcde"))
149 self.assertFalse(m == tp_comp(b"abcde1"))
150 self.assertTrue(m != tp_comp(b"abcde1"))
151 self.assertTrue(m == m)
152 self.assertTrue(m == m[:])
153 self.assertTrue(m[0:6] == m[:])
154 self.assertFalse(m[0:5] == m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000155
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000156 # Comparison with objects which don't support the buffer API
157 self.assertFalse(m == "abcdef")
158 self.assertTrue(m != "abcdef")
159 self.assertFalse("abcdef" == m)
160 self.assertTrue("abcdef" != m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000161
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000162 # Unordered comparisons
163 for c in (m, b"abcdef"):
164 self.assertRaises(TypeError, lambda: m < c)
165 self.assertRaises(TypeError, lambda: c <= m)
166 self.assertRaises(TypeError, lambda: m >= c)
167 self.assertRaises(TypeError, lambda: c > m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000168
169 def check_attributes_with_type(self, tp):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000170 m = self._view(tp(self._source))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000171 self.assertEqual(m.format, self.format)
172 self.assertEqual(m.itemsize, self.itemsize)
173 self.assertEqual(m.ndim, 1)
174 self.assertEqual(m.shape, (6,))
175 self.assertEqual(len(m), 6)
176 self.assertEqual(m.strides, (self.itemsize,))
177 self.assertEqual(m.suboffsets, None)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000178 return m
179
180 def test_attributes_readonly(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000181 if not self.ro_type:
182 return
183 m = self.check_attributes_with_type(self.ro_type)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000184 self.assertEqual(m.readonly, True)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000185
186 def test_attributes_writable(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000187 if not self.rw_type:
188 return
189 m = self.check_attributes_with_type(self.rw_type)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000190 self.assertEqual(m.readonly, False)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000191
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000192 def test_getbuffer(self):
193 # Test PyObject_GetBuffer() on a memoryview object.
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000194 for tp in self._types:
195 b = tp(self._source)
196 oldrefcount = sys.getrefcount(b)
197 m = self._view(b)
198 oldviewrefcount = sys.getrefcount(m)
199 s = str(m, "utf-8")
200 self._check_contents(tp, b, s.encode("utf-8"))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000201 self.assertEqual(sys.getrefcount(m), oldviewrefcount)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000202 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +0000203 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000204
205 def test_gc(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000206 for tp in self._types:
207 if not isinstance(tp, type):
208 # If tp is a factory rather than a plain type, skip
209 continue
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000210
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000211 class MySource(tp):
212 pass
213 class MyObject:
214 pass
215
216 # Create a reference cycle through a memoryview object
217 b = MySource(tp(b'abc'))
218 m = self._view(b)
219 o = MyObject()
220 b.m = m
221 b.o = o
222 wr = weakref.ref(o)
223 b = m = o = None
224 # The cycle must be broken
225 gc.collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000226 self.assertTrue(wr() is None, wr())
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000227
Antoine Pitrou6e6cc832010-09-09 12:59:39 +0000228 def _check_released(self, m, tp):
229 check = self.assertRaisesRegexp(ValueError, "released")
230 with check: bytes(m)
231 with check: m.tobytes()
232 with check: m.tolist()
233 with check: m[0]
234 with check: m[0] = b'x'
235 with check: len(m)
236 with check: m.format
237 with check: m.itemsize
238 with check: m.ndim
239 with check: m.readonly
240 with check: m.shape
241 with check: m.strides
242 with check:
243 with m:
244 pass
245 # str() and repr() still function
246 self.assertIn("released memory", str(m))
247 self.assertIn("released memory", repr(m))
248 self.assertEqual(m, m)
249 self.assertNotEqual(m, memoryview(tp(self._source)))
250 self.assertNotEqual(m, tp(self._source))
251
252 def test_contextmanager(self):
253 for tp in self._types:
254 b = tp(self._source)
255 m = self._view(b)
256 with m as cm:
257 self.assertIs(cm, m)
258 self._check_released(m, tp)
259 m = self._view(b)
260 # Can release explicitly inside the context manager
261 with m:
262 m.release()
263
264 def test_release(self):
265 for tp in self._types:
266 b = tp(self._source)
267 m = self._view(b)
268 m.release()
269 self._check_released(m, tp)
270 # Can be called a second time (it's a no-op)
271 m.release()
272 self._check_released(m, tp)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000273
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000274# Variations on source objects for the buffer: bytes-like objects, then arrays
275# with itemsize > 1.
276# NOTE: support for multi-dimensional objects is unimplemented.
Antoine Pitrou616d2852008-08-19 22:09:34 +0000277
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000278class BaseBytesMemoryTests(AbstractMemoryTests):
279 ro_type = bytes
280 rw_type = bytearray
281 getitem_type = bytes
282 itemsize = 1
283 format = 'B'
284
285class BaseArrayMemoryTests(AbstractMemoryTests):
286 ro_type = None
287 rw_type = lambda self, b: array.array('i', list(b))
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000288 getitem_type = lambda self, b: array.array('i', list(b)).tobytes()
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000289 itemsize = array.array('i').itemsize
290 format = 'i'
291
292 def test_getbuffer(self):
293 # XXX Test should be adapted for non-byte buffers
294 pass
295
296 def test_tolist(self):
297 # XXX NotImplementedError: tolist() only supports byte views
298 pass
299
300
301# Variations on indirection levels: memoryview, slice of memoryview,
302# slice of slice of memoryview.
303# This is important to test allocation subtleties.
304
305class BaseMemoryviewTests:
Antoine Pitrou616d2852008-08-19 22:09:34 +0000306 def _view(self, obj):
307 return memoryview(obj)
308
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000309 def _check_contents(self, tp, obj, contents):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000310 self.assertEqual(obj, tp(contents))
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000311
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000312class BaseMemorySliceTests:
313 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000314
315 def _view(self, obj):
316 m = memoryview(obj)
317 return m[1:7]
318
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000319 def _check_contents(self, tp, obj, contents):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000320 self.assertEqual(obj[1:7], tp(contents))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000321
322 def test_refs(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000323 for tp in self._types:
324 m = memoryview(tp(self._source))
325 oldrefcount = sys.getrefcount(m)
326 m[1:2]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000327 self.assertEqual(sys.getrefcount(m), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000328
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000329class BaseMemorySliceSliceTests:
330 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000331
332 def _view(self, obj):
333 m = memoryview(obj)
334 return m[:7][1:]
335
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000336 def _check_contents(self, tp, obj, contents):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000337 self.assertEqual(obj[1:7], tp(contents))
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000338
339
340# Concrete test classes
341
342class BytesMemoryviewTest(unittest.TestCase,
343 BaseMemoryviewTests, BaseBytesMemoryTests):
344
345 def test_constructor(self):
346 for tp in self._types:
347 ob = tp(self._source)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000348 self.assertTrue(memoryview(ob))
349 self.assertTrue(memoryview(object=ob))
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000350 self.assertRaises(TypeError, memoryview)
351 self.assertRaises(TypeError, memoryview, ob, ob)
352 self.assertRaises(TypeError, memoryview, argument=ob)
353 self.assertRaises(TypeError, memoryview, ob, argument=True)
354
355class ArrayMemoryviewTest(unittest.TestCase,
356 BaseMemoryviewTests, BaseArrayMemoryTests):
357
358 def test_array_assign(self):
359 # Issue #4569: segfault when mutating a memoryview with itemsize != 1
360 a = array.array('i', range(10))
361 m = memoryview(a)
362 new_a = array.array('i', range(9, -1, -1))
363 m[:] = new_a
Ezio Melottib3aedd42010-11-20 19:04:17 +0000364 self.assertEqual(a, new_a)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000365
366
367class BytesMemorySliceTest(unittest.TestCase,
368 BaseMemorySliceTests, BaseBytesMemoryTests):
369 pass
370
371class ArrayMemorySliceTest(unittest.TestCase,
372 BaseMemorySliceTests, BaseArrayMemoryTests):
373 pass
374
375class BytesMemorySliceSliceTest(unittest.TestCase,
376 BaseMemorySliceSliceTests, BaseBytesMemoryTests):
377 pass
378
379class ArrayMemorySliceSliceTest(unittest.TestCase,
380 BaseMemorySliceSliceTests, BaseArrayMemoryTests):
381 pass
Antoine Pitrou616d2852008-08-19 22:09:34 +0000382
383
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000384def test_main():
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000385 test.support.run_unittest(__name__)
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000386
387if __name__ == "__main__":
388 test_main()