blob: 95071b5d8bdacdadcd0f88d464c2bf17aa2677a5 [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 Pitrou616d2852008-08-19 22:09:34 +0000114 def test_tobytes(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000115 for tp in self._types:
116 m = self._view(tp(self._source))
117 b = m.tobytes()
118 # This calls self.getitem_type() on each separate byte of b"abcdef"
119 expected = b"".join(
120 self.getitem_type(bytes([c])) for c in b"abcdef")
121 self.assertEquals(b, expected)
Ezio Melottie9615932010-01-24 19:26:24 +0000122 self.assertIsInstance(b, bytes)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000123
124 def test_tolist(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000125 for tp in self._types:
126 m = self._view(tp(self._source))
127 l = m.tolist()
128 self.assertEquals(l, list(b"abcdef"))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000129
130 def test_compare(self):
131 # memoryviews can compare for equality with other objects
132 # having the buffer interface.
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000133 for tp in self._types:
134 m = self._view(tp(self._source))
135 for tp_comp in self._types:
136 self.assertTrue(m == tp_comp(b"abcdef"))
137 self.assertFalse(m != tp_comp(b"abcdef"))
138 self.assertFalse(m == tp_comp(b"abcde"))
139 self.assertTrue(m != tp_comp(b"abcde"))
140 self.assertFalse(m == tp_comp(b"abcde1"))
141 self.assertTrue(m != tp_comp(b"abcde1"))
142 self.assertTrue(m == m)
143 self.assertTrue(m == m[:])
144 self.assertTrue(m[0:6] == m[:])
145 self.assertFalse(m[0:5] == m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000146
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000147 # Comparison with objects which don't support the buffer API
148 self.assertFalse(m == "abcdef")
149 self.assertTrue(m != "abcdef")
150 self.assertFalse("abcdef" == m)
151 self.assertTrue("abcdef" != m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000152
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000153 # Unordered comparisons
154 for c in (m, b"abcdef"):
155 self.assertRaises(TypeError, lambda: m < c)
156 self.assertRaises(TypeError, lambda: c <= m)
157 self.assertRaises(TypeError, lambda: m >= c)
158 self.assertRaises(TypeError, lambda: c > m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000159
160 def check_attributes_with_type(self, tp):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000161 m = self._view(tp(self._source))
162 self.assertEquals(m.format, self.format)
163 self.assertEquals(m.itemsize, self.itemsize)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000164 self.assertEquals(m.ndim, 1)
165 self.assertEquals(m.shape, (6,))
Benjamin Peterson5e19e442008-09-10 21:47:03 +0000166 self.assertEquals(len(m), 6)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000167 self.assertEquals(m.strides, (self.itemsize,))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000168 self.assertEquals(m.suboffsets, None)
169 return m
170
171 def test_attributes_readonly(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000172 if not self.ro_type:
173 return
174 m = self.check_attributes_with_type(self.ro_type)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000175 self.assertEquals(m.readonly, True)
176
177 def test_attributes_writable(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000178 if not self.rw_type:
179 return
180 m = self.check_attributes_with_type(self.rw_type)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000181 self.assertEquals(m.readonly, False)
182
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000183 def test_getbuffer(self):
184 # Test PyObject_GetBuffer() on a memoryview object.
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000185 for tp in self._types:
186 b = tp(self._source)
187 oldrefcount = sys.getrefcount(b)
188 m = self._view(b)
189 oldviewrefcount = sys.getrefcount(m)
190 s = str(m, "utf-8")
191 self._check_contents(tp, b, s.encode("utf-8"))
192 self.assertEquals(sys.getrefcount(m), oldviewrefcount)
193 m = None
194 self.assertEquals(sys.getrefcount(b), oldrefcount)
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000195
196 def test_gc(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000197 for tp in self._types:
198 if not isinstance(tp, type):
199 # If tp is a factory rather than a plain type, skip
200 continue
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000201
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000202 class MySource(tp):
203 pass
204 class MyObject:
205 pass
206
207 # Create a reference cycle through a memoryview object
208 b = MySource(tp(b'abc'))
209 m = self._view(b)
210 o = MyObject()
211 b.m = m
212 b.o = o
213 wr = weakref.ref(o)
214 b = m = o = None
215 # The cycle must be broken
216 gc.collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000217 self.assertTrue(wr() is None, wr())
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000218
Antoine Pitrou616d2852008-08-19 22:09:34 +0000219
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000220# Variations on source objects for the buffer: bytes-like objects, then arrays
221# with itemsize > 1.
222# NOTE: support for multi-dimensional objects is unimplemented.
Antoine Pitrou616d2852008-08-19 22:09:34 +0000223
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000224class BaseBytesMemoryTests(AbstractMemoryTests):
225 ro_type = bytes
226 rw_type = bytearray
227 getitem_type = bytes
228 itemsize = 1
229 format = 'B'
230
231class BaseArrayMemoryTests(AbstractMemoryTests):
232 ro_type = None
233 rw_type = lambda self, b: array.array('i', list(b))
234 getitem_type = lambda self, b: array.array('i', list(b)).tostring()
235 itemsize = array.array('i').itemsize
236 format = 'i'
237
238 def test_getbuffer(self):
239 # XXX Test should be adapted for non-byte buffers
240 pass
241
242 def test_tolist(self):
243 # XXX NotImplementedError: tolist() only supports byte views
244 pass
245
246
247# Variations on indirection levels: memoryview, slice of memoryview,
248# slice of slice of memoryview.
249# This is important to test allocation subtleties.
250
251class BaseMemoryviewTests:
Antoine Pitrou616d2852008-08-19 22:09:34 +0000252 def _view(self, obj):
253 return memoryview(obj)
254
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000255 def _check_contents(self, tp, obj, contents):
256 self.assertEquals(obj, tp(contents))
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000257
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000258class BaseMemorySliceTests:
259 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000260
261 def _view(self, obj):
262 m = memoryview(obj)
263 return m[1:7]
264
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000265 def _check_contents(self, tp, obj, contents):
266 self.assertEquals(obj[1:7], tp(contents))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000267
268 def test_refs(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000269 for tp in self._types:
270 m = memoryview(tp(self._source))
271 oldrefcount = sys.getrefcount(m)
272 m[1:2]
273 self.assertEquals(sys.getrefcount(m), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000274
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000275class BaseMemorySliceSliceTests:
276 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000277
278 def _view(self, obj):
279 m = memoryview(obj)
280 return m[:7][1:]
281
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000282 def _check_contents(self, tp, obj, contents):
283 self.assertEquals(obj[1:7], tp(contents))
284
285
286# Concrete test classes
287
288class BytesMemoryviewTest(unittest.TestCase,
289 BaseMemoryviewTests, BaseBytesMemoryTests):
290
291 def test_constructor(self):
292 for tp in self._types:
293 ob = tp(self._source)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000294 self.assertTrue(memoryview(ob))
295 self.assertTrue(memoryview(object=ob))
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000296 self.assertRaises(TypeError, memoryview)
297 self.assertRaises(TypeError, memoryview, ob, ob)
298 self.assertRaises(TypeError, memoryview, argument=ob)
299 self.assertRaises(TypeError, memoryview, ob, argument=True)
300
301class ArrayMemoryviewTest(unittest.TestCase,
302 BaseMemoryviewTests, BaseArrayMemoryTests):
303
304 def test_array_assign(self):
305 # Issue #4569: segfault when mutating a memoryview with itemsize != 1
306 a = array.array('i', range(10))
307 m = memoryview(a)
308 new_a = array.array('i', range(9, -1, -1))
309 m[:] = new_a
310 self.assertEquals(a, new_a)
311
312
313class BytesMemorySliceTest(unittest.TestCase,
314 BaseMemorySliceTests, BaseBytesMemoryTests):
315 pass
316
317class ArrayMemorySliceTest(unittest.TestCase,
318 BaseMemorySliceTests, BaseArrayMemoryTests):
319 pass
320
321class BytesMemorySliceSliceTest(unittest.TestCase,
322 BaseMemorySliceSliceTests, BaseBytesMemoryTests):
323 pass
324
325class ArrayMemorySliceSliceTest(unittest.TestCase,
326 BaseMemorySliceSliceTests, BaseArrayMemoryTests):
327 pass
Antoine Pitrou616d2852008-08-19 22:09:34 +0000328
329
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000330def test_main():
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000331 test.support.run_unittest(__name__)
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000332
333if __name__ == "__main__":
334 test_main()