Issue #7142: Fix uses of unicode in memoryview objects
[python.git] / Lib / test / test_memoryview.py
blob0caf5ec90521c9feea76d153be92a8f6ecf67fc1
1 """Unit tests for the memoryview
3 XXX We need more tests! Some tests are in test_bytes
4 """
6 import unittest
7 import sys
8 import gc
9 import weakref
10 import array
11 from test import test_support
14 class AbstractMemoryTests:
15 source_bytes = b"abcdef"
17 @property
18 def _source(self):
19 return self.source_bytes
21 @property
22 def _types(self):
23 return filter(None, [self.ro_type, self.rw_type])
25 def check_getitem_with_type(self, tp):
26 item = self.getitem_type
27 b = tp(self._source)
28 oldrefcount = sys.getrefcount(b)
29 m = self._view(b)
30 self.assertEquals(m[0], item(b"a"))
31 self.assertTrue(isinstance(m[0], bytes), type(m[0]))
32 self.assertEquals(m[5], item(b"f"))
33 self.assertEquals(m[-1], item(b"f"))
34 self.assertEquals(m[-6], item(b"a"))
35 # 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)
47 def test_getitem(self):
48 for tp in self._types:
49 self.check_getitem_with_type(tp)
51 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))])
57 def test_repr(self):
58 for tp in self._types:
59 b = tp(self._source)
60 m = self._view(b)
61 self.assertIsInstance(m.__repr__(), str)
63 def test_setitem_readonly(self):
64 if not self.ro_type:
65 return
66 b = self.ro_type(self._source)
67 oldrefcount = sys.getrefcount(b)
68 m = self._view(b)
69 def setitem(value):
70 m[0] = value
71 self.assertRaises(TypeError, setitem, b"a")
72 self.assertRaises(TypeError, setitem, 65)
73 self.assertRaises(TypeError, setitem, memoryview(b"a"))
74 m = None
75 self.assertEquals(sys.getrefcount(b), oldrefcount)
77 def test_setitem_writable(self):
78 if not self.rw_type:
79 return
80 tp = self.rw_type
81 b = self.rw_type(self._source)
82 oldrefcount = sys.getrefcount(b)
83 m = self._view(b)
84 m[0] = tp(b"0")
85 self._check_contents(tp, b, b"0bcdef")
86 m[1:3] = tp(b"12")
87 self._check_contents(tp, b, b"012def")
88 m[1:1] = tp(b"")
89 self._check_contents(tp, b, b"012def")
90 m[:] = tp(b"abcdef")
91 self._check_contents(tp, b, b"abcdef")
93 # Overlapping copies of a view into itself
94 m[0:3] = m[2:5]
95 self._check_contents(tp, b, b"cdedef")
96 m[:] = tp(b"abcdef")
97 m[2:5] = m[0:3]
98 self._check_contents(tp, b, b"ababcf")
100 def setitem(key, value):
101 m[key] = tp(value)
102 # Bounds checking
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")
117 m = None
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))
123 b = m.tobytes()
124 # This calls self.getitem_type() on each separate byte of b"abcdef"
125 expected = b"".join(
126 self.getitem_type(c) for c in b"abcdef")
127 self.assertEquals(b, expected)
128 self.assertTrue(isinstance(b, bytes), type(b))
130 def test_tolist(self):
131 for tp in self._types:
132 m = self._view(tp(self._source))
133 l = m.tolist()
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)
172 return m
174 def test_attributes_readonly(self):
175 if not self.ro_type:
176 return
177 m = self.check_attributes_with_type(self.ro_type)
178 self.assertEquals(m.readonly, True)
180 def test_attributes_writable(self):
181 if not self.rw_type:
182 return
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)
193 #m = self._view(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)
198 #m = None
199 #self.assertEquals(sys.getrefcount(b), oldrefcount)
201 def test_gc(self):
202 for tp in self._types:
203 if not isinstance(tp, type):
204 # If tp is a factory rather than a plain type, skip
205 continue
207 class MySource(tp):
208 pass
209 class MyObject:
210 pass
212 # Create a reference cycle through a memoryview object
213 b = MySource(tp(b'abc'))
214 m = self._view(b)
215 o = MyObject()
216 b.m = m
217 b.o = o
218 wr = weakref.ref(o)
219 b = m = o = None
220 # The cycle must be broken
221 gc.collect()
222 self.assertTrue(wr() is None, wr())
225 # Variations on source objects for the buffer: bytes-like objects, then arrays
226 # with itemsize > 1.
227 # NOTE: support for multi-dimensional objects is unimplemented.
229 class BaseBytesMemoryTests(AbstractMemoryTests):
230 ro_type = bytes
231 rw_type = bytearray
232 getitem_type = bytes
233 itemsize = 1
234 format = 'B'
236 # Disabled: array.array() does not support the new buffer API in 2.x
238 #class BaseArrayMemoryTests(AbstractMemoryTests):
239 #ro_type = None
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
243 #format = 'i'
245 #def test_getbuffer(self):
246 ## XXX Test should be adapted for non-byte buffers
247 #pass
249 #def test_tolist(self):
250 ## XXX NotImplementedError: tolist() only supports byte views
251 #pass
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):
269 m = memoryview(obj)
270 return m[1:7]
272 def _check_contents(self, tp, obj, contents):
273 self.assertEquals(obj[1:7], tp(contents))
275 def test_refs(self):
276 for tp in self._types:
277 m = memoryview(tp(self._source))
278 oldrefcount = sys.getrefcount(m)
279 m[1:2]
280 self.assertEquals(sys.getrefcount(m), oldrefcount)
282 class BaseMemorySliceSliceTests:
283 source_bytes = b"XabcdefY"
285 def _view(self, obj):
286 m = memoryview(obj)
287 return m[:7][1:]
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))
314 #m = memoryview(a)
315 #new_a = array.array('i', range(9, -1, -1))
316 #m[:] = new_a
317 #self.assertEquals(a, new_a)
320 class BytesMemorySliceTest(unittest.TestCase,
321 BaseMemorySliceTests, BaseBytesMemoryTests):
322 pass
324 #class ArrayMemorySliceTest(unittest.TestCase,
325 #BaseMemorySliceTests, BaseArrayMemoryTests):
326 #pass
328 class BytesMemorySliceSliceTest(unittest.TestCase,
329 BaseMemorySliceSliceTests, BaseBytesMemoryTests):
330 pass
332 #class ArrayMemorySliceSliceTest(unittest.TestCase,
333 #BaseMemorySliceSliceTests, BaseArrayMemoryTests):
334 #pass
337 def test_main():
338 test_support.run_unittest(__name__)
340 if __name__ == "__main__":
341 test_main()