def test_SimpleView_basic(): buf = RawByteBuffer(10) view = SimpleView(buf) assert view.getlength() == 10 assert view.getformat() == 'B' assert view.getitemsize() == 1 assert view.getndim() == 1 assert view.getshape() == [10] assert view.getstrides() == [1] assert view.as_readbuf() is view.as_writebuf() is buf
def _raw_read(self, space, buffer, start, length): assert buffer is not None length = intmask(length) start = intmask(start) w_view = SimpleView(SubBuffer(buffer, start, length)).wrap(space) while True: try: w_size = space.call_method(self.w_raw, "readinto", w_view) except OperationError as e: if trap_eintr(space, e): continue # try again raise else: break if space.is_w(w_size, space.w_None): raise BlockingIOError() size = space.int_w(w_size) if size < 0 or size > length: raise oefmt( space.w_IOError, "raw readinto() returned invalid length %d (should " "have been between 0 and %d)", size, length) if self.abs_pos != -1: self.abs_pos += size return size
def buffer_w(self, space, flags): self.check_valid() readonly = (self.mmap.access == ACCESS_READ) write_required = bool(flags & space.BUF_WRITABLE) if write_required and readonly: raise oefmt(space.w_BufferError, "Object is not writable.") return SimpleView(MMapBuffer(self.space, self.mmap, readonly))
def test_frombuffer(self, space, api): w_view = SimpleView(StringBuffer("hello")).wrap(space) w_memoryview = api.PyMemoryView_FromObject(w_view) c_memoryview = rffi.cast(PyMemoryViewObject, make_ref(space, w_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)) decref(space, ref) decref(space, c_memoryview)
def getbuffer_w(self, space): self._check_closed(space) return SimpleView(BytesIOBuffer(self)).wrap(space)
def buffer_w(self, space, flags): return SimpleView(RawFFIBuffer(self))
def buffer_w(self, space, flags): return SimpleView(self.buffer)
def buffer_w(self, space, flags): return SimpleView(BytearrayBuffer(self))
def buffer_w(self, space, flags): return SimpleView(StringBuffer("foobar"))
def buffer_w(self, space, flags): space.check_buf_flags(flags, self.buf.readonly) return SimpleView(self.buf)
def buffer_w(self, space, flags): space.check_buf_flags(flags, True) return SimpleView(StringBuffer(self._value))
def bytebuffer(space, length): return SimpleView(ByteBuffer(length)).wrap(space)