1 | n/a | """Unit tests for the memoryview |
---|
2 | n/a | |
---|
3 | n/a | Some tests are in test_bytes. Many tests that require _testbuffer.ndarray |
---|
4 | n/a | are in test_buffer. |
---|
5 | n/a | """ |
---|
6 | n/a | |
---|
7 | n/a | import unittest |
---|
8 | n/a | import test.support |
---|
9 | n/a | import sys |
---|
10 | n/a | import gc |
---|
11 | n/a | import weakref |
---|
12 | n/a | import array |
---|
13 | n/a | import io |
---|
14 | n/a | import copy |
---|
15 | n/a | import pickle |
---|
16 | n/a | |
---|
17 | n/a | |
---|
18 | n/a | class AbstractMemoryTests: |
---|
19 | n/a | source_bytes = b"abcdef" |
---|
20 | n/a | |
---|
21 | n/a | @property |
---|
22 | n/a | def _source(self): |
---|
23 | n/a | return self.source_bytes |
---|
24 | n/a | |
---|
25 | n/a | @property |
---|
26 | n/a | def _types(self): |
---|
27 | n/a | return filter(None, [self.ro_type, self.rw_type]) |
---|
28 | n/a | |
---|
29 | n/a | def check_getitem_with_type(self, tp): |
---|
30 | n/a | b = tp(self._source) |
---|
31 | n/a | oldrefcount = sys.getrefcount(b) |
---|
32 | n/a | m = self._view(b) |
---|
33 | n/a | self.assertEqual(m[0], ord(b"a")) |
---|
34 | n/a | self.assertIsInstance(m[0], int) |
---|
35 | n/a | self.assertEqual(m[5], ord(b"f")) |
---|
36 | n/a | self.assertEqual(m[-1], ord(b"f")) |
---|
37 | n/a | self.assertEqual(m[-6], ord(b"a")) |
---|
38 | n/a | # Bounds checking |
---|
39 | n/a | self.assertRaises(IndexError, lambda: m[6]) |
---|
40 | n/a | self.assertRaises(IndexError, lambda: m[-7]) |
---|
41 | n/a | self.assertRaises(IndexError, lambda: m[sys.maxsize]) |
---|
42 | n/a | self.assertRaises(IndexError, lambda: m[-sys.maxsize]) |
---|
43 | n/a | # Type checking |
---|
44 | n/a | self.assertRaises(TypeError, lambda: m[None]) |
---|
45 | n/a | self.assertRaises(TypeError, lambda: m[0.0]) |
---|
46 | n/a | self.assertRaises(TypeError, lambda: m["a"]) |
---|
47 | n/a | m = None |
---|
48 | n/a | self.assertEqual(sys.getrefcount(b), oldrefcount) |
---|
49 | n/a | |
---|
50 | n/a | def test_getitem(self): |
---|
51 | n/a | for tp in self._types: |
---|
52 | n/a | self.check_getitem_with_type(tp) |
---|
53 | n/a | |
---|
54 | n/a | def test_iter(self): |
---|
55 | n/a | for tp in self._types: |
---|
56 | n/a | b = tp(self._source) |
---|
57 | n/a | m = self._view(b) |
---|
58 | n/a | self.assertEqual(list(m), [m[i] for i in range(len(m))]) |
---|
59 | n/a | |
---|
60 | n/a | def test_setitem_readonly(self): |
---|
61 | n/a | if not self.ro_type: |
---|
62 | n/a | self.skipTest("no read-only type to test") |
---|
63 | n/a | b = self.ro_type(self._source) |
---|
64 | n/a | oldrefcount = sys.getrefcount(b) |
---|
65 | n/a | m = self._view(b) |
---|
66 | n/a | def setitem(value): |
---|
67 | n/a | m[0] = value |
---|
68 | n/a | self.assertRaises(TypeError, setitem, b"a") |
---|
69 | n/a | self.assertRaises(TypeError, setitem, 65) |
---|
70 | n/a | self.assertRaises(TypeError, setitem, memoryview(b"a")) |
---|
71 | n/a | m = None |
---|
72 | n/a | self.assertEqual(sys.getrefcount(b), oldrefcount) |
---|
73 | n/a | |
---|
74 | n/a | def test_setitem_writable(self): |
---|
75 | n/a | if not self.rw_type: |
---|
76 | n/a | self.skipTest("no writable type to test") |
---|
77 | n/a | tp = self.rw_type |
---|
78 | n/a | b = self.rw_type(self._source) |
---|
79 | n/a | oldrefcount = sys.getrefcount(b) |
---|
80 | n/a | m = self._view(b) |
---|
81 | n/a | m[0] = ord(b'1') |
---|
82 | n/a | self._check_contents(tp, b, b"1bcdef") |
---|
83 | n/a | m[0:1] = tp(b"0") |
---|
84 | n/a | self._check_contents(tp, b, b"0bcdef") |
---|
85 | n/a | m[1:3] = tp(b"12") |
---|
86 | n/a | self._check_contents(tp, b, b"012def") |
---|
87 | n/a | m[1:1] = tp(b"") |
---|
88 | n/a | self._check_contents(tp, b, b"012def") |
---|
89 | n/a | m[:] = tp(b"abcdef") |
---|
90 | n/a | self._check_contents(tp, b, b"abcdef") |
---|
91 | n/a | |
---|
92 | n/a | # Overlapping copies of a view into itself |
---|
93 | n/a | m[0:3] = m[2:5] |
---|
94 | n/a | self._check_contents(tp, b, b"cdedef") |
---|
95 | n/a | m[:] = tp(b"abcdef") |
---|
96 | n/a | m[2:5] = m[0:3] |
---|
97 | n/a | self._check_contents(tp, b, b"ababcf") |
---|
98 | n/a | |
---|
99 | n/a | def setitem(key, value): |
---|
100 | n/a | m[key] = tp(value) |
---|
101 | n/a | # Bounds checking |
---|
102 | n/a | self.assertRaises(IndexError, setitem, 6, b"a") |
---|
103 | n/a | self.assertRaises(IndexError, setitem, -7, b"a") |
---|
104 | n/a | self.assertRaises(IndexError, setitem, sys.maxsize, b"a") |
---|
105 | n/a | self.assertRaises(IndexError, setitem, -sys.maxsize, b"a") |
---|
106 | n/a | # Wrong index/slice types |
---|
107 | n/a | self.assertRaises(TypeError, setitem, 0.0, b"a") |
---|
108 | n/a | self.assertRaises(TypeError, setitem, (0,), b"a") |
---|
109 | n/a | self.assertRaises(TypeError, setitem, (slice(0,1,1), 0), b"a") |
---|
110 | n/a | self.assertRaises(TypeError, setitem, (0, slice(0,1,1)), b"a") |
---|
111 | n/a | self.assertRaises(TypeError, setitem, (0,), b"a") |
---|
112 | n/a | self.assertRaises(TypeError, setitem, "a", b"a") |
---|
113 | n/a | # Not implemented: multidimensional slices |
---|
114 | n/a | slices = (slice(0,1,1), slice(0,1,2)) |
---|
115 | n/a | self.assertRaises(NotImplementedError, setitem, slices, b"a") |
---|
116 | n/a | # Trying to resize the memory object |
---|
117 | n/a | exc = ValueError if m.format == 'c' else TypeError |
---|
118 | n/a | self.assertRaises(exc, setitem, 0, b"") |
---|
119 | n/a | self.assertRaises(exc, setitem, 0, b"ab") |
---|
120 | n/a | self.assertRaises(ValueError, setitem, slice(1,1), b"a") |
---|
121 | n/a | self.assertRaises(ValueError, setitem, slice(0,2), b"a") |
---|
122 | n/a | |
---|
123 | n/a | m = None |
---|
124 | n/a | self.assertEqual(sys.getrefcount(b), oldrefcount) |
---|
125 | n/a | |
---|
126 | n/a | def test_delitem(self): |
---|
127 | n/a | for tp in self._types: |
---|
128 | n/a | b = tp(self._source) |
---|
129 | n/a | m = self._view(b) |
---|
130 | n/a | with self.assertRaises(TypeError): |
---|
131 | n/a | del m[1] |
---|
132 | n/a | with self.assertRaises(TypeError): |
---|
133 | n/a | del m[1:4] |
---|
134 | n/a | |
---|
135 | n/a | def test_tobytes(self): |
---|
136 | n/a | for tp in self._types: |
---|
137 | n/a | m = self._view(tp(self._source)) |
---|
138 | n/a | b = m.tobytes() |
---|
139 | n/a | # This calls self.getitem_type() on each separate byte of b"abcdef" |
---|
140 | n/a | expected = b"".join( |
---|
141 | n/a | self.getitem_type(bytes([c])) for c in b"abcdef") |
---|
142 | n/a | self.assertEqual(b, expected) |
---|
143 | n/a | self.assertIsInstance(b, bytes) |
---|
144 | n/a | |
---|
145 | n/a | def test_tolist(self): |
---|
146 | n/a | for tp in self._types: |
---|
147 | n/a | m = self._view(tp(self._source)) |
---|
148 | n/a | l = m.tolist() |
---|
149 | n/a | self.assertEqual(l, list(b"abcdef")) |
---|
150 | n/a | |
---|
151 | n/a | def test_compare(self): |
---|
152 | n/a | # memoryviews can compare for equality with other objects |
---|
153 | n/a | # having the buffer interface. |
---|
154 | n/a | for tp in self._types: |
---|
155 | n/a | m = self._view(tp(self._source)) |
---|
156 | n/a | for tp_comp in self._types: |
---|
157 | n/a | self.assertTrue(m == tp_comp(b"abcdef")) |
---|
158 | n/a | self.assertFalse(m != tp_comp(b"abcdef")) |
---|
159 | n/a | self.assertFalse(m == tp_comp(b"abcde")) |
---|
160 | n/a | self.assertTrue(m != tp_comp(b"abcde")) |
---|
161 | n/a | self.assertFalse(m == tp_comp(b"abcde1")) |
---|
162 | n/a | self.assertTrue(m != tp_comp(b"abcde1")) |
---|
163 | n/a | self.assertTrue(m == m) |
---|
164 | n/a | self.assertTrue(m == m[:]) |
---|
165 | n/a | self.assertTrue(m[0:6] == m[:]) |
---|
166 | n/a | self.assertFalse(m[0:5] == m) |
---|
167 | n/a | |
---|
168 | n/a | # Comparison with objects which don't support the buffer API |
---|
169 | n/a | self.assertFalse(m == "abcdef") |
---|
170 | n/a | self.assertTrue(m != "abcdef") |
---|
171 | n/a | self.assertFalse("abcdef" == m) |
---|
172 | n/a | self.assertTrue("abcdef" != m) |
---|
173 | n/a | |
---|
174 | n/a | # Unordered comparisons |
---|
175 | n/a | for c in (m, b"abcdef"): |
---|
176 | n/a | self.assertRaises(TypeError, lambda: m < c) |
---|
177 | n/a | self.assertRaises(TypeError, lambda: c <= m) |
---|
178 | n/a | self.assertRaises(TypeError, lambda: m >= c) |
---|
179 | n/a | self.assertRaises(TypeError, lambda: c > m) |
---|
180 | n/a | |
---|
181 | n/a | def check_attributes_with_type(self, tp): |
---|
182 | n/a | m = self._view(tp(self._source)) |
---|
183 | n/a | self.assertEqual(m.format, self.format) |
---|
184 | n/a | self.assertEqual(m.itemsize, self.itemsize) |
---|
185 | n/a | self.assertEqual(m.ndim, 1) |
---|
186 | n/a | self.assertEqual(m.shape, (6,)) |
---|
187 | n/a | self.assertEqual(len(m), 6) |
---|
188 | n/a | self.assertEqual(m.strides, (self.itemsize,)) |
---|
189 | n/a | self.assertEqual(m.suboffsets, ()) |
---|
190 | n/a | return m |
---|
191 | n/a | |
---|
192 | n/a | def test_attributes_readonly(self): |
---|
193 | n/a | if not self.ro_type: |
---|
194 | n/a | self.skipTest("no read-only type to test") |
---|
195 | n/a | m = self.check_attributes_with_type(self.ro_type) |
---|
196 | n/a | self.assertEqual(m.readonly, True) |
---|
197 | n/a | |
---|
198 | n/a | def test_attributes_writable(self): |
---|
199 | n/a | if not self.rw_type: |
---|
200 | n/a | self.skipTest("no writable type to test") |
---|
201 | n/a | m = self.check_attributes_with_type(self.rw_type) |
---|
202 | n/a | self.assertEqual(m.readonly, False) |
---|
203 | n/a | |
---|
204 | n/a | def test_getbuffer(self): |
---|
205 | n/a | # Test PyObject_GetBuffer() on a memoryview object. |
---|
206 | n/a | for tp in self._types: |
---|
207 | n/a | b = tp(self._source) |
---|
208 | n/a | oldrefcount = sys.getrefcount(b) |
---|
209 | n/a | m = self._view(b) |
---|
210 | n/a | oldviewrefcount = sys.getrefcount(m) |
---|
211 | n/a | s = str(m, "utf-8") |
---|
212 | n/a | self._check_contents(tp, b, s.encode("utf-8")) |
---|
213 | n/a | self.assertEqual(sys.getrefcount(m), oldviewrefcount) |
---|
214 | n/a | m = None |
---|
215 | n/a | self.assertEqual(sys.getrefcount(b), oldrefcount) |
---|
216 | n/a | |
---|
217 | n/a | def test_gc(self): |
---|
218 | n/a | for tp in self._types: |
---|
219 | n/a | if not isinstance(tp, type): |
---|
220 | n/a | # If tp is a factory rather than a plain type, skip |
---|
221 | n/a | continue |
---|
222 | n/a | |
---|
223 | n/a | class MyView(): |
---|
224 | n/a | def __init__(self, base): |
---|
225 | n/a | self.m = memoryview(base) |
---|
226 | n/a | class MySource(tp): |
---|
227 | n/a | pass |
---|
228 | n/a | class MyObject: |
---|
229 | n/a | pass |
---|
230 | n/a | |
---|
231 | n/a | # Create a reference cycle through a memoryview object. |
---|
232 | n/a | # This exercises mbuf_clear(). |
---|
233 | n/a | b = MySource(tp(b'abc')) |
---|
234 | n/a | m = self._view(b) |
---|
235 | n/a | o = MyObject() |
---|
236 | n/a | b.m = m |
---|
237 | n/a | b.o = o |
---|
238 | n/a | wr = weakref.ref(o) |
---|
239 | n/a | b = m = o = None |
---|
240 | n/a | # The cycle must be broken |
---|
241 | n/a | gc.collect() |
---|
242 | n/a | self.assertTrue(wr() is None, wr()) |
---|
243 | n/a | |
---|
244 | n/a | # This exercises memory_clear(). |
---|
245 | n/a | m = MyView(tp(b'abc')) |
---|
246 | n/a | o = MyObject() |
---|
247 | n/a | m.x = m |
---|
248 | n/a | m.o = o |
---|
249 | n/a | wr = weakref.ref(o) |
---|
250 | n/a | m = o = None |
---|
251 | n/a | # The cycle must be broken |
---|
252 | n/a | gc.collect() |
---|
253 | n/a | self.assertTrue(wr() is None, wr()) |
---|
254 | n/a | |
---|
255 | n/a | def _check_released(self, m, tp): |
---|
256 | n/a | check = self.assertRaisesRegex(ValueError, "released") |
---|
257 | n/a | with check: bytes(m) |
---|
258 | n/a | with check: m.tobytes() |
---|
259 | n/a | with check: m.tolist() |
---|
260 | n/a | with check: m[0] |
---|
261 | n/a | with check: m[0] = b'x' |
---|
262 | n/a | with check: len(m) |
---|
263 | n/a | with check: m.format |
---|
264 | n/a | with check: m.itemsize |
---|
265 | n/a | with check: m.ndim |
---|
266 | n/a | with check: m.readonly |
---|
267 | n/a | with check: m.shape |
---|
268 | n/a | with check: m.strides |
---|
269 | n/a | with check: |
---|
270 | n/a | with m: |
---|
271 | n/a | pass |
---|
272 | n/a | # str() and repr() still function |
---|
273 | n/a | self.assertIn("released memory", str(m)) |
---|
274 | n/a | self.assertIn("released memory", repr(m)) |
---|
275 | n/a | self.assertEqual(m, m) |
---|
276 | n/a | self.assertNotEqual(m, memoryview(tp(self._source))) |
---|
277 | n/a | self.assertNotEqual(m, tp(self._source)) |
---|
278 | n/a | |
---|
279 | n/a | def test_contextmanager(self): |
---|
280 | n/a | for tp in self._types: |
---|
281 | n/a | b = tp(self._source) |
---|
282 | n/a | m = self._view(b) |
---|
283 | n/a | with m as cm: |
---|
284 | n/a | self.assertIs(cm, m) |
---|
285 | n/a | self._check_released(m, tp) |
---|
286 | n/a | m = self._view(b) |
---|
287 | n/a | # Can release explicitly inside the context manager |
---|
288 | n/a | with m: |
---|
289 | n/a | m.release() |
---|
290 | n/a | |
---|
291 | n/a | def test_release(self): |
---|
292 | n/a | for tp in self._types: |
---|
293 | n/a | b = tp(self._source) |
---|
294 | n/a | m = self._view(b) |
---|
295 | n/a | m.release() |
---|
296 | n/a | self._check_released(m, tp) |
---|
297 | n/a | # Can be called a second time (it's a no-op) |
---|
298 | n/a | m.release() |
---|
299 | n/a | self._check_released(m, tp) |
---|
300 | n/a | |
---|
301 | n/a | def test_writable_readonly(self): |
---|
302 | n/a | # Issue #10451: memoryview incorrectly exposes a readonly |
---|
303 | n/a | # buffer as writable causing a segfault if using mmap |
---|
304 | n/a | tp = self.ro_type |
---|
305 | n/a | if tp is None: |
---|
306 | n/a | self.skipTest("no read-only type to test") |
---|
307 | n/a | b = tp(self._source) |
---|
308 | n/a | m = self._view(b) |
---|
309 | n/a | i = io.BytesIO(b'ZZZZ') |
---|
310 | n/a | self.assertRaises(TypeError, i.readinto, m) |
---|
311 | n/a | |
---|
312 | n/a | def test_getbuf_fail(self): |
---|
313 | n/a | self.assertRaises(TypeError, self._view, {}) |
---|
314 | n/a | |
---|
315 | n/a | def test_hash(self): |
---|
316 | n/a | # Memoryviews of readonly (hashable) types are hashable, and they |
---|
317 | n/a | # hash as hash(obj.tobytes()). |
---|
318 | n/a | tp = self.ro_type |
---|
319 | n/a | if tp is None: |
---|
320 | n/a | self.skipTest("no read-only type to test") |
---|
321 | n/a | b = tp(self._source) |
---|
322 | n/a | m = self._view(b) |
---|
323 | n/a | self.assertEqual(hash(m), hash(b"abcdef")) |
---|
324 | n/a | # Releasing the memoryview keeps the stored hash value (as with weakrefs) |
---|
325 | n/a | m.release() |
---|
326 | n/a | self.assertEqual(hash(m), hash(b"abcdef")) |
---|
327 | n/a | # Hashing a memoryview for the first time after it is released |
---|
328 | n/a | # results in an error (as with weakrefs). |
---|
329 | n/a | m = self._view(b) |
---|
330 | n/a | m.release() |
---|
331 | n/a | self.assertRaises(ValueError, hash, m) |
---|
332 | n/a | |
---|
333 | n/a | def test_hash_writable(self): |
---|
334 | n/a | # Memoryviews of writable types are unhashable |
---|
335 | n/a | tp = self.rw_type |
---|
336 | n/a | if tp is None: |
---|
337 | n/a | self.skipTest("no writable type to test") |
---|
338 | n/a | b = tp(self._source) |
---|
339 | n/a | m = self._view(b) |
---|
340 | n/a | self.assertRaises(ValueError, hash, m) |
---|
341 | n/a | |
---|
342 | n/a | def test_weakref(self): |
---|
343 | n/a | # Check memoryviews are weakrefable |
---|
344 | n/a | for tp in self._types: |
---|
345 | n/a | b = tp(self._source) |
---|
346 | n/a | m = self._view(b) |
---|
347 | n/a | L = [] |
---|
348 | n/a | def callback(wr, b=b): |
---|
349 | n/a | L.append(b) |
---|
350 | n/a | wr = weakref.ref(m, callback) |
---|
351 | n/a | self.assertIs(wr(), m) |
---|
352 | n/a | del m |
---|
353 | n/a | test.support.gc_collect() |
---|
354 | n/a | self.assertIs(wr(), None) |
---|
355 | n/a | self.assertIs(L[0], b) |
---|
356 | n/a | |
---|
357 | n/a | def test_reversed(self): |
---|
358 | n/a | for tp in self._types: |
---|
359 | n/a | b = tp(self._source) |
---|
360 | n/a | m = self._view(b) |
---|
361 | n/a | aslist = list(reversed(m.tolist())) |
---|
362 | n/a | self.assertEqual(list(reversed(m)), aslist) |
---|
363 | n/a | self.assertEqual(list(reversed(m)), list(m[::-1])) |
---|
364 | n/a | |
---|
365 | n/a | def test_issue22668(self): |
---|
366 | n/a | a = array.array('H', [256, 256, 256, 256]) |
---|
367 | n/a | x = memoryview(a) |
---|
368 | n/a | m = x.cast('B') |
---|
369 | n/a | b = m.cast('H') |
---|
370 | n/a | c = b[0:2] |
---|
371 | n/a | d = memoryview(b) |
---|
372 | n/a | |
---|
373 | n/a | del b |
---|
374 | n/a | |
---|
375 | n/a | self.assertEqual(c[0], 256) |
---|
376 | n/a | self.assertEqual(d[0], 256) |
---|
377 | n/a | self.assertEqual(c.format, "H") |
---|
378 | n/a | self.assertEqual(d.format, "H") |
---|
379 | n/a | |
---|
380 | n/a | _ = m.cast('I') |
---|
381 | n/a | self.assertEqual(c[0], 256) |
---|
382 | n/a | self.assertEqual(d[0], 256) |
---|
383 | n/a | self.assertEqual(c.format, "H") |
---|
384 | n/a | self.assertEqual(d.format, "H") |
---|
385 | n/a | |
---|
386 | n/a | |
---|
387 | n/a | # Variations on source objects for the buffer: bytes-like objects, then arrays |
---|
388 | n/a | # with itemsize > 1. |
---|
389 | n/a | # NOTE: support for multi-dimensional objects is unimplemented. |
---|
390 | n/a | |
---|
391 | n/a | class BaseBytesMemoryTests(AbstractMemoryTests): |
---|
392 | n/a | ro_type = bytes |
---|
393 | n/a | rw_type = bytearray |
---|
394 | n/a | getitem_type = bytes |
---|
395 | n/a | itemsize = 1 |
---|
396 | n/a | format = 'B' |
---|
397 | n/a | |
---|
398 | n/a | class BaseArrayMemoryTests(AbstractMemoryTests): |
---|
399 | n/a | ro_type = None |
---|
400 | n/a | rw_type = lambda self, b: array.array('i', list(b)) |
---|
401 | n/a | getitem_type = lambda self, b: array.array('i', list(b)).tobytes() |
---|
402 | n/a | itemsize = array.array('i').itemsize |
---|
403 | n/a | format = 'i' |
---|
404 | n/a | |
---|
405 | n/a | @unittest.skip('XXX test should be adapted for non-byte buffers') |
---|
406 | n/a | def test_getbuffer(self): |
---|
407 | n/a | pass |
---|
408 | n/a | |
---|
409 | n/a | @unittest.skip('XXX NotImplementedError: tolist() only supports byte views') |
---|
410 | n/a | def test_tolist(self): |
---|
411 | n/a | pass |
---|
412 | n/a | |
---|
413 | n/a | |
---|
414 | n/a | # Variations on indirection levels: memoryview, slice of memoryview, |
---|
415 | n/a | # slice of slice of memoryview. |
---|
416 | n/a | # This is important to test allocation subtleties. |
---|
417 | n/a | |
---|
418 | n/a | class BaseMemoryviewTests: |
---|
419 | n/a | def _view(self, obj): |
---|
420 | n/a | return memoryview(obj) |
---|
421 | n/a | |
---|
422 | n/a | def _check_contents(self, tp, obj, contents): |
---|
423 | n/a | self.assertEqual(obj, tp(contents)) |
---|
424 | n/a | |
---|
425 | n/a | class BaseMemorySliceTests: |
---|
426 | n/a | source_bytes = b"XabcdefY" |
---|
427 | n/a | |
---|
428 | n/a | def _view(self, obj): |
---|
429 | n/a | m = memoryview(obj) |
---|
430 | n/a | return m[1:7] |
---|
431 | n/a | |
---|
432 | n/a | def _check_contents(self, tp, obj, contents): |
---|
433 | n/a | self.assertEqual(obj[1:7], tp(contents)) |
---|
434 | n/a | |
---|
435 | n/a | def test_refs(self): |
---|
436 | n/a | for tp in self._types: |
---|
437 | n/a | m = memoryview(tp(self._source)) |
---|
438 | n/a | oldrefcount = sys.getrefcount(m) |
---|
439 | n/a | m[1:2] |
---|
440 | n/a | self.assertEqual(sys.getrefcount(m), oldrefcount) |
---|
441 | n/a | |
---|
442 | n/a | class BaseMemorySliceSliceTests: |
---|
443 | n/a | source_bytes = b"XabcdefY" |
---|
444 | n/a | |
---|
445 | n/a | def _view(self, obj): |
---|
446 | n/a | m = memoryview(obj) |
---|
447 | n/a | return m[:7][1:] |
---|
448 | n/a | |
---|
449 | n/a | def _check_contents(self, tp, obj, contents): |
---|
450 | n/a | self.assertEqual(obj[1:7], tp(contents)) |
---|
451 | n/a | |
---|
452 | n/a | |
---|
453 | n/a | # Concrete test classes |
---|
454 | n/a | |
---|
455 | n/a | class BytesMemoryviewTest(unittest.TestCase, |
---|
456 | n/a | BaseMemoryviewTests, BaseBytesMemoryTests): |
---|
457 | n/a | |
---|
458 | n/a | def test_constructor(self): |
---|
459 | n/a | for tp in self._types: |
---|
460 | n/a | ob = tp(self._source) |
---|
461 | n/a | self.assertTrue(memoryview(ob)) |
---|
462 | n/a | self.assertTrue(memoryview(object=ob)) |
---|
463 | n/a | self.assertRaises(TypeError, memoryview) |
---|
464 | n/a | self.assertRaises(TypeError, memoryview, ob, ob) |
---|
465 | n/a | self.assertRaises(TypeError, memoryview, argument=ob) |
---|
466 | n/a | self.assertRaises(TypeError, memoryview, ob, argument=True) |
---|
467 | n/a | |
---|
468 | n/a | class ArrayMemoryviewTest(unittest.TestCase, |
---|
469 | n/a | BaseMemoryviewTests, BaseArrayMemoryTests): |
---|
470 | n/a | |
---|
471 | n/a | def test_array_assign(self): |
---|
472 | n/a | # Issue #4569: segfault when mutating a memoryview with itemsize != 1 |
---|
473 | n/a | a = array.array('i', range(10)) |
---|
474 | n/a | m = memoryview(a) |
---|
475 | n/a | new_a = array.array('i', range(9, -1, -1)) |
---|
476 | n/a | m[:] = new_a |
---|
477 | n/a | self.assertEqual(a, new_a) |
---|
478 | n/a | |
---|
479 | n/a | |
---|
480 | n/a | class BytesMemorySliceTest(unittest.TestCase, |
---|
481 | n/a | BaseMemorySliceTests, BaseBytesMemoryTests): |
---|
482 | n/a | pass |
---|
483 | n/a | |
---|
484 | n/a | class ArrayMemorySliceTest(unittest.TestCase, |
---|
485 | n/a | BaseMemorySliceTests, BaseArrayMemoryTests): |
---|
486 | n/a | pass |
---|
487 | n/a | |
---|
488 | n/a | class BytesMemorySliceSliceTest(unittest.TestCase, |
---|
489 | n/a | BaseMemorySliceSliceTests, BaseBytesMemoryTests): |
---|
490 | n/a | pass |
---|
491 | n/a | |
---|
492 | n/a | class ArrayMemorySliceSliceTest(unittest.TestCase, |
---|
493 | n/a | BaseMemorySliceSliceTests, BaseArrayMemoryTests): |
---|
494 | n/a | pass |
---|
495 | n/a | |
---|
496 | n/a | |
---|
497 | n/a | class OtherTest(unittest.TestCase): |
---|
498 | n/a | def test_ctypes_cast(self): |
---|
499 | n/a | # Issue 15944: Allow all source formats when casting to bytes. |
---|
500 | n/a | ctypes = test.support.import_module("ctypes") |
---|
501 | n/a | p6 = bytes(ctypes.c_double(0.6)) |
---|
502 | n/a | |
---|
503 | n/a | d = ctypes.c_double() |
---|
504 | n/a | m = memoryview(d).cast("B") |
---|
505 | n/a | m[:2] = p6[:2] |
---|
506 | n/a | m[2:] = p6[2:] |
---|
507 | n/a | self.assertEqual(d.value, 0.6) |
---|
508 | n/a | |
---|
509 | n/a | for format in "Bbc": |
---|
510 | n/a | with self.subTest(format): |
---|
511 | n/a | d = ctypes.c_double() |
---|
512 | n/a | m = memoryview(d).cast(format) |
---|
513 | n/a | m[:2] = memoryview(p6).cast(format)[:2] |
---|
514 | n/a | m[2:] = memoryview(p6).cast(format)[2:] |
---|
515 | n/a | self.assertEqual(d.value, 0.6) |
---|
516 | n/a | |
---|
517 | n/a | def test_memoryview_hex(self): |
---|
518 | n/a | # Issue #9951: memoryview.hex() segfaults with non-contiguous buffers. |
---|
519 | n/a | x = b'0' * 200000 |
---|
520 | n/a | m1 = memoryview(x) |
---|
521 | n/a | m2 = m1[::-1] |
---|
522 | n/a | self.assertEqual(m2.hex(), '30' * 200000) |
---|
523 | n/a | |
---|
524 | n/a | def test_copy(self): |
---|
525 | n/a | m = memoryview(b'abc') |
---|
526 | n/a | with self.assertRaises(TypeError): |
---|
527 | n/a | copy.copy(m) |
---|
528 | n/a | |
---|
529 | n/a | def test_pickle(self): |
---|
530 | n/a | m = memoryview(b'abc') |
---|
531 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
532 | n/a | with self.assertRaises(TypeError): |
---|
533 | n/a | pickle.dumps(m, proto) |
---|
534 | n/a | |
---|
535 | n/a | |
---|
536 | n/a | if __name__ == "__main__": |
---|
537 | n/a | unittest.main() |
---|