blob: 6fe61a4ecc3d2e346ea5eb0bf94088d1e7a72f45 [file] [log] [blame]
Christian Heimes7b6fc8e2007-11-08 02:28:11 +00001"""Unit tests for the memoryview
2
Stefan Krahaaf8e2e2012-08-19 12:50:24 +02003 Some tests are in test_bytes. Many tests that require _testbuffer.ndarray
4 are in test_buffer.
Christian Heimes7b6fc8e2007-11-08 02:28:11 +00005"""
6
7import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008import test.support
Antoine Pitrou616d2852008-08-19 22:09:34 +00009import sys
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +000010import gc
11import weakref
Antoine Pitrouc3b39242009-01-03 16:59:18 +000012import array
Antoine Pitrouad62b032011-01-18 18:57:52 +000013import io
Christian Heimes7b6fc8e2007-11-08 02:28:11 +000014
Antoine Pitrou616d2852008-08-19 22:09:34 +000015
Antoine Pitrouc3b39242009-01-03 16:59:18 +000016class AbstractMemoryTests:
17 source_bytes = b"abcdef"
Antoine Pitrou616d2852008-08-19 22:09:34 +000018
Antoine Pitrouc3b39242009-01-03 16:59:18 +000019 @property
20 def _source(self):
21 return self.source_bytes
22
23 @property
24 def _types(self):
25 return filter(None, [self.ro_type, self.rw_type])
Antoine Pitrou616d2852008-08-19 22:09:34 +000026
27 def check_getitem_with_type(self, tp):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000028 b = tp(self._source)
Antoine Pitrou616d2852008-08-19 22:09:34 +000029 oldrefcount = sys.getrefcount(b)
30 m = self._view(b)
Stefan Krah9a2d99e2012-02-25 12:24:21 +010031 self.assertEqual(m[0], ord(b"a"))
32 self.assertIsInstance(m[0], int)
33 self.assertEqual(m[5], ord(b"f"))
34 self.assertEqual(m[-1], ord(b"f"))
35 self.assertEqual(m[-6], ord(b"a"))
Antoine Pitrou616d2852008-08-19 22:09:34 +000036 # Bounds checking
37 self.assertRaises(IndexError, lambda: m[6])
38 self.assertRaises(IndexError, lambda: m[-7])
39 self.assertRaises(IndexError, lambda: m[sys.maxsize])
40 self.assertRaises(IndexError, lambda: m[-sys.maxsize])
41 # Type checking
42 self.assertRaises(TypeError, lambda: m[None])
43 self.assertRaises(TypeError, lambda: m[0.0])
44 self.assertRaises(TypeError, lambda: m["a"])
45 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +000046 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +000047
Antoine Pitrouc3b39242009-01-03 16:59:18 +000048 def test_getitem(self):
49 for tp in self._types:
50 self.check_getitem_with_type(tp)
Antoine Pitrou616d2852008-08-19 22:09:34 +000051
Raymond Hettinger159eac92009-06-23 20:38:54 +000052 def test_iter(self):
53 for tp in self._types:
54 b = tp(self._source)
55 m = self._view(b)
56 self.assertEqual(list(m), [m[i] for i in range(len(m))])
57
Antoine Pitrou616d2852008-08-19 22:09:34 +000058 def test_setitem_readonly(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000059 if not self.ro_type:
Zachary Ware9fe6d862013-12-08 00:20:35 -060060 self.skipTest("no read-only type to test")
Antoine Pitrouc3b39242009-01-03 16:59:18 +000061 b = self.ro_type(self._source)
Antoine Pitrou616d2852008-08-19 22:09:34 +000062 oldrefcount = sys.getrefcount(b)
63 m = self._view(b)
64 def setitem(value):
65 m[0] = value
66 self.assertRaises(TypeError, setitem, b"a")
67 self.assertRaises(TypeError, setitem, 65)
68 self.assertRaises(TypeError, setitem, memoryview(b"a"))
69 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +000070 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +000071
72 def test_setitem_writable(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000073 if not self.rw_type:
Zachary Ware9fe6d862013-12-08 00:20:35 -060074 self.skipTest("no writable type to test")
Antoine Pitrouc3b39242009-01-03 16:59:18 +000075 tp = self.rw_type
76 b = self.rw_type(self._source)
Antoine Pitrou616d2852008-08-19 22:09:34 +000077 oldrefcount = sys.getrefcount(b)
78 m = self._view(b)
Stefan Krah9a2d99e2012-02-25 12:24:21 +010079 m[0] = ord(b'1')
80 self._check_contents(tp, b, b"1bcdef")
81 m[0:1] = tp(b"0")
Antoine Pitrouc3b39242009-01-03 16:59:18 +000082 self._check_contents(tp, b, b"0bcdef")
83 m[1:3] = tp(b"12")
84 self._check_contents(tp, b, b"012def")
85 m[1:1] = tp(b"")
86 self._check_contents(tp, b, b"012def")
87 m[:] = tp(b"abcdef")
88 self._check_contents(tp, b, b"abcdef")
Antoine Pitrou616d2852008-08-19 22:09:34 +000089
90 # Overlapping copies of a view into itself
91 m[0:3] = m[2:5]
Antoine Pitrouc3b39242009-01-03 16:59:18 +000092 self._check_contents(tp, b, b"cdedef")
93 m[:] = tp(b"abcdef")
Antoine Pitrou616d2852008-08-19 22:09:34 +000094 m[2:5] = m[0:3]
Antoine Pitrouc3b39242009-01-03 16:59:18 +000095 self._check_contents(tp, b, b"ababcf")
Antoine Pitrou616d2852008-08-19 22:09:34 +000096
97 def setitem(key, value):
Antoine Pitrouc3b39242009-01-03 16:59:18 +000098 m[key] = tp(value)
Antoine Pitrou616d2852008-08-19 22:09:34 +000099 # Bounds checking
100 self.assertRaises(IndexError, setitem, 6, b"a")
101 self.assertRaises(IndexError, setitem, -7, b"a")
102 self.assertRaises(IndexError, setitem, sys.maxsize, b"a")
103 self.assertRaises(IndexError, setitem, -sys.maxsize, b"a")
104 # Wrong index/slice types
105 self.assertRaises(TypeError, setitem, 0.0, b"a")
106 self.assertRaises(TypeError, setitem, (0,), b"a")
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100107 self.assertRaises(TypeError, setitem, (slice(0,1,1), 0), b"a")
108 self.assertRaises(TypeError, setitem, (0, slice(0,1,1)), b"a")
109 self.assertRaises(TypeError, setitem, (0,), b"a")
Antoine Pitrou616d2852008-08-19 22:09:34 +0000110 self.assertRaises(TypeError, setitem, "a", b"a")
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100111 # Not implemented: multidimensional slices
112 slices = (slice(0,1,1), slice(0,1,2))
113 self.assertRaises(NotImplementedError, setitem, slices, b"a")
Antoine Pitrou616d2852008-08-19 22:09:34 +0000114 # Trying to resize the memory object
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100115 exc = ValueError if m.format == 'c' else TypeError
116 self.assertRaises(exc, setitem, 0, b"")
117 self.assertRaises(exc, setitem, 0, b"ab")
Antoine Pitrou616d2852008-08-19 22:09:34 +0000118 self.assertRaises(ValueError, setitem, slice(1,1), b"a")
119 self.assertRaises(ValueError, setitem, slice(0,2), b"a")
120
121 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +0000122 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000123
Antoine Pitroue0793ba2010-09-01 21:14:16 +0000124 def test_delitem(self):
125 for tp in self._types:
126 b = tp(self._source)
127 m = self._view(b)
128 with self.assertRaises(TypeError):
129 del m[1]
130 with self.assertRaises(TypeError):
131 del m[1:4]
132
Antoine Pitrou616d2852008-08-19 22:09:34 +0000133 def test_tobytes(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000134 for tp in self._types:
135 m = self._view(tp(self._source))
136 b = m.tobytes()
137 # This calls self.getitem_type() on each separate byte of b"abcdef"
138 expected = b"".join(
139 self.getitem_type(bytes([c])) for c in b"abcdef")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000140 self.assertEqual(b, expected)
Ezio Melottie9615932010-01-24 19:26:24 +0000141 self.assertIsInstance(b, bytes)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000142
143 def test_tolist(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000144 for tp in self._types:
145 m = self._view(tp(self._source))
146 l = m.tolist()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000147 self.assertEqual(l, list(b"abcdef"))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000148
149 def test_compare(self):
150 # memoryviews can compare for equality with other objects
151 # having the buffer interface.
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000152 for tp in self._types:
153 m = self._view(tp(self._source))
154 for tp_comp in self._types:
155 self.assertTrue(m == tp_comp(b"abcdef"))
156 self.assertFalse(m != tp_comp(b"abcdef"))
157 self.assertFalse(m == tp_comp(b"abcde"))
158 self.assertTrue(m != tp_comp(b"abcde"))
159 self.assertFalse(m == tp_comp(b"abcde1"))
160 self.assertTrue(m != tp_comp(b"abcde1"))
161 self.assertTrue(m == m)
162 self.assertTrue(m == m[:])
163 self.assertTrue(m[0:6] == m[:])
164 self.assertFalse(m[0:5] == m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000165
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000166 # Comparison with objects which don't support the buffer API
167 self.assertFalse(m == "abcdef")
168 self.assertTrue(m != "abcdef")
169 self.assertFalse("abcdef" == m)
170 self.assertTrue("abcdef" != m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000171
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000172 # Unordered comparisons
173 for c in (m, b"abcdef"):
174 self.assertRaises(TypeError, lambda: m < c)
175 self.assertRaises(TypeError, lambda: c <= m)
176 self.assertRaises(TypeError, lambda: m >= c)
177 self.assertRaises(TypeError, lambda: c > m)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000178
179 def check_attributes_with_type(self, tp):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000180 m = self._view(tp(self._source))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000181 self.assertEqual(m.format, self.format)
182 self.assertEqual(m.itemsize, self.itemsize)
183 self.assertEqual(m.ndim, 1)
184 self.assertEqual(m.shape, (6,))
185 self.assertEqual(len(m), 6)
186 self.assertEqual(m.strides, (self.itemsize,))
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100187 self.assertEqual(m.suboffsets, ())
Antoine Pitrou616d2852008-08-19 22:09:34 +0000188 return m
189
190 def test_attributes_readonly(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000191 if not self.ro_type:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600192 self.skipTest("no read-only type to test")
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000193 m = self.check_attributes_with_type(self.ro_type)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000194 self.assertEqual(m.readonly, True)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000195
196 def test_attributes_writable(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000197 if not self.rw_type:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600198 self.skipTest("no writable type to test")
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000199 m = self.check_attributes_with_type(self.rw_type)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000200 self.assertEqual(m.readonly, False)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000201
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000202 def test_getbuffer(self):
203 # Test PyObject_GetBuffer() on a memoryview object.
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000204 for tp in self._types:
205 b = tp(self._source)
206 oldrefcount = sys.getrefcount(b)
207 m = self._view(b)
208 oldviewrefcount = sys.getrefcount(m)
209 s = str(m, "utf-8")
210 self._check_contents(tp, b, s.encode("utf-8"))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000211 self.assertEqual(sys.getrefcount(m), oldviewrefcount)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000212 m = None
Ezio Melottib3aedd42010-11-20 19:04:17 +0000213 self.assertEqual(sys.getrefcount(b), oldrefcount)
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000214
215 def test_gc(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000216 for tp in self._types:
217 if not isinstance(tp, type):
218 # If tp is a factory rather than a plain type, skip
219 continue
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000220
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100221 class MyView():
222 def __init__(self, base):
223 self.m = memoryview(base)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000224 class MySource(tp):
225 pass
226 class MyObject:
227 pass
228
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100229 # Create a reference cycle through a memoryview object.
230 # This exercises mbuf_clear().
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000231 b = MySource(tp(b'abc'))
232 m = self._view(b)
233 o = MyObject()
234 b.m = m
235 b.o = o
236 wr = weakref.ref(o)
237 b = m = o = None
238 # The cycle must be broken
239 gc.collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000240 self.assertTrue(wr() is None, wr())
Antoine Pitrouc6b09eb2008-09-01 15:10:14 +0000241
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100242 # This exercises memory_clear().
243 m = MyView(tp(b'abc'))
244 o = MyObject()
245 m.x = m
246 m.o = o
247 wr = weakref.ref(o)
248 m = o = None
249 # The cycle must be broken
250 gc.collect()
251 self.assertTrue(wr() is None, wr())
252
Antoine Pitrou6e6cc832010-09-09 12:59:39 +0000253 def _check_released(self, m, tp):
Ezio Melottied3a7d22010-12-01 02:32:32 +0000254 check = self.assertRaisesRegex(ValueError, "released")
Antoine Pitrou6e6cc832010-09-09 12:59:39 +0000255 with check: bytes(m)
256 with check: m.tobytes()
257 with check: m.tolist()
258 with check: m[0]
259 with check: m[0] = b'x'
260 with check: len(m)
261 with check: m.format
262 with check: m.itemsize
263 with check: m.ndim
264 with check: m.readonly
265 with check: m.shape
266 with check: m.strides
267 with check:
268 with m:
269 pass
270 # str() and repr() still function
271 self.assertIn("released memory", str(m))
272 self.assertIn("released memory", repr(m))
273 self.assertEqual(m, m)
274 self.assertNotEqual(m, memoryview(tp(self._source)))
275 self.assertNotEqual(m, tp(self._source))
276
277 def test_contextmanager(self):
278 for tp in self._types:
279 b = tp(self._source)
280 m = self._view(b)
281 with m as cm:
282 self.assertIs(cm, m)
283 self._check_released(m, tp)
284 m = self._view(b)
285 # Can release explicitly inside the context manager
286 with m:
287 m.release()
288
289 def test_release(self):
290 for tp in self._types:
291 b = tp(self._source)
292 m = self._view(b)
293 m.release()
294 self._check_released(m, tp)
295 # Can be called a second time (it's a no-op)
296 m.release()
297 self._check_released(m, tp)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000298
Antoine Pitrouad62b032011-01-18 18:57:52 +0000299 def test_writable_readonly(self):
300 # Issue #10451: memoryview incorrectly exposes a readonly
301 # buffer as writable causing a segfault if using mmap
302 tp = self.ro_type
303 if tp is None:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600304 self.skipTest("no read-only type to test")
Antoine Pitrouad62b032011-01-18 18:57:52 +0000305 b = tp(self._source)
306 m = self._view(b)
307 i = io.BytesIO(b'ZZZZ')
308 self.assertRaises(TypeError, i.readinto, m)
309
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100310 def test_getbuf_fail(self):
311 self.assertRaises(TypeError, self._view, {})
312
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100313 def test_hash(self):
314 # Memoryviews of readonly (hashable) types are hashable, and they
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100315 # hash as hash(obj.tobytes()).
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100316 tp = self.ro_type
317 if tp is None:
318 self.skipTest("no read-only type to test")
319 b = tp(self._source)
320 m = self._view(b)
321 self.assertEqual(hash(m), hash(b"abcdef"))
322 # Releasing the memoryview keeps the stored hash value (as with weakrefs)
323 m.release()
324 self.assertEqual(hash(m), hash(b"abcdef"))
325 # Hashing a memoryview for the first time after it is released
326 # results in an error (as with weakrefs).
327 m = self._view(b)
328 m.release()
329 self.assertRaises(ValueError, hash, m)
330
331 def test_hash_writable(self):
332 # Memoryviews of writable types are unhashable
333 tp = self.rw_type
334 if tp is None:
335 self.skipTest("no writable type to test")
336 b = tp(self._source)
337 m = self._view(b)
338 self.assertRaises(ValueError, hash, m)
339
Richard Oudkerk3e0a1eb2012-05-28 21:35:09 +0100340 def test_weakref(self):
341 # Check memoryviews are weakrefable
342 for tp in self._types:
343 b = tp(self._source)
344 m = self._view(b)
345 L = []
346 def callback(wr, b=b):
347 L.append(b)
348 wr = weakref.ref(m, callback)
349 self.assertIs(wr(), m)
350 del m
351 test.support.gc_collect()
352 self.assertIs(wr(), None)
353 self.assertIs(L[0], b)
354
Nick Coghlana0f169c2013-10-02 22:06:54 +1000355 def test_reversed(self):
356 for tp in self._types:
357 b = tp(self._source)
358 m = self._view(b)
359 aslist = list(reversed(m.tolist()))
360 self.assertEqual(list(reversed(m)), aslist)
361 self.assertEqual(list(reversed(m)), list(m[::-1]))
362
Stefan Krahfa5d6a52015-01-29 14:27:23 +0100363 def test_issue22668(self):
Stefan Krah3c0cf052015-01-29 17:33:31 +0100364 a = array.array('H', [256, 256, 256, 256])
365 x = memoryview(a)
366 m = x.cast('B')
Stefan Krahfa5d6a52015-01-29 14:27:23 +0100367 b = m.cast('H')
368 c = b[0:2]
369 d = memoryview(b)
370
371 del b
Stefan Krahfc341bd2015-01-29 14:33:37 +0100372
Stefan Krahfa5d6a52015-01-29 14:27:23 +0100373 self.assertEqual(c[0], 256)
374 self.assertEqual(d[0], 256)
375 self.assertEqual(c.format, "H")
376 self.assertEqual(d.format, "H")
Stefan Krahfc341bd2015-01-29 14:33:37 +0100377
Stefan Krahfa5d6a52015-01-29 14:27:23 +0100378 _ = m.cast('I')
379 self.assertEqual(c[0], 256)
380 self.assertEqual(d[0], 256)
381 self.assertEqual(c.format, "H")
382 self.assertEqual(d.format, "H")
383
Nick Coghlana0f169c2013-10-02 22:06:54 +1000384
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000385# Variations on source objects for the buffer: bytes-like objects, then arrays
386# with itemsize > 1.
387# NOTE: support for multi-dimensional objects is unimplemented.
Antoine Pitrou616d2852008-08-19 22:09:34 +0000388
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000389class BaseBytesMemoryTests(AbstractMemoryTests):
390 ro_type = bytes
391 rw_type = bytearray
392 getitem_type = bytes
393 itemsize = 1
394 format = 'B'
395
396class BaseArrayMemoryTests(AbstractMemoryTests):
397 ro_type = None
398 rw_type = lambda self, b: array.array('i', list(b))
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000399 getitem_type = lambda self, b: array.array('i', list(b)).tobytes()
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000400 itemsize = array.array('i').itemsize
401 format = 'i'
402
Zachary Ware9fe6d862013-12-08 00:20:35 -0600403 @unittest.skip('XXX test should be adapted for non-byte buffers')
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000404 def test_getbuffer(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000405 pass
406
Zachary Ware9fe6d862013-12-08 00:20:35 -0600407 @unittest.skip('XXX NotImplementedError: tolist() only supports byte views')
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000408 def test_tolist(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000409 pass
410
411
412# Variations on indirection levels: memoryview, slice of memoryview,
413# slice of slice of memoryview.
414# This is important to test allocation subtleties.
415
416class BaseMemoryviewTests:
Antoine Pitrou616d2852008-08-19 22:09:34 +0000417 def _view(self, obj):
418 return memoryview(obj)
419
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000420 def _check_contents(self, tp, obj, contents):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000421 self.assertEqual(obj, tp(contents))
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000422
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000423class BaseMemorySliceTests:
424 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000425
426 def _view(self, obj):
427 m = memoryview(obj)
428 return m[1:7]
429
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000430 def _check_contents(self, tp, obj, contents):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000431 self.assertEqual(obj[1:7], tp(contents))
Antoine Pitrou616d2852008-08-19 22:09:34 +0000432
433 def test_refs(self):
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000434 for tp in self._types:
435 m = memoryview(tp(self._source))
436 oldrefcount = sys.getrefcount(m)
437 m[1:2]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000438 self.assertEqual(sys.getrefcount(m), oldrefcount)
Antoine Pitrou616d2852008-08-19 22:09:34 +0000439
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000440class BaseMemorySliceSliceTests:
441 source_bytes = b"XabcdefY"
Antoine Pitrou616d2852008-08-19 22:09:34 +0000442
443 def _view(self, obj):
444 m = memoryview(obj)
445 return m[:7][1:]
446
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000447 def _check_contents(self, tp, obj, contents):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000448 self.assertEqual(obj[1:7], tp(contents))
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000449
450
451# Concrete test classes
452
453class BytesMemoryviewTest(unittest.TestCase,
454 BaseMemoryviewTests, BaseBytesMemoryTests):
455
456 def test_constructor(self):
457 for tp in self._types:
458 ob = tp(self._source)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000459 self.assertTrue(memoryview(ob))
460 self.assertTrue(memoryview(object=ob))
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000461 self.assertRaises(TypeError, memoryview)
462 self.assertRaises(TypeError, memoryview, ob, ob)
463 self.assertRaises(TypeError, memoryview, argument=ob)
464 self.assertRaises(TypeError, memoryview, ob, argument=True)
465
466class ArrayMemoryviewTest(unittest.TestCase,
467 BaseMemoryviewTests, BaseArrayMemoryTests):
468
469 def test_array_assign(self):
470 # Issue #4569: segfault when mutating a memoryview with itemsize != 1
471 a = array.array('i', range(10))
472 m = memoryview(a)
473 new_a = array.array('i', range(9, -1, -1))
474 m[:] = new_a
Ezio Melottib3aedd42010-11-20 19:04:17 +0000475 self.assertEqual(a, new_a)
Antoine Pitrouc3b39242009-01-03 16:59:18 +0000476
477
478class BytesMemorySliceTest(unittest.TestCase,
479 BaseMemorySliceTests, BaseBytesMemoryTests):
480 pass
481
482class ArrayMemorySliceTest(unittest.TestCase,
483 BaseMemorySliceTests, BaseArrayMemoryTests):
484 pass
485
486class BytesMemorySliceSliceTest(unittest.TestCase,
487 BaseMemorySliceSliceTests, BaseBytesMemoryTests):
488 pass
489
490class ArrayMemorySliceSliceTest(unittest.TestCase,
491 BaseMemorySliceSliceTests, BaseArrayMemoryTests):
492 pass
Antoine Pitrou616d2852008-08-19 22:09:34 +0000493
494
Christian Heimes7b6fc8e2007-11-08 02:28:11 +0000495if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500496 unittest.main()