def unpack_ieee(fmtiter): size = rffi.sizeof(TYPE) if fmtiter.bigendian != native_is_bigendian or not native_is_ieee754: # fallback to the very slow unpacking code in ieee.py data = fmtiter.read(size) fmtiter.appendobj(ieee.unpack_float(data, fmtiter.bigendian)) return ## XXX check if the following code is still needed ## if not str_storage_supported(TYPE): ## # this happens e.g. on win32 and ARM32: we cannot read the string ## # content as an array of doubles because it's not properly ## # aligned. But we can read a longlong and convert to float ## assert TYPE == rffi.DOUBLE ## assert rffi.sizeof(TYPE) == 8 ## return unpack_longlong2float(fmtiter) try: # fast path val = unpack_fastpath(TYPE)(fmtiter) except CannotRead: # slow path: we should arrive here only if we could not unpack # because of alignment issues. So we copy the slice into a new # string, which is guaranteed to be properly aligned, and read the # float/double from there input = fmtiter.read(size) val = StringBuffer(input).typed_read(TYPE, 0) fmtiter.appendobj(float(val))
def make_buffer(flag): if flag: buf = ByteBuffer(len(DATA)) buf.setslice(0, DATA) else: buf = StringBuffer(DATA) return buf
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_string_buffer(): buf = StringBuffer('hello world') assert buf.getitem(4) == 'o' assert buf.getitem(4) == buf[4] assert buf.getlength() == 11 assert buf.getlength() == len(buf) assert buf.getslice(1, 1, 5) == 'ello ' assert buf.getslice(1, 1, 5) == buf[1:6] assert buf.getslice(1, 2, 3) == 'el ' assert buf.as_str() == 'hello world'
def test_frombuffer(self, space, api): w_buf = space.newbuffer(StringBuffer("hello")) c_memoryview = rffi.cast(PyMemoryViewObject, api.PyMemoryView_FromObject(w_buf)) w_memoryview = from_ref(space, c_memoryview) view = c_memoryview.c_view assert view.c_ndim == 1 f = rffi.charp2str(view.c_format) assert f == 'B' assert view.c_shape[0] == 5 assert view.c_strides[0] == 1 assert view.c_len == 5 o = rffi.charp2str(view.c_buf) assert o == 'hello' ref = api.PyMemoryView_FromBuffer(view) w_mv = from_ref(space, ref) for f in ('format', 'itemsize', 'ndim', 'readonly', 'shape', 'strides', 'suboffsets'): w_f = space.wrap(f) assert space.eq_w(space.getattr(w_mv, w_f), space.getattr(w_memoryview, w_f)) api.Py_DecRef(ref)
def test_as_str_and_offset_maybe(): buf = StringBuffer('hello world') assert buf.as_str_and_offset_maybe() == ('hello world', 0) # sbuf = SubBuffer(buf, 6, 5) assert sbuf.getslice(0, 5, 1, 5) == 'world' assert sbuf.as_str_and_offset_maybe() == ('hello world', 6) # ssbuf = SubBuffer(sbuf, 3, 2) assert ssbuf.getslice(0, 2, 1, 2) == 'ld' assert ssbuf.as_str_and_offset_maybe() == ('hello world', 9) # ss2buf = SubBuffer(sbuf, 1, -1) assert ss2buf.as_str() == 'orld' assert ss2buf.getlength() == 4 ss3buf = SubBuffer(ss2buf, 1, -1) assert ss3buf.as_str() == 'rld' assert ss3buf.getlength() == 3 # ss4buf = SubBuffer(buf, 3, 4) assert ss4buf.as_str() == 'lo w' ss5buf = SubBuffer(ss4buf, 1, -1) assert ss5buf.as_str() == 'o w' assert ss5buf.getlength() == 3
def buffer_w(self, space, flags): return StringBuffer("foobar")
def read(self, TYPE, data, offset): buf = StringBuffer('x' * 16 + data) subbuf = SubBuffer(buf, 16, len(data)) return subbuf.typed_read(TYPE, offset)
def buffer_w(self, space, flags): space.check_buf_flags(flags, True) return SimpleView(StringBuffer(self._value))
def readbuf_w(self, space): return StringBuffer(self._value)
def test_repeated_subbuffer(): buf = StringBuffer('x' * 10000) for i in range(9999, 9, -1): buf = SubBuffer(buf, 1, i) assert buf.getlength() == 10
def test_string_buffer_as_buffer(): buf = StringBuffer(b'hello world') addr = buf.get_raw_address() assert addr[0] == b'h' assert addr[4] == b'o' assert addr[6] == b'w'
def as_readbuf(self): # Inefficient. May be overridden. return StringBuffer(self.as_str())
def f(x): buf = StringBuffer(x) return direct_read(buf), typed_read(buf)
def __init__(self, s): self.inputbuf = StringBuffer(s) self.length = len(s) self.inputpos = 0
def value_from_bytes(self, space, s): from pypy.module.struct.formatiterator import UnpackFormatIterator buf = StringBuffer(s) fmtiter = UnpackFormatIterator(space, buf) fmtiter.interpret(self.getformat()) return fmtiter.result_w[0]
def read(self, TYPE, data, offset): buf = StringBuffer(data) return buf.typed_read(TYPE, offset)
def _test_sre_ctx_buf_(self, str, start, end): # Test BufMatchContext. buf = StringBuffer(str) return rsre_core.BufMatchContext(buf, start, end)
def fn(): res = True res = res and find('a//b//c//d', StringBuffer('//'), 0, 10) != -1 res = res and rfind('a//b//c//d', StringBuffer('//'), 0, 10) != -1 res = res and count('a//b//c//d', StringBuffer('//'), 0, 10) != 0 return res
def readbuf_w(self, space): from rpython.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE builder = StringBuilder(len(self._value) * UNICODE_SIZE) for unich in self._value: pack_unichar(unich, builder) return StringBuffer(builder.build())
def llf(data, offset): buf = StringBuffer(data) x = buf.typed_read(TYPE, offset) return lltype.cast_primitive(TARGET_TYPE, x)