def wdouble(fd, obj): assert isinstance(obj, Float) result = MutableStringBuffer(8) ieee.pack_float(result, 0, obj.number, 8, True) val = result.finish() assert val is not None fd.write(val)
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()
def test_setitem(self): buf = MutableStringBuffer(4) buf.setitem(0, 'A') buf.setitem(1, 'B') buf.setitem(2, 'C') buf.setitem(3, 'D') assert buf.finish() == 'ABCD'
def pack(x, size): buf = MutableStringBuffer(size) ieee.pack_float(buf, 0, x, size, False) l = [] for c in buf.finish(): l.append(str(ord(c))) return ','.join(l)
def test_finish(self): buf = MutableStringBuffer(4) buf.setzeros(0, 4) pytest.raises(ValueError, "buf.as_str()") s = buf.finish() assert s == '\x00' * 4 pytest.raises(ValueError, "buf.finish()")
def readbuf_w(self, space): from rpython.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE buf = MutableStringBuffer(len(self._value) * UNICODE_SIZE) pos = 0 for unich in self._value: pack_unichar(unich, buf, pos) pos += UNICODE_SIZE return StringBuffer(buf.finish())
def test_pack_halffloat(self): size = 2 wbuf = MutableStringBuffer(size) self.mypack_into('e', wbuf, 6.5e+04) got = wbuf.finish() if self.bigendian: assert got == b'\x7b\xef' else: assert got == b'\xef\x7b'
def test_typed_write(self): expected = struct.pack('ifqd', 0x1234, 123.456, 0x12345678, 789.123) buf = MutableStringBuffer(24) buf.typed_write(rffi.INT, 0, 0x1234) buf.typed_write(rffi.FLOAT, 4, 123.456) buf.typed_write(rffi.LONGLONG, 8, 0x12345678) buf.typed_write(rffi.DOUBLE, 16, 789.123) s = buf.finish() assert s == expected
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
def bytes_from_value(self, space, w_val): from pypy.module.struct.formatiterator import PackFormatIterator itemsize = self.getitemsize() buf = MutableStringBuffer(itemsize) fmtiter = PackFormatIterator(space, buf, [w_val]) try: fmtiter.interpret(self.getformat()) except StructError as e: raise oefmt(space.w_TypeError, "memoryview: invalid type for format '%s'", self.getformat()) return buf.finish()
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()
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
def check_float(self, x): # check roundtrip for size in [10, 12, 16]: for be in [False, True]: Q = [] ieee.pack_float80(Q, x, size, be) Q = Q[0] y = ieee.unpack_float80(Q, be) assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) for be in [False, True]: buf = MutableStringBuffer(8) ieee.pack_float(buf, 0, x, 8, be) Q = buf.finish() y = ieee.unpack_float(Q, be) assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) # check that packing agrees with the struct module struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0] float_pack8 = ieee.float_pack(x, 8) assert struct_pack8 == float_pack8 # check that packing agrees with the struct module try: struct_pack4 = struct.unpack('<L', struct.pack('<f', x))[0] except OverflowError: struct_pack4 = "overflow" try: float_pack4 = ieee.float_pack(x, 4) except OverflowError: float_pack4 = "overflow" assert struct_pack4 == float_pack4 if float_pack4 == "overflow": return # if we didn't overflow, try round-tripping the binary32 value roundtrip = ieee.float_pack(ieee.float_unpack(float_pack4, 4), 4) assert float_pack4 == roundtrip try: float_pack2 = ieee.float_pack(x, 2) except OverflowError: return roundtrip = ieee.float_pack(ieee.float_unpack(float_pack2, 2), 2) assert (float_pack2, x) == (roundtrip, x)
def test_setzeros(self): buf = MutableStringBuffer(8) buf.setslice(0, 'ABCDEFGH') buf.setzeros(2, 3) assert buf.finish() == 'AB\x00\x00\x00FGH'
def test_setslice(self): buf = MutableStringBuffer(6) buf.setzeros(0, 6) buf.setslice(2, 'ABCD') assert buf.finish() == '\x00\x00ABCD'
def pack_float(f): buf = MutableStringBuffer(8) ieee.pack_float(buf, 0, f, 8, False) return buf.finish()
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()