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

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

Issue 15944: memoryviews and ctypes
Patch Set: Created 4 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 | « Doc/library/stdtypes.rst ('k') | Lib/test/test_memoryview.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 2541 matching lines...) Expand 10 before | Expand all | Expand 10 after
2552 dformat = '@' + dfmt if randrange(2) else dfmt 2552 dformat = '@' + dfmt if randrange(2) else dfmt
2553 dsize = struct.calcsize(dformat) 2553 dsize = struct.calcsize(dformat)
2554 ex = ndarray(list(range(32)), shape=[32//ssize], format=sformat) 2554 ex = ndarray(list(range(32)), shape=[32//ssize], format=sformat)
2555 msrc = memoryview(ex) 2555 msrc = memoryview(ex)
2556 self.assertRaises(TypeError, msrc.cast, dfmt, [32//dsize]) 2556 self.assertRaises(TypeError, msrc.cast, dfmt, [32//dsize])
2557 2557
2558 for sfmt, sitems, _ in iter_format(1): 2558 for sfmt, sitems, _ in iter_format(1):
2559 ex = ndarray(sitems, shape=[1], format=sfmt) 2559 ex = ndarray(sitems, shape=[1], format=sfmt)
2560 msrc = memoryview(ex) 2560 msrc = memoryview(ex)
2561 for dfmt, _, _ in iter_format(1): 2561 for dfmt, _, _ in iter_format(1):
2562 if (not is_memoryview_format(sfmt) or 2562 with self.subTest(sfmt=sfmt, dfmt=dfmt):
2563 not is_memoryview_format(dfmt)): 2563 if not is_memoryview_format(dfmt):
2564 self.assertRaises(ValueError, msrc.cast, dfmt, 2564 msg = ("destination format must be a "
2565 [32//dsize]) 2565 "native single character")
2566 else: 2566 self.assertRaisesRegex(ValueError, msg,
2567 if not is_byte_format(sfmt) and not is_byte_format(dfmt): 2567 msrc.cast, dfmt, [32//dsize])
2568 self.assertRaises(TypeError, msrc.cast, dfmt, 2568 else:
2569 [32//dsize]) 2569 if not is_byte_format(sfmt) and not is_byte_format(dfmt) :
2570 msg = "cast between two non-byte formats"
2571 self.assertRaisesRegex(TypeError, msg,
2572 msrc.cast, dfmt, [32//dsize])
2570 2573
2571 # invalid shape 2574 # invalid shape
2572 size_h = struct.calcsize('h') 2575 size_h = struct.calcsize('h')
2573 size_d = struct.calcsize('d') 2576 size_d = struct.calcsize('d')
2574 ex = ndarray(list(range(2*2*size_d)), shape=[2,2,size_d], format='h') 2577 ex = ndarray(list(range(2*2*size_d)), shape=[2,2,size_d], format='h')
2575 msrc = memoryview(ex) 2578 msrc = memoryview(ex)
2576 self.assertRaises(TypeError, msrc.cast, shape=[2,2,size_h], format='d') 2579 self.assertRaises(TypeError, msrc.cast, shape=[2,2,size_h], format='d')
2577 2580
2578 ex = ndarray(list(range(120)), shape=[1,2,3,4,5]) 2581 ex = ndarray(list(range(120)), shape=[1,2,3,4,5])
2579 m = memoryview(ex) 2582 m = memoryview(ex)
(...skipping 1777 matching lines...) Expand 10 before | Expand all | Expand 10 after
4357 nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED) 4360 nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED)
4358 self.assertRaises(BufferError, memoryview, nd) 4361 self.assertRaises(BufferError, memoryview, nd)
4359 4362
4360 def test_issue_7385(self): 4363 def test_issue_7385(self):
4361 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) 4364 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
4362 self.assertRaises(BufferError, memoryview, x) 4365 self.assertRaises(BufferError, memoryview, x)
4363 4366
4364 4367
4365 if __name__ == "__main__": 4368 if __name__ == "__main__":
4366 unittest.main() 4369 unittest.main()
OLDNEW
« no previous file with comments | « Doc/library/stdtypes.rst ('k') | Lib/test/test_memoryview.py » ('j') | no next file with comments »

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