Пример #1
0
 def mypack(self, fmt, value):
     size = struct.calcsize(fmt)
     wbuf = MutableStringBuffer(size)
     fake_fmtiter = self.mypack_into(fmt, wbuf, value)
     # check that we called advance() the right number of times
     assert fake_fmtiter.pos == wbuf.getlength()
     return wbuf.finish()
Пример #2
0
 def test_unaligned(self):
     # to force a non-aligned 'i'
     expected = struct.pack('=BBi', 0xAB, 0xCD, 0x1234)
     #
     wbuf = MutableStringBuffer(len(expected))
     wbuf.setitem(0, chr(0xAB))
     wbuf.setitem(1, chr(0xCD))
     fake_fmtiter = self.mypack_into('i', wbuf, 0x1234, advance=2)
     assert fake_fmtiter.pos == wbuf.getlength()
     got = wbuf.finish()
     assert got == expected
Пример #3
0
def _pack(space, format, args_w):
    """Return string containing values v1, v2, ... packed according to fmt."""
    size = _calcsize(space, format)
    wbuf = MutableStringBuffer(size)
    fmtiter = PackFormatIterator(space, wbuf, args_w)
    try:
        fmtiter.interpret(format)
    except StructOverflowError as e:
        raise OperationError(space.w_OverflowError, space.newtext(e.msg))
    except StructError as e:
        raise OperationError(get_error(space), space.newtext(e.msg))
    assert fmtiter.pos == wbuf.getlength(), 'missing .advance() or wrong calcsize()'
    return wbuf.finish()
Пример #4
0
 def test_subbuffer(self):
     # to force a non-aligned 'i'
     expected = struct.pack('=BBi', 0xAB, 0xCD, 0x1234)
     size = len(expected)
     #
     wbuf = MutableStringBuffer(size)
     wsubbuf = SubBuffer(wbuf, 2, size - 4)
     wbuf.setitem(0, chr(0xAB))
     wbuf.setitem(1, chr(0xCD))
     fake_fmtiter = self.mypack_into('i', wsubbuf, 0x1234)
     assert fake_fmtiter.pos == wbuf.getlength(
     ) - 2  # -2 since it's a SubBuffer
     got = wbuf.finish()
     assert got == expected
Пример #5
0
 def mypack_fn(self, func, size, arg, value):
     wbuf = MutableStringBuffer(size)
     fake_fmtiter = FakeFormatIter(self.bigendian, wbuf, value)
     func(fake_fmtiter, arg)
     assert fake_fmtiter.pos == wbuf.getlength()
     return wbuf.finish()