def test_write(self): # __doc__ (as of 2008-08-02) for pygame.bufferproxy.BufferProxy.write: # B.write (bufferproxy, buffer, offset) -> None # # Writes raw data to the bufferproxy. # # Writes the raw data from buffer to the BufferProxy object, starting # at the specified offset within the BufferProxy. # If the length of the passed buffer exceeds the length of the # BufferProxy (reduced by the offset), an IndexError will be raised. from ctypes import c_byte, sizeof, addressof, string_at, memset nullbyte = '\x00'.encode('latin_1') Buf = c_byte * 10 data_buf = Buf(*range(1, 3 * sizeof(Buf) + 1, 3)) data = string_at(data_buf, sizeof(data_buf)) buf = Buf() bp = BufferProxy({ 'typestr': '|u1', 'shape': (sizeof(buf), ), 'data': (addressof(buf), False) }) try: self.assertEqual(bp.raw, nullbyte * sizeof(Buf)) bp.write(data) self.assertEqual(bp.raw, data) memset(buf, 0, sizeof(buf)) bp.write(data[:3], 2) raw = bp.raw self.assertEqual(raw[:2], nullbyte * 2) self.assertEqual(raw[2:5], data[:3]) self.assertEqual(raw[5:], nullbyte * (sizeof(Buf) - 5)) bp.write(data[:3], bp.length - 3) raw = bp.raw self.assertEqual(raw[-3:], data[:3]) self.assertRaises(IndexError, bp.write, data, 1) self.assertRaises(IndexError, bp.write, data[:5], -1) self.assertRaises(IndexError, bp.write, data[:5], bp.length) self.assertRaises(TypeError, bp.write, 12) bp = BufferProxy({ 'typestr': '|u1', 'shape': (sizeof(buf), ), 'data': (addressof(buf), True) }) self.assertRaises(pygame.BufferError, bp.write, '123'.encode('latin_1')) finally: # Make sure bp is garbage collected before buf bp = None gc.collect()
def test_write(self): # __doc__ (as of 2008-08-02) for pygame.bufferproxy.BufferProxy.write: # B.write (bufferproxy, buffer, offset) -> None # # Writes raw data to the bufferproxy. # # Writes the raw data from buffer to the BufferProxy object, starting # at the specified offset within the BufferProxy. # If the length of the passed buffer exceeds the length of the # BufferProxy (reduced by the offset), an IndexError will be raised. from ctypes import c_byte, sizeof, addressof, string_at, memset nullbyte = '\x00'.encode('latin_1') Buf = c_byte * 10 data_buf = Buf(*range(1, 3 * sizeof(Buf) + 1, 3)) data = string_at(data_buf, sizeof(data_buf)) buf = Buf() bp = BufferProxy({'typestr': '|u1', 'shape': (sizeof(buf),), 'data': (addressof(buf), False)}) try: self.assertEqual(bp.raw, nullbyte * sizeof(Buf)) bp.write(data) self.assertEqual(bp.raw, data) memset(buf, 0, sizeof(buf)) bp.write(data[:3], 2) raw = bp.raw self.assertEqual(raw[:2], nullbyte * 2) self.assertEqual(raw[2:5], data[:3]) self.assertEqual(raw[5:], nullbyte * (sizeof(Buf) - 5)) bp.write(data[:3], bp.length - 3) raw = bp.raw self.assertEqual(raw[-3:], data[:3]) self.assertRaises(IndexError, bp.write, data, 1) self.assertRaises(IndexError, bp.write, data[:5], -1) self.assertRaises(IndexError, bp.write, data[:5], bp.length) self.assertRaises(TypeError, bp.write, 12) bp = BufferProxy({'typestr': '|u1', 'shape': (sizeof(buf),), 'data': (addressof(buf), True)}) self.assertRaises(BufferError, bp.write, '123'.encode('latin_1')) finally: # Make sure bp is garbage collected before buf bp = None gc.collect()