Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(169320)

Side by Side Diff: Lib/test/test_buffer.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_bisect.py ('k') | Lib/test/test_builtin.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # 1 #
2 # The ndarray object from _testbuffer.c is a complete implementation of 2 # The ndarray object from _testbuffer.c is a complete implementation of
3 # a PEP-3118 buffer provider. It is independent from NumPy's ndarray 3 # a PEP-3118 buffer provider. It is independent from NumPy's ndarray
4 # and the tests don't require NumPy. 4 # and the tests don't require NumPy.
5 # 5 #
6 # If NumPy is present, some tests check both ndarray implementations 6 # If NumPy is present, some tests check both ndarray implementations
7 # against each other. 7 # against each other.
8 # 8 #
9 # Most ndarray tests also check that memoryview(ndarray) behaves in 9 # Most ndarray tests also check that memoryview(ndarray) behaves in
10 # the same way as the original. Thus, a substantial part of the 10 # the same way as the original. Thus, a substantial part of the
(...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1321 lst = carray(items, shape) 1321 lst = carray(items, shape)
1322 self.verify(nd, obj=None, 1322 self.verify(nd, obj=None,
1323 itemsize=itemsize, fmt=fmt, readonly=1, 1323 itemsize=itemsize, fmt=fmt, readonly=1,
1324 ndim=ndim, shape=shape, strides=strides, 1324 ndim=ndim, shape=shape, strides=strides,
1325 lst=lst) 1325 lst=lst)
1326 1326
1327 if is_memoryview_format(fmt): 1327 if is_memoryview_format(fmt):
1328 # memoryview: reconstruct strides 1328 # memoryview: reconstruct strides
1329 ex = ndarray(items, shape=shape, format=fmt) 1329 ex = ndarray(items, shape=shape, format=fmt)
1330 nd = ndarray(ex, getbuf=PyBUF_CONTIG_RO|PyBUF_FORMAT) 1330 nd = ndarray(ex, getbuf=PyBUF_CONTIG_RO|PyBUF_FORMAT)
1331 self.assertTrue(nd.strides == ()) 1331 self.assertEqual(nd.strides, ())
1332 mv = nd.memoryview_from_buffer() 1332 mv = nd.memoryview_from_buffer()
1333 self.verify(mv, obj=None, 1333 self.verify(mv, obj=None,
1334 itemsize=itemsize, fmt=fmt, readonly=1, 1334 itemsize=itemsize, fmt=fmt, readonly=1,
1335 ndim=ndim, shape=shape, strides=strides, 1335 ndim=ndim, shape=shape, strides=strides,
1336 lst=lst) 1336 lst=lst)
1337 1337
1338 # Fortran array 1338 # Fortran array
1339 nd = ndarray(items, shape=shape, format=fmt, 1339 nd = ndarray(items, shape=shape, format=fmt,
1340 flags=flags|ND_FORTRAN) 1340 flags=flags|ND_FORTRAN)
1341 1341
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 nd = ndarray(1, shape=()) 1549 nd = ndarray(1, shape=())
1550 self.assertRaises(TypeError, eval, "1 in nd", locals()) 1550 self.assertRaises(TypeError, eval, "1 in nd", locals())
1551 mv = memoryview(nd) 1551 mv = memoryview(nd)
1552 self.assertEqual(mv, nd) 1552 self.assertEqual(mv, nd)
1553 self.assertRaises(TypeError, eval, "1 in mv", locals()) 1553 self.assertRaises(TypeError, eval, "1 in mv", locals())
1554 1554
1555 for fmt, items, _ in iter_format(5): 1555 for fmt, items, _ in iter_format(5):
1556 nd = ndarray(items, shape=[5], format=fmt) 1556 nd = ndarray(items, shape=[5], format=fmt)
1557 for i, v in enumerate(nd): 1557 for i, v in enumerate(nd):
1558 self.assertEqual(v, items[i]) 1558 self.assertEqual(v, items[i])
1559 self.assertTrue(v in nd) 1559 self.assertIn(v, nd)
1560 1560
1561 if is_memoryview_format(fmt): 1561 if is_memoryview_format(fmt):
1562 mv = memoryview(nd) 1562 mv = memoryview(nd)
1563 for i, v in enumerate(mv): 1563 for i, v in enumerate(mv):
1564 self.assertEqual(v, items[i]) 1564 self.assertEqual(v, items[i])
1565 self.assertTrue(v in mv) 1565 self.assertIn(v, mv)
1566 1566
1567 def test_ndarray_slice_invalid(self): 1567 def test_ndarray_slice_invalid(self):
1568 items = [1,2,3,4,5,6,7,8] 1568 items = [1,2,3,4,5,6,7,8]
1569 1569
1570 # rvalue is not an exporter 1570 # rvalue is not an exporter
1571 xl = ndarray(items, shape=[8], flags=ND_WRITABLE) 1571 xl = ndarray(items, shape=[8], flags=ND_WRITABLE)
1572 ml = memoryview(xl) 1572 ml = memoryview(xl)
1573 self.assertRaises(TypeError, xl.__setitem__, slice(0,8,1), items) 1573 self.assertRaises(TypeError, xl.__setitem__, slice(0,8,1), items)
1574 self.assertRaises(TypeError, ml.__setitem__, slice(0,8,1), items) 1574 self.assertRaises(TypeError, ml.__setitem__, slice(0,8,1), items)
1575 1575
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after
2493 except struct.error: 2493 except struct.error:
2494 struct_err = struct.error 2494 struct_err = struct.error
2495 2495
2496 mv_err = None 2496 mv_err = None
2497 try: 2497 try:
2498 m[1] = v 2498 m[1] = v
2499 except (TypeError, ValueError) as e: 2499 except (TypeError, ValueError) as e:
2500 mv_err = e.__class__ 2500 mv_err = e.__class__
2501 2501
2502 if struct_err or mv_err: 2502 if struct_err or mv_err:
2503 self.assertIsNot(struct_err, None) 2503 self.assertIsNotNone(struct_err)
2504 self.assertIsNot(mv_err, None) 2504 self.assertIsNotNone(mv_err)
2505 else: 2505 else:
2506 self.assertEqual(m[1], nd[1]) 2506 self.assertEqual(m[1], nd[1])
2507 2507
2508 def test_memoryview_cast_zero_strides(self): 2508 def test_memoryview_cast_zero_strides(self):
2509 # Casts are undefined if strides contains zeros. These arrays are 2509 # Casts are undefined if strides contains zeros. These arrays are
2510 # (sometimes!) regarded as C-contiguous by Numpy, but not by 2510 # (sometimes!) regarded as C-contiguous by Numpy, but not by
2511 # PyBuffer_GetContiguous(). 2511 # PyBuffer_GetContiguous().
2512 ex = ndarray([1,2,3], shape=[3], strides=[0]) 2512 ex = ndarray([1,2,3], shape=[3], strides=[0])
2513 self.assertFalse(ex.c_contiguous) 2513 self.assertFalse(ex.c_contiguous)
2514 msrc = memoryview(ex) 2514 msrc = memoryview(ex)
(...skipping 1734 matching lines...) Expand 10 before | Expand all | Expand 10 after
4249 4249
4250 x = staticarray(legacy_mode=True) 4250 x = staticarray(legacy_mode=True)
4251 y = memoryview(x) 4251 y = memoryview(x)
4252 del y 4252 del y
4253 del x 4253 del x
4254 4254
4255 x = staticarray(legacy_mode=True) 4255 x = staticarray(legacy_mode=True)
4256 y = ndarray(x, getbuf=PyBUF_FULL_RO) 4256 y = ndarray(x, getbuf=PyBUF_FULL_RO)
4257 z = ndarray(y, getbuf=PyBUF_FULL_RO) 4257 z = ndarray(y, getbuf=PyBUF_FULL_RO)
4258 m = memoryview(z) 4258 m = memoryview(z)
4259 self.assertIs(y.obj, None) 4259 self.assertIsNone(y.obj)
4260 self.assertIs(m.obj, z) 4260 self.assertIs(m.obj, z)
4261 self.verify(m, obj=z, 4261 self.verify(m, obj=z,
4262 itemsize=1, fmt=fmt, readonly=1, 4262 itemsize=1, fmt=fmt, readonly=1,
4263 ndim=1, shape=[12], strides=[1], 4263 ndim=1, shape=[12], strides=[1],
4264 lst=lst) 4264 lst=lst)
4265 del x, y, z, m 4265 del x, y, z, m
4266 4266
4267 x = staticarray(legacy_mode=True) 4267 x = staticarray(legacy_mode=True)
4268 y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) 4268 y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
4269 z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) 4269 z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
4270 m = memoryview(z) 4270 m = memoryview(z)
4271 # Clearly setting view.obj==NULL is inferior, since it 4271 # Clearly setting view.obj==NULL is inferior, since it
4272 # messes up the redirection chain: 4272 # messes up the redirection chain:
4273 self.assertIs(y.obj, None) 4273 self.assertIsNone(y.obj)
4274 self.assertIs(z.obj, y) 4274 self.assertIs(z.obj, y)
4275 self.assertIs(m.obj, y) 4275 self.assertIs(m.obj, y)
4276 self.verify(m, obj=y, 4276 self.verify(m, obj=y,
4277 itemsize=1, fmt=fmt, readonly=1, 4277 itemsize=1, fmt=fmt, readonly=1,
4278 ndim=1, shape=[12], strides=[1], 4278 ndim=1, shape=[12], strides=[1],
4279 lst=lst) 4279 lst=lst)
4280 del x, y, z, m 4280 del x, y, z, m
4281 4281
4282 def test_memoryview_getbuffer_undefined(self): 4282 def test_memoryview_getbuffer_undefined(self):
4283 4283
4284 # getbufferproc does not adhere to the new documentation 4284 # getbufferproc does not adhere to the new documentation
4285 nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED) 4285 nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED)
4286 self.assertRaises(BufferError, memoryview, nd) 4286 self.assertRaises(BufferError, memoryview, nd)
4287 4287
4288 def test_issue_7385(self): 4288 def test_issue_7385(self):
4289 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) 4289 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
4290 self.assertRaises(BufferError, memoryview, x) 4290 self.assertRaises(BufferError, memoryview, x)
4291 4291
4292 4292
4293 if __name__ == "__main__": 4293 if __name__ == "__main__":
4294 unittest.main() 4294 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_bisect.py ('k') | Lib/test/test_builtin.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+