def __init__(self, filePathName, conversionFmt, bufSizeInRecs): self._c = _struct.Struct(conversionFmt) self._s = _struct.calcsize(conversionFmt) self._b = bytearray(bufSizeInRecs * self._s) self._o = 0 self._in = gzip.open(filePathName, "rb") self._m = len(self._b) self._sn = _struct.Struct(">Q") self._r = deque() self._read()
def test_struct_uint_bad_value_cp20039(self): class x(object): def __init__(self, value): self.value = value def __and__(self, other): global andCalled andCalled = True return self.value def __int__(self): raise Exception('foo') import _struct global andCalled andCalled = False self.assertRaisesRegexp(_struct.error, "integer out of range for 'L' format code", _struct.Struct('L').pack, 4294967296) self.assertRaisesRegexp(_struct.error, "integer out of range for 'L' format code", _struct.Struct('L').pack, -1) self.assertRaisesRegexp(Exception, "foo", _struct.Struct('L').pack, x(0)) self.assertRaisesRegexp(Exception, "foo", _struct.Struct('L').pack, x(-1)) self.assertRaisesRegexp(_struct.error, "integer out of range for 'I' format code", _struct.Struct('I').pack, 4294967296) self.assertRaisesRegexp(_struct.error, "integer out of range for 'I' format code", _struct.Struct('I').pack, -1) self.assertRaisesRegexp(Exception, "foo", _struct.Struct('I').pack, x(0)) self.assertRaisesRegexp(Exception, "foo", _struct.Struct('I').pack, x(-1)) # __and__ was called in Python2.6 check that this is no longer True self.assertTrue(not andCalled)
def test_new_init(self): """tests for calling __new__/__init__ directly on the Struct object""" for x in (_struct.Struct.__new__(_struct.Struct), _struct.Struct.__new__(_struct.Struct, a = 2)): # state of uninitialized object... self.assertEqual(x.size, -1) self.assertEqual(x.format, None if is_cli or is_cpython and sys.version_info < (3,7) else '\x00\x00\x00\x00\x00\x00\x00') self.assertRaisesMessage(_struct.error, "pack requires exactly -1 arguments" if is_cli else "pack expected -1 items for packing (got 0)", x.pack) self.assertRaisesMessage(_struct.error, "unpack requires a bytes object of length -1" if is_cli or is_cpython and sys.version_info < (3,6) else "unpack requires a buffer of -1 bytes", x.unpack, b'') # invalid format passed to __init__ - format string is updated but old format info is stored... a = _struct.Struct('c') try: a.__init__('bad') self.fail("Unreachable code reached") except _struct.error as e: pass self.assertEqual(a.format, 'bad' if is_cli or is_cpython and sys.version_info >= (3,7) else b"bad") self.assertEqual(a.pack(b'1'), b'1') self.assertEqual(a.unpack(b'1'), (b'1', )) # and then back to a valid format a.__init__('i') self.assertEqual(a.format, 'i' if is_cli or is_cpython and sys.version_info >= (3,7) else b'i') self.assertEqual(a.pack(0), b'\x00\x00\x00\x00') self.assertEqual(a.unpack(b'\x00\x00\x00\x00'), (0, ))
def test_new_init(self): """tests for calling __new__/__init__ directly on the Struct object""" for x in (_struct.Struct.__new__(_struct.Struct), _struct.Struct.__new__(_struct.Struct, a = 2)): # state of uninitialized object... self.assertEqual(x.size, -1) self.assertEqual(x.format, None) self.assertRaisesMessage(_struct.error, "pack requires exactly -1 arguments", x.pack) self.assertRaisesMessage(_struct.error, "unpack requires a string argument of length -1", x.unpack, b'') # invalid format passed to __init__ - format string is updated but old format info is stored... a = _struct.Struct('c') try: a.__init__('bad') self.assertUnreachable() except _struct.error as e: pass self.assertEqual(a.format, 'bad') self.assertEqual(a.pack(b'1'), b'1') self.assertEqual(a.unpack(b'1'), (b'1', )) # and then back to a valid format a.__init__('i') self.assertEqual(a.format, 'i') self.assertEqual(a.pack(0), b'\x00\x00\x00\x00') self.assertEqual(a.unpack(b'\x00\x00\x00\x00'), (0, ))
def test_new_init(): """tests for calling __new__/__init__ directly on the Struct object""" for x in (_struct.Struct.__new__(_struct.Struct), _struct.Struct.__new__(_struct.Struct, a = 2)): # state of uninitialized object... AreEqual(x.size, -1) AreEqual(x.format, None) AssertErrorWithMessage(_struct.error, "pack requires exactly -1 arguments", x.pack) AssertErrorWithMessage(_struct.error, "unpack requires a string argument of length -1", x.unpack, '') # invalid format passed to __init__ - format string is updated but old format info is stored... a = _struct.Struct('c') try: a.__init__('bad') AssertUnreachable() except _struct.error, e: pass
def test_weakref(self): """weakrefs to struct objects are supported""" x = _struct.Struct('i') import _weakref self.assertEqual(_weakref.proxy(x).size, x.size)
def calcsize(f): return _struct.Struct(f).size
def unpack(f, *v): return _struct.Struct(f).unpack(*v)
def pack(f, *v): return _struct.Struct(f).pack(*v)
def test_struct_uint_bad_value_cp20039(): class x(object): def __init__(self, value): self.value = value def __and__(self, other): global andCalled andCalled = True return self.value def __int__(self): raise Exception('foo') import _struct global andCalled if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AreEqual(_struct.Struct('L').pack(4294967296), '\x00\x00\x00\x00') else: AssertErrorWithMessage(_struct.error, "integer out of range for 'L' format code", _struct.Struct('L').pack, 4294967296) if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AreEqual(_struct.Struct('L').pack(-1), '\xff\xff\xff\xff') else: AssertErrorWithMessage(_struct.error, "integer out of range for 'L' format code", _struct.Struct('L').pack, -1) if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AreEqual(_struct.Struct('L').pack(x(0)), '\x00\x00\x00\x00') AreEqual(andCalled, True) else: AssertErrorWithMessage(Exception, "foo", _struct.Struct('L').pack, x(0)) if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AreEqual(_struct.Struct('I').pack(4294967296), '\x00\x00\x00\x00') else: AssertErrorWithMessage(_struct.error, "integer out of range for 'I' format code", _struct.Struct('I').pack, 4294967296) if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AreEqual(_struct.Struct('I').pack(-1), '\xff\xff\xff\xff') else: AssertErrorWithMessage(_struct.error, "integer out of range for 'I' format code", _struct.Struct('I').pack, -1) andCalled = False if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AreEqual(_struct.Struct('I').pack(x(0)), '\x00\x00\x00\x00') AreEqual(andCalled, True) else: AssertErrorWithMessage(Exception, "foo", _struct.Struct('I').pack, x(0)) if is_ironpython: #http://ironpython.codeplex.com/workitem/27901 AssertError(OverflowError, _struct.Struct('I').pack, x(-1)) AssertError(OverflowError, _struct.Struct('L').pack, x(-1)) else: AssertErrorWithMessage(Exception, "foo", _struct.Struct('I').pack, x(-1)) AssertErrorWithMessage(Exception, "foo", _struct.Struct('L').pack, x(-1))
def playSnake(pad, fb): ogc.video.SetNextFramebuffer(fb[1]) ogc.video.Flush() ogc.video.SetNextFramebuffer(fb[0]) ogc.video.Flush() import ogc import _struct pxlpr = _struct.Struct('L') fbuffer = fb.get_buffer() fb.clear(ogc.video.COLOR_SKYBLUE) N, E, S, W = (0,-1), (1,0), (0,1), (-1,0) MAX_X, MAX_Y = 319, 479 GROWTH_FRAMES = 20 snake = [ (160,240) ] direction = E frames_till_growth = 0 paused = False while True: # Handle input if pad['Up']: direction = N elif pad['Down']: direction = S elif pad['Left']: direction = W elif pad['Right']: direction = E if pad['X']: break elif pad['Start']: paused = not paused thread_start # If paused, don't execute the game if paused: ogc.video.WaitVSync() continue # Find next position x = snake[-1][0] + direction[0] y = snake[-1][1] + direction[1] if x < 0: x = MAX_X elif x > MAX_X: x = 0 if y < 0: y = MAX_Y elif y > MAX_Y: y = 0 # Check if we've run into ourselves collision = False for x2,y2 in snake: if x==x2 and y==y2: collision = True break if collision: break # Remove the end of the tail unless we grew if frames_till_growth > 0: frames_till_growth = frames_till_growth - 1 snake = snake[1:] else: frames_till_growth = GROWTH_FRAMES snake.append( (x,y) ) # Actually draw the snake fb.clear(ogc.video.COLOR_SKYBLUE) for i,j in snake: pxlpr.pack_into(fbuffer, 2*640*j+4*i, ogc.video.COLOR_GREEN) ogc.video.WaitVSync() fb.clear(ogc.video.COLOR_RED) for i in range(30): ogc.video.WaitVSync()