Ejemplo n.º 1
0
    def readline_w(self, space, w_limit=None):
        self._check_closed(space)
        limit = convert_size(space, w_limit)

        if self.pos >= len(self.buf):
            return space.newunicode(u"")

        start = self.pos
        if limit < 0 or limit > len(self.buf) - self.pos:
            limit = len(self.buf) - self.pos

        assert limit >= 0
        end = start + limit

        endpos, consumed = self._find_line_ending(
            # XXX: super inefficient, makes a copy of the entire contents.
            u"".join(self.buf),
            start,
            end
        )
        if endpos >= 0:
            endpos += start
        else:
            endpos = end
        assert endpos >= 0
        self.pos = endpos
        return space.newunicode(u"".join(self.buf[start:endpos]))
Ejemplo n.º 2
0
    def readline_w(self, space, w_limit=None):
        self._check_closed(space)
        limit = convert_size(space, w_limit)

        if self.pos >= len(self.buf):
            return space.wrap(u"")

        start = self.pos
        if limit < 0 or limit > len(self.buf) - self.pos:
            limit = len(self.buf) - self.pos

        assert limit >= 0
        end = start + limit

        endpos, consumed = self._find_line_ending(
            # XXX: super inefficient, makes a copy of the entire contents.
            u"".join(self.buf),
            start,
            end
        )
        if endpos >= 0:
            endpos += start
        else:
            endpos = end
        assert endpos >= 0
        self.pos = endpos
        return space.wrap(u"".join(self.buf[start:endpos]))
Ejemplo n.º 3
0
 def readline_w(self, space, w_limit=None):
     self._check_attached(space)
     self._check_closed(space)
     self._writeflush(space)
     limit = convert_size(space, w_limit)
     text, lgt = self._readline(space, limit)
     return space.newutf8(text, lgt)
Ejemplo n.º 4
0
    def read_w(self, space, w_size=None):
        self._check_attached(space)
        self._check_closed(space)
        if not self.w_decoder:
            raise oefmt(space.w_IOError, "not readable")

        size = convert_size(space, w_size)
        self._writeflush(space)

        if size < 0:
            # Read everything
            w_bytes = space.call_method(self.w_buffer, "read")
            w_decoded = space.call_method(self.w_decoder, "decode", w_bytes,
                                          space.w_True)
            check_decoded(space, w_decoded)
            chars, lgt = self.decoded.get_chars(-1)
            w_result = space.newutf8(chars, lgt)
            w_final = space.add(w_result, w_decoded)
            self.decoded.reset()
            self.snapshot = None
            return w_final

        remaining = size
        builder = Utf8StringBuilder(size)

        # Keep reading chunks until we have n characters to return
        while remaining > 0:
            if not self._ensure_data(space):
                break
            data, size = self.decoded.get_chars(remaining)
            builder.append_utf8(data, size)
            remaining -= size

        return space.newutf8(builder.build(), builder.getlength())
Ejemplo n.º 5
0
    def readline_w(self, space, w_limit=None):
        self._check_attached(space)
        self._check_closed(space)
        self._writeflush(space)

        limit = convert_size(space, w_limit)
        remnant = None
        builder = StringBuilder()
        # XXX maybe use Utf8StringBuilder instead?
        while True:
            # First, get some data if necessary
            has_data = self._ensure_data(space)
            if not has_data:
                # end of file
                if remnant:
                    builder.append(remnant)
                break

            if remnant:
                assert not self.readtranslate and self.readnl == '\r\n'
                assert self.decoded.pos == 0
                if remnant == '\r' and self.decoded.text[0] == '\n':
                    builder.append('\r\n')
                    self.decoded.pos = 1
                    remnant = None
                    break
                else:
                    builder.append(remnant)
                    remnant = None
                    continue

            if limit >= 0:
                remaining = limit - builder.getlength()
                assert remaining >= 0
            else:
                remaining = -1
            start = self.decoded.pos
            assert start >= 0
            found = self._scan_line_ending(remaining)
            end_scan = self.decoded.pos
            if end_scan > start:
                s = self.decoded.text[start:end_scan]
                builder.append(s)

            if found or (limit >= 0 and builder.getlength() >= limit):
                break

            # There may be some remaining chars we'll have to prepend to the
            # next chunk of data
            if not self.decoded.exhausted():
                remnant = self.decoded.get_chars(-1)
            # We have consumed the buffer
            self.decoded.reset()

        result = builder.build()
        lgt = get_utf8_length(result)
        return space.newutf8(result, lgt)
Ejemplo n.º 6
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_readable(space)
        size = convert_size(space, w_size)

        if size < 0:
            return self.readall_w(space)

        try:
            s = os.read(self.fd, size)
        except OSError, e:
            raise wrap_oserror(space, e, exception_name='w_IOError')
Ejemplo n.º 7
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_readable(space)
        size = convert_size(space, w_size)

        if size < 0:
            return self.readall_w(space)

        try:
            s = os.read(self.fd, size)
        except OSError, e:
            raise wrap_oserror(space, e, exception_name="w_IOError")
Ejemplo n.º 8
0
    def read_w(self, space, w_size=None):
        self._check_attached(space)
        self._check_closed(space)
        if not self.w_decoder:
            self._unsupportedoperation(space, "not readable")

        size = convert_size(space, w_size)
        self._writeflush(space)

        if size < 0:
            return self._read_all(space)
        else:
            return self._read(space, size)
Ejemplo n.º 9
0
 def readline_w(self, space, w_limit=None):
     self._check_closed(space)
     limit = convert_size(space, w_limit)
     if self.readuniversal:
         result = self.buf.readline_universal(limit)
     else:
         if self.readtranslate:
             # Newlines are already translated, only search for \n
             newline = u'\n'
         else:
             newline = self.readnl
         result = self.buf.readline(newline, limit)
     return space.newunicode(result)
Ejemplo n.º 10
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        size = convert_size(space, w_size)

        # adjust invalid sizes
        available = self.string_size - self.pos
        if not 0 <= size <= available:
            size = available
            if size < 0:
                size = 0

        output = buffer2string(self.buf, self.pos, self.pos + size)
        self.pos += size
        return space.wrap(output)
Ejemplo n.º 11
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        size = convert_size(space, w_size)

        # adjust invalid sizes
        available = self.string_size - self.pos
        if not 0 <= size <= available:
            size = available
            if size < 0:
                size = 0

        output = buffer2string(self.buf, self.pos, self.pos + size)
        self.pos += size
        return space.wrap(output)
Ejemplo n.º 12
0
 def read_w(self, space, w_size=None):
     self._check_closed(space)
     size = convert_size(space, w_size)
     start = self.pos
     available = len(self.buf) - start
     if available <= 0:
         return space.newunicode(u"")
     if size >= 0 and size <= available:
         end = start + size
     else:
         end = len(self.buf)
     assert 0 <= start <= end
     self.pos = end
     return space.newunicode(u''.join(self.buf[start:end]))
Ejemplo n.º 13
0
 def read_w(self, space, w_size=None):
     self._check_closed(space)
     size = convert_size(space, w_size)
     start = self.pos
     available = len(self.buf) - start
     if available <= 0:
         return space.wrap(u"")
     if size >= 0 and size <= available:
         end = start + size
     else:
         end = len(self.buf)
     assert 0 <= start <= end
     self.pos = end
     return space.wrap(u''.join(self.buf[start:end]))
Ejemplo n.º 14
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_readable(space)
        size = convert_size(space, w_size)

        if size < 0:
            return self.readall_w(space)

        try:
            s = os.read(self.fd, size)
        except OSError, e:
            if e.errno == errno.EAGAIN:
                return space.w_None
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')
Ejemplo n.º 15
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_readable(space)
        size = convert_size(space, w_size)

        if size < 0:
            return self.readall_w(space)

        try:
            s = os.read(self.fd, size)
        except OSError as e:
            if e.errno == errno.EAGAIN:
                return space.w_None
            raise wrap_oserror(space, e, w_exception_class=space.w_IOError)

        return space.newbytes(s)
Ejemplo n.º 16
0
    def read_w(self, space, w_size=None):
        self._check_init(space)
        self._check_closed(space, "read of closed file")
        size = convert_size(space, w_size)

        if size == -1:
            # read until the end of stream
            with self.lock:
                return self._read_all(space)
        elif size >= 0:
            res = self._read_fast(size)
            if res is None:
                with self.lock:
                    res = self._read_generic(space, size)
        else:
            raise OperationError(space.w_ValueError, space.wrap(
                "read length must be positive or -1"))
        return space.wrap(res)
Ejemplo n.º 17
0
    def read_w(self, space, w_size=None):
        self._check_init(space)
        self._check_closed(space, "read of closed file")
        size = convert_size(space, w_size)

        if size == -1:
            # read until the end of stream
            with self.lock:
                return self._read_all(space)
        elif size >= 0:
            res = self._read_fast(size)
            if res is None:
                with self.lock:
                    res = self._read_generic(space, size)
        else:
            raise OperationError(space.w_ValueError, space.wrap(
                "read length must be positive or -1"))
        return space.wrap(res)
Ejemplo n.º 18
0
    def read_w(self, space, w_size=None):
        self._check_attached(space)
        self._check_closed(space)
        if not self.w_decoder:
            raise oefmt(space.w_IOError, "not readable")

        size = convert_size(space, w_size)
        self._writeflush(space)

        if size < 0:
            # Read everything
            w_bytes = space.call_method(self.w_buffer, "read")
            w_decoded = space.call_method(self.w_decoder, "decode", w_bytes,
                                          space.w_True)
            check_decoded(space, w_decoded)
            w_result = space.newunicode(self._get_decoded_chars(-1))
            w_final = space.add(w_result, w_decoded)
            self.snapshot = None
            return w_final

        remaining = size
        builder = UnicodeBuilder(size)

        # Keep reading chunks until we have n characters to return
        while True:
            data = self._get_decoded_chars(remaining)
            builder.append(data)
            remaining -= len(data)

            if remaining <= 0:  # Done
                break

            try:
                if not self._read_chunk(space):
                    # EOF
                    break
            except OperationError as e:
                if trap_eintr(space, e):
                    continue
                raise

        return space.newunicode(builder.build())
Ejemplo n.º 19
0
    def read_w(self, space, w_size=None):
        size = convert_size(space, w_size)
        if self.handle == rwin32.INVALID_HANDLE_VALUE:
            raise err_closed(space)
        if not self.readable:
            raise err_mode(space, "reading")

        if size < 0:
            return self.readall_w(space)

        if size > BUFMAX:
            raise oefmt(space.w_ValueError, "Cannot read more than %d bytes",
                        BUFMAX)

        # If self.mode is 'u', we want to return a unicode
        buf, length = self.read(space, size)
        if 1 or self.mode == 'u':
            return space.newtext(buf, length)
        else:
            return space.newbytes(buf)
Ejemplo n.º 20
0
    def read_w(self, space, w_size=None):
        self._check_attached(space)
        self._check_closed(space)
        if not self.w_decoder:
            raise oefmt(space.w_IOError, "not readable")

        size = convert_size(space, w_size)
        self._writeflush(space)

        if size < 0:
            # Read everything
            w_bytes = space.call_method(self.w_buffer, "read")
            w_decoded = space.call_method(self.w_decoder, "decode", w_bytes, space.w_True)
            check_decoded(space, w_decoded)
            w_result = space.wrap(self._get_decoded_chars(-1))
            w_final = space.add(w_result, w_decoded)
            self.snapshot = None
            return w_final

        remaining = size
        builder = UnicodeBuilder(size)

        # Keep reading chunks until we have n characters to return
        while True:
            data = self._get_decoded_chars(remaining)
            builder.append(data)
            remaining -= len(data)

            if remaining <= 0: # Done
                break

            try:
                if not self._read_chunk(space):
                    # EOF
                    break
            except OperationError as e:
                if trap_eintr(space, e):
                    continue
                raise

        return space.wrap(builder.build())
Ejemplo n.º 21
0
    def read_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_readable(space)
        size = convert_size(space, w_size)

        if size < 0:
            return self.readall_w(space)

        while True:
            try:
                s = os.read(self.fd, size)
                break
            except OSError as e:
                if e.errno == errno.EAGAIN:
                    return space.w_None
                wrap_oserror(space,
                             e,
                             exception_name='w_IOError',
                             eintr_retry=True)

        return space.newbytes(s)
Ejemplo n.º 22
0
 def readline_w(self, space, w_limit=None):
     self._check_closed(space)
     limit = convert_size(space, w_limit)
     return space.wrap(self.readline(limit))
Ejemplo n.º 23
0
 def read_w(self, space, w_size=None):
     self._check_closed(space)
     size = convert_size(space, w_size)
     return space.wrap(self.read(size))
Ejemplo n.º 24
0
    def readline_w(self, space, w_limit=None):
        self._check_init(space)
        self._check_closed(space, "readline of closed file")

        limit = convert_size(space, w_limit)

        # First, try to find a line in the buffer. This can run
        # unlocked because the calls to the C API are simple enough
        # that they can't trigger any thread switch.
        have = self._readahead()
        if limit >= 0 and have > limit:
            have = limit
        for pos in range(self.pos, self.pos+have):
            if self.buffer[pos] == '\n':
                break
        else:
            pos = -1
        if pos >= 0:
            w_res = space.wrap(''.join(self.buffer[self.pos:pos+1]))
            self.pos = pos + 1
            return w_res
        if have == limit:
            w_res = space.wrap(''.join(self.buffer[self.pos:self.pos+have]))
            self.pos += have
            return w_res

        written = 0
        with self.lock:
            # Now we try to get some more from the raw stream
            chunks = []
            if have > 0:
                chunks.extend(self.buffer[self.pos:self.pos+have])
                written += have
                self.pos += have
                if limit >= 0:
                    limit -= have
            if self.writable:
                self._flush_and_rewind_unlocked(space)

            while True:
                self._reader_reset_buf()
                have = self._fill_buffer(space)
                if have == 0:
                    break
                if limit >= 0 and have > limit:
                    have = limit
                pos = 0
                found = False
                while pos < have:
                    c = self.buffer[pos]
                    pos += 1
                    if c == '\n':
                        self.pos = pos
                        found = True
                        break
                chunks.extend(self.buffer[0:pos])
                if found:
                    break
                if have == limit:
                    self.pos = have
                    break
                written += have
                if limit >= 0:
                    limit -= have
            return space.wrap(''.join(chunks))
Ejemplo n.º 25
0
    def readline_w(self, space, w_limit=None):
        self._check_init(space)
        self._check_closed(space, "readline of closed file")

        limit = convert_size(space, w_limit)

        # First, try to find a line in the buffer. This can run
        # unlocked because the calls to the C API are simple enough
        # that they can't trigger any thread switch.
        have = self._readahead()
        if limit >= 0 and have > limit:
            have = limit
        for pos in range(self.pos, self.pos + have):
            if self.buffer[pos] == '\n':
                break
        else:
            pos = -1
        if pos >= 0:
            w_res = space.newbytes(self.buffer[self.pos:pos + 1])
            self.pos = pos + 1
            return w_res
        if have == limit:
            w_res = space.newbytes(self.buffer[self.pos:self.pos + have])
            self.pos += have
            return w_res

        written = 0
        with self.lock:
            # Now we try to get some more from the raw stream
            chunks = []
            if have > 0:
                chunks.append(self.buffer[self.pos:self.pos + have])
                written += have
                self.pos += have
                if limit >= 0:
                    limit -= have
            if self.writable:
                self._flush_and_rewind_unlocked(space)

            while True:
                self._reader_reset_buf()
                have = self._fill_buffer(space)
                if have == 0:
                    break
                if limit >= 0 and have > limit:
                    have = limit
                pos = 0
                found = False
                while pos < have:
                    # 'buffer.data[]' instead of 'buffer[]' because RPython...
                    c = self.buffer.data[pos]
                    pos += 1
                    if c == '\n':
                        self.pos = pos
                        found = True
                        break
                chunks.append(self.buffer[0:pos])
                if found:
                    break
                if have == limit:
                    self.pos = have
                    break
                written += have
                if limit >= 0:
                    limit -= have
            return space.newbytes(''.join(chunks))
Ejemplo n.º 26
0
 def read_w(self, space, w_size=None):
     self._check_closed(space)
     size = convert_size(space, w_size)
     v = self.buf.read(size)
     lgt = get_utf8_length(v)
     return space.newutf8(v, lgt)
Ejemplo n.º 27
0
 def read_w(self, space, w_size=None):
     self._check_closed(space)
     size = convert_size(space, w_size)
     return space.newbytes(self.read(size))
Ejemplo n.º 28
0
    def readline_w(self, space, w_limit=None):
        self._check_attached(space)
        self._check_closed(space)
        self._writeflush(space)

        limit = convert_size(space, w_limit)
        chunked = 0

        line = None
        remaining = None
        chunks = []

        while True:
            # First, get some data if necessary
            has_data = True
            while not self.decoded_chars:
                try:
                    if not self._read_chunk(space):
                        has_data = False
                        break
                except OperationError as e:
                    if trap_eintr(space, e):
                        continue
                    raise
            if not has_data:
                # end of file
                self._set_decoded_chars(None)
                self.snapshot = None
                start = endpos = offset_to_buffer = 0
                break

            if not remaining:
                line = self.decoded_chars
                start = self.decoded_chars_used
                offset_to_buffer = 0
            else:
                assert self.decoded_chars_used == 0
                line = remaining + self.decoded_chars
                start = 0
                offset_to_buffer = len(remaining)
                remaining = None

            line_len = len(line)
            endpos, consumed = self._find_line_ending(line, start, line_len)
            if endpos >= 0:
                endpos += start
                if limit >= 0 and endpos >= start + limit - chunked:
                    endpos = start + limit - chunked
                    assert endpos >= 0
                break
            assert consumed >= 0

            # We can put aside up to `endpos`
            endpos = consumed + start
            if limit >= 0 and endpos >= start + limit - chunked:
                # Didn't find line ending, but reached length limit
                endpos = start + limit - chunked
                assert endpos >= 0
                break

            # No line ending seen yet - put aside current data
            if endpos > start:
                s = line[start:endpos]
                chunks.append(s)
                chunked += len(s)
            # There may be some remaining bytes we'll have to prepend to the
            # next chunk of data
            if endpos < line_len:
                remaining = line[endpos:]
            line = None
            # We have consumed the buffer
            self._set_decoded_chars(None)

        if line:
            # Our line ends in the current buffer
            decoded_chars_used = endpos - offset_to_buffer
            assert decoded_chars_used >= 0
            self.decoded_chars_used = decoded_chars_used
            if start > 0 or endpos < len(line):
                line = line[start:endpos]
        if remaining:
            chunks.append(remaining)
            remaining = None
        if chunks:
            if line:
                chunks.append(line)
            line = u''.join(chunks)

        if line:
            return space.wrap(line)
        else:
            return space.wrap(u'')
Ejemplo n.º 29
0
    def readline_w(self, space, w_limit=None):
        self._check_attached(space)
        self._check_closed(space)
        self._writeflush(space)

        limit = convert_size(space, w_limit)
        chunked = 0

        line = None
        remaining = None
        chunks = []

        while True:
            # First, get some data if necessary
            has_data = True
            while not self.decoded_chars:
                try:
                    if not self._read_chunk(space):
                        has_data = False
                        break
                except OperationError as e:
                    if trap_eintr(space, e):
                        continue
                    raise
            if not has_data:
                # end of file
                self._set_decoded_chars(None)
                self.snapshot = None
                start = endpos = offset_to_buffer = 0
                break

            if not remaining:
                line = self.decoded_chars
                start = self.decoded_chars_used
                offset_to_buffer = 0
            else:
                assert self.decoded_chars_used == 0
                line = remaining + self.decoded_chars
                start = 0
                offset_to_buffer = len(remaining)
                remaining = None

            line_len = len(line)
            endpos, consumed = self._find_line_ending(line, start, line_len)
            if endpos >= 0:
                endpos += start
                if limit >= 0 and endpos >= start + limit - chunked:
                    endpos = start + limit - chunked
                    assert endpos >= 0
                break
            assert consumed >= 0

            # We can put aside up to `endpos`
            endpos = consumed + start
            if limit >= 0 and endpos >= start + limit - chunked:
                # Didn't find line ending, but reached length limit
                endpos = start + limit - chunked
                assert endpos >= 0
                break

            # No line ending seen yet - put aside current data
            if endpos > start:
                s = line[start:endpos]
                chunks.append(s)
                chunked += len(s)
            # There may be some remaining bytes we'll have to prepend to the
            # next chunk of data
            if endpos < line_len:
                remaining = line[endpos:]
            line = None
            # We have consumed the buffer
            self._set_decoded_chars(None)

        if line:
            # Our line ends in the current buffer
            decoded_chars_used = endpos - offset_to_buffer
            assert decoded_chars_used >= 0
            self.decoded_chars_used = decoded_chars_used
            if start > 0 or endpos < len(line):
                line = line[start:endpos]
        if remaining:
            chunks.append(remaining)
            remaining = None
        if chunks:
            if line:
                chunks.append(line)
            line = u''.join(chunks)

        if line:
            return space.newunicode(line)
        else:
            return space.newunicode(u'')
Ejemplo n.º 30
0
 def readline_w(self, space, w_limit=None):
     self._check_closed(space)
     limit = convert_size(space, w_limit)
     return space.newbytes(self.readline(limit))
Ejemplo n.º 31
0
 def read_w(self, space, w_size=None):
     self._check_closed(space)
     size = convert_size(space, w_size)
     v = self.buf.read(size)
     lgt = codepoints_in_utf8(v)
     return space.newutf8(v, lgt)