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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 years, 4 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_bufio.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 910 matching lines...) Expand 10 before | Expand all | Expand 10 after
2486 except struct.error: 2486 except struct.error:
2487 struct_err = struct.error 2487 struct_err = struct.error
2488 2488
2489 mv_err = None 2489 mv_err = None
2490 try: 2490 try:
2491 m[1] = v 2491 m[1] = v
2492 except (TypeError, ValueError) as e: 2492 except (TypeError, ValueError) as e:
2493 mv_err = e.__class__ 2493 mv_err = e.__class__
2494 2494
2495 if struct_err or mv_err: 2495 if struct_err or mv_err:
2496 self.assertIsNot(struct_err, None) 2496 self.assertIsNotNone(struct_err)
2497 self.assertIsNot(mv_err, None) 2497 self.assertIsNotNone(mv_err)
2498 else: 2498 else:
2499 self.assertEqual(m[1], nd[1]) 2499 self.assertEqual(m[1], nd[1])
2500 2500
2501 def test_memoryview_cast_zero_strides(self): 2501 def test_memoryview_cast_zero_strides(self):
2502 # Casts are undefined if strides contains zeros. These arrays are 2502 # Casts are undefined if strides contains zeros. These arrays are
2503 # (sometimes!) regarded as C-contiguous by Numpy, but not by 2503 # (sometimes!) regarded as C-contiguous by Numpy, but not by
2504 # PyBuffer_GetContiguous(). 2504 # PyBuffer_GetContiguous().
2505 ex = ndarray([1,2,3], shape=[3], strides=[0]) 2505 ex = ndarray([1,2,3], shape=[3], strides=[0])
2506 self.assertFalse(ex.c_contiguous) 2506 self.assertFalse(ex.c_contiguous)
2507 msrc = memoryview(ex) 2507 msrc = memoryview(ex)
(...skipping 1734 matching lines...) Expand 10 before | Expand all | Expand 10 after
4242 4242
4243 x = staticarray(legacy_mode=True) 4243 x = staticarray(legacy_mode=True)
4244 y = memoryview(x) 4244 y = memoryview(x)
4245 del y 4245 del y
4246 del x 4246 del x
4247 4247
4248 x = staticarray(legacy_mode=True) 4248 x = staticarray(legacy_mode=True)
4249 y = ndarray(x, getbuf=PyBUF_FULL_RO) 4249 y = ndarray(x, getbuf=PyBUF_FULL_RO)
4250 z = ndarray(y, getbuf=PyBUF_FULL_RO) 4250 z = ndarray(y, getbuf=PyBUF_FULL_RO)
4251 m = memoryview(z) 4251 m = memoryview(z)
4252 self.assertIs(y.obj, None) 4252 self.assertIsNone(y.obj)
4253 self.assertIs(m.obj, z) 4253 self.assertIs(m.obj, z)
4254 self.verify(m, obj=z, 4254 self.verify(m, obj=z,
4255 itemsize=1, fmt=fmt, readonly=1, 4255 itemsize=1, fmt=fmt, readonly=1,
4256 ndim=1, shape=[12], strides=[1], 4256 ndim=1, shape=[12], strides=[1],
4257 lst=lst) 4257 lst=lst)
4258 del x, y, z, m 4258 del x, y, z, m
4259 4259
4260 x = staticarray(legacy_mode=True) 4260 x = staticarray(legacy_mode=True)
4261 y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) 4261 y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
4262 z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) 4262 z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
4263 m = memoryview(z) 4263 m = memoryview(z)
4264 # Clearly setting view.obj==NULL is inferior, since it 4264 # Clearly setting view.obj==NULL is inferior, since it
4265 # messes up the redirection chain: 4265 # messes up the redirection chain:
4266 self.assertIs(y.obj, None) 4266 self.assertIsNone(y.obj)
4267 self.assertIs(z.obj, y) 4267 self.assertIs(z.obj, y)
4268 self.assertIs(m.obj, y) 4268 self.assertIs(m.obj, y)
4269 self.verify(m, obj=y, 4269 self.verify(m, obj=y,
4270 itemsize=1, fmt=fmt, readonly=1, 4270 itemsize=1, fmt=fmt, readonly=1,
4271 ndim=1, shape=[12], strides=[1], 4271 ndim=1, shape=[12], strides=[1],
4272 lst=lst) 4272 lst=lst)
4273 del x, y, z, m 4273 del x, y, z, m
4274 4274
4275 def test_memoryview_getbuffer_undefined(self): 4275 def test_memoryview_getbuffer_undefined(self):
4276 4276
4277 # getbufferproc does not adhere to the new documentation 4277 # getbufferproc does not adhere to the new documentation
4278 nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED) 4278 nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED)
4279 self.assertRaises(BufferError, memoryview, nd) 4279 self.assertRaises(BufferError, memoryview, nd)
4280 4280
4281 def test_issue_7385(self): 4281 def test_issue_7385(self):
4282 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) 4282 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
4283 self.assertRaises(BufferError, memoryview, x) 4283 self.assertRaises(BufferError, memoryview, x)
4284 4284
4285 4285
4286 def test_main(): 4286 def test_main():
4287 support.run_unittest(TestBufferProtocol) 4287 support.run_unittest(TestBufferProtocol)
4288 4288
4289 4289
4290 if __name__ == "__main__": 4290 if __name__ == "__main__":
4291 test_main() 4291 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_bisect.py ('k') | Lib/test/test_bufio.py » ('j') | no next file with comments »

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