1"""Unit tests for buffer objects. 2 3For now, tests just new or changed functionality. 4 5""" 6 7import copy 8import pickle 9import sys 10import unittest 11import warnings 12from test import test_support 13 14class BufferTests(unittest.TestCase): 15 16 def test_extended_getslice(self): 17 # Test extended slicing by comparing with list slicing. 18 s = "".join(chr(c) for c in list(range(255, -1, -1))) 19 b = buffer(s) 20 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) 21 for start in indices: 22 for stop in indices: 23 # Skip step 0 (invalid) 24 for step in indices[1:]: 25 self.assertEqual(b[start:stop:step], 26 s[start:stop:step]) 27 28 def test_newbuffer_interface(self): 29 # Test that the buffer object has the new buffer interface 30 # as used by the memoryview object 31 s = "".join(chr(c) for c in list(range(255, -1, -1))) 32 b = buffer(s) 33 m = memoryview(b) # Should not raise an exception 34 self.assertEqual(m.tobytes(), s) 35 36 def test_large_buffer_size_and_offset(self): 37 data = bytearray('hola mundo') 38 buf = buffer(data, sys.maxsize, sys.maxsize) 39 self.assertEqual(buf[:4096], "") 40 41 def test_copy(self): 42 buf = buffer(b'abc') 43 with self.assertRaises(TypeError), warnings.catch_warnings(): 44 warnings.filterwarnings('ignore', ".*buffer", DeprecationWarning) 45 copy.copy(buf) 46 47 @test_support.cpython_only 48 def test_pickle(self): 49 buf = buffer(b'abc') 50 for proto in range(2): 51 with self.assertRaises(TypeError): 52 pickle.dumps(buf, proto) 53 with test_support.check_py3k_warnings( 54 (".*buffer", DeprecationWarning)): 55 pickle.dumps(buf, 2) 56 57 58def test_main(): 59 with test_support.check_py3k_warnings(("buffer.. not supported", 60 DeprecationWarning)): 61 test_support.run_unittest(BufferTests) 62 63if __name__ == "__main__": 64 test_main() 65