blob: 8e56df993fa368f311f762e6e294ca376f0ac1ca [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)
Antoine Pitrouc3b39242009-01-03 16:59:18 +000030 self.assertEquals(m[0], item(b"a"))
Ezio Melottie9615932010-01-24 19:26:24 +000031 self.assertIsInstance(m[0], bytes)
Antoine Pitrouc3b39242009-01-03 16:59:18 +000032 self.assertEquals(m[5], item(b"f"))
33 self.assertEquals(m[-1], item(b"f"))
34 self.assertEquals(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
45 self.assertEquals(sys.getrefcount(b), oldrefcount)
46
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
69 self.assertEquals(sys.getrefcount(b), oldrefcount)
70
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
112 self.assertEquals(sys.getrefcount(b), oldrefcount)
113
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")
130 self.assertEquals(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()
137 self.assertEquals(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))
171 self.assertEquals(m.format, self.format)
172 self.assertEquals(m.itemsize, self.itemsize)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000173 self.assertEquals(m.ndim, 1)
174 self.assertEquals(m.shape, (6,))
Benjamin Peterson5e19e442008-09-10 21:47:03 +0000175 self.assertEquals(len(m), 6)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000176 self.assertEquals(m.strides, (self.itemsize,))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000177 self.assertEquals(m.suboffsets, None)
178 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)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000184 self.assertEquals(m.readonly, True)
185
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)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000190 self.assertEquals(m.readonly, False)
191
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"))
201 self.assertEquals(sys.getrefcount(m), oldviewrefcount)
202 m = None
203 self.assertEquals(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 Pitrou616d2852008-08-19 22:09:34 +0000228
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000229# Variations on source objects for the buffer: bytes-like objects, then arrays
230# with itemsize > 1.
231# NOTE: support for multi-dimensional objects is unimplemented.
Antoine Pitrou616d2852008-08-19 22:09:34 +0000232
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000233class BaseBytesMemoryTests(AbstractMemoryTests):
234 ro_type = bytes
235 rw_type = bytearray
236 getitem_type = bytes
237 itemsize = 1
238 format = 'B'
239
240class BaseArrayMemoryTests(AbstractMemoryTests):
241 ro_type = None
242 rw_type = lambda self, b: array.array('i', list(b))
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000243 getitem_type = lambda self, b: array.array('i', list(b)).tobytes()
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000244 itemsize = array.array('i').itemsize
245 format = 'i'
246
247 def test_getbuffer(self):
248 # XXX Test should be adapted for non-byte buffers
249 pass
250
251 def test_tolist(self):
252 # XXX NotImplementedError: tolist() only supports byte views
253 pass
254
255
256# Variations on indirection levels: memoryview, slice of memoryview,
257# slice of slice of memoryview.
258# This is important to test allocation subtleties.
259
260class BaseMemoryviewTests:
Antoine Pitrou616d2852008-08-19 22:09:34 +0000261 def _view(self, obj):
262 return memoryview(obj)
263
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000264 def _check_contents(self, tp, obj, contents):
265 self.assertEquals(obj, tp(contents))
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000266
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000267class BaseMemorySliceTests:
268 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000269
270 def _view(self, obj):
271 m = memoryview(obj)
272 return m[1:7]
273
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000274 def _check_contents(self, tp, obj, contents):
275 self.assertEquals(obj[1:7], tp(contents))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000276
277 def test_refs(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000278 for tp in self._types:
279 m = memoryview(tp(self._source))
280 oldrefcount = sys.getrefcount(m)
281 m[1:2]
282 self.assertEquals(sys.getrefcount(m), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000283
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000284class BaseMemorySliceSliceTests:
285 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000286
287 def _view(self, obj):
288 m = memoryview(obj)
289 return m[:7][1:]
290
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000291 def _check_contents(self, tp, obj, contents):
292 self.assertEquals(obj[1:7], tp(contents))
293
294
295# Concrete test classes
296
297class BytesMemoryviewTest(unittest.TestCase,
298 BaseMemoryviewTests, BaseBytesMemoryTests):
299
300 def test_constructor(self):
301 for tp in self._types:
302 ob = tp(self._source)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000303 self.assertTrue(memoryview(ob))
304 self.assertTrue(memoryview(object=ob))
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000305 self.assertRaises(TypeError, memoryview)
306 self.assertRaises(TypeError, memoryview, ob, ob)
307 self.assertRaises(TypeError, memoryview, argument=ob)
308 self.assertRaises(TypeError, memoryview, ob, argument=True)
309
310class ArrayMemoryviewTest(unittest.TestCase,
311 BaseMemoryviewTests, BaseArrayMemoryTests):
312
313 def test_array_assign(self):
314 # Issue #4569: segfault when mutating a memoryview with itemsize != 1
315 a = array.array('i', range(10))
316 m = memoryview(a)
317 new_a = array.array('i', range(9, -1, -1))
318 m[:] = new_a
319 self.assertEquals(a, new_a)
320
321
322class BytesMemorySliceTest(unittest.TestCase,
323 BaseMemorySliceTests, BaseBytesMemoryTests):
324 pass
325
326class ArrayMemorySliceTest(unittest.TestCase,
327 BaseMemorySliceTests, BaseArrayMemoryTests):
328 pass
329
330class BytesMemorySliceSliceTest(unittest.TestCase,
331 BaseMemorySliceSliceTests, BaseBytesMemoryTests):
332 pass
333
334class ArrayMemorySliceSliceTest(unittest.TestCase,
335 BaseMemorySliceSliceTests, BaseArrayMemoryTests):
336 pass
Antoine Pitrou616d2852008-08-19 22:09:34 +0000337
338
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000339def test_main():
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000340 test.support.run_unittest(__name__)
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000341
342if __name__ == "__main__":
343 test_main()