def descr_init(self, space, w_raw, buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=-234): if max_buffer_size != -234: self._complain_about_max_buffer_size(space) self.state = STATE_ZERO check_readable_w(space, w_raw) check_writable_w(space, w_raw) check_seekable_w(space, w_raw) self.w_raw = w_raw self.buffer_size = buffer_size self.readable = self.writable = True self._init(space) self._reader_reset_buf() self._writer_reset_buf() self.pos = 0 self.state = STATE_OK
def descr_init(self, space, w_raw, buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size = -234): if max_buffer_size != -234: self._deprecated_max_buffer_size(space) self.state = STATE_ZERO check_readable_w(space, w_raw) check_writable_w(space, w_raw) check_seekable_w(space, w_raw) self.w_raw = w_raw self.buffer_size = buffer_size self.readable = self.writable = True self._init(space) self._reader_reset_buf() self._writer_reset_buf() self.pos = 0 self.state = STATE_OK
def seek_w(self, space, pos, whence=0): self._check_init(space) if whence not in (0, 1, 2): raise oefmt(space.w_ValueError, "whence must be between 0 and 2, not %d", whence) self._check_closed(space, "seek of closed file") check_seekable_w(space, self.w_raw) if whence != 2 and self.readable: # Check if seeking leaves us inside the current buffer, so as to # return quickly if possible. Also, we needn't take the lock in # this fast path. if self.abs_pos == -1: self._raw_tell(space) current = self.abs_pos available = self._readahead() if available > 0: if whence == 0: offset = pos - (current - self._raw_offset()) else: offset = pos if -self.pos <= offset <= available: newpos = self.pos + offset assert newpos >= 0 self.pos = newpos return space.wrap(current - available + offset) # Fallback: invoke raw seek() method and clear buffer with self.lock: if self.writable: self._writer_flush_unlocked(space) self._writer_reset_buf() if whence == 1: pos -= self._raw_offset() n = self._raw_seek(space, pos, whence) self.raw_pos = -1 if self.readable: self._reader_reset_buf() return space.wrap(n)