Example #1
0
    def descr_init(self, space, w_source=None, encoding=None, errors=None):
        if w_source is None:
            w_source = space.newbytes('')
        if encoding is not None:
            from pypy.objspace.std.unicodeobject import encode_object
            # if w_source is an integer this correctly raises a
            # TypeError the CPython error message is: "encoding or
            # errors without a string argument" ours is: "expected
            # unicode, got int object"
            w_source = encode_object(space, w_source, encoding, errors)

        # Is it an integer?
        # Note that we're calling space.getindex_w() instead of space.int_w().
        try:
            count = space.getindex_w(w_source, space.w_OverflowError)
        except OperationError as e:
            if not e.match(space, space.w_TypeError):
                raise
        else:
            if count < 0:
                raise oefmt(space.w_ValueError, "bytearray negative count")
            self._data = resizable_list_supporting_raw_ptr(['\0'] * count)
            self._offset = 0
            return

        data = makebytearraydata_w(space, w_source)
        self._data = resizable_list_supporting_raw_ptr(data)
        self._offset = 0
        _tweak_for_tests(self)
Example #2
0
    def descr_init(self, space, w_source=None, encoding=None, errors=None):
        if w_source is None:
            w_source = space.wrap('')
        if encoding is not None:
            from pypy.objspace.std.unicodeobject import encode_object
            # if w_source is an integer this correctly raises a
            # TypeError the CPython error message is: "encoding or
            # errors without a string argument" ours is: "expected
            # unicode, got int object"
            w_source = encode_object(space, w_source, encoding, errors)

        # Is it an int?
        try:
            count = space.int_w(w_source)
        except OperationError as e:
            if not e.match(space, space.w_TypeError):
                raise
        else:
            if count < 0:
                raise oefmt(space.w_ValueError, "bytearray negative count")
            self.data = resizable_list_supporting_raw_ptr(['\0'] * count)
            return

        data = makebytearraydata_w(space, w_source)
        self.data = resizable_list_supporting_raw_ptr(data)
Example #3
0
 def __init__(self, data):
     check_list_of_chars(data)
     self._data = resizable_list_supporting_raw_ptr(data)
     self._offset = 0
     # NOTE: the bytearray data is in 'self._data[self._offset:]'
     check_nonneg(self._offset)
     _tweak_for_tests(self)
Example #4
0
 def f(n):
     lst = ['a', 'b', 'c']
     lst = rgc.resizable_list_supporting_raw_ptr(lst)
     lst.append(chr(n))
     assert lst[3] == chr(n)
     assert lst[-1] == chr(n)
     #
     ptr = rgc.nonmoving_raw_ptr_for_resizable_list(lst)
     assert lst[:] == ['a', 'b', 'c', chr(n)]
     assert lltype.typeOf(ptr) == rffi.CCHARP
     assert [ptr[i] for i in range(4)] == ['a', 'b', 'c', chr(n)]
     #
     lst[-3] = 'X'
     assert ptr[1] == 'X'
     ptr[2] = 'Y'
     assert lst[-2] == 'Y'
     #
     addr = rffi.cast(lltype.Signed, ptr)
     ptr = rffi.cast(rffi.CCHARP, addr)
     rgc.collect()    # should not move lst.items
     lst[-4] = 'g'
     assert ptr[0] == 'g'
     ptr[3] = 'H'
     assert lst[-1] == 'H'
     return lst
Example #5
0
def newlist_and_gc_store(TYPE, value):
    size = rffi.sizeof(TYPE)
    lst = resizable_list_supporting_raw_ptr(['\x00'] * size)
    ll_data = ll_for_resizable_list(lst)
    ll_items = ll_data.items
    LIST = lltype.typeOf(ll_data).TO  # rlist.LIST_OF(lltype.Char)
    base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0)
    scale_factor = llmemory.sizeof(lltype.Char)
    value = lltype.cast_primitive(TYPE, value)
    llop.gc_store_indexed(lltype.Void, ll_items, 0, value, scale_factor,
                          base_ofs)
    return lst
Example #6
0
 def f(n):
     lst = ['a', 'b', 'c', 'd', 'e']
     lst = rgc.resizable_list_supporting_raw_ptr(lst)
     lst = lst[:3]
     lst.append(chr(n))
     assert lst[3] == chr(n)
     assert lst[-1] == chr(n)
     #
     ptr = rgc.nonmoving_raw_ptr_for_resizable_list(lst)
     assert lst[:] == ['a', 'b', 'c', chr(n)]
     assert lltype.typeOf(ptr) == rffi.CCHARP
     assert [ptr[i] for i in range(4)] == ['a', 'b', 'c', chr(n)]
     return lst
Example #7
0
    def _init(self, space):
        if self.buffer_size <= 0:
            raise oefmt(space.w_ValueError,
                        "buffer size must be strictly positive")

        self.buffer = resizable_list_supporting_raw_ptr(['\0'] *
                                                        self.buffer_size)

        self.lock = TryLock(space)

        try:
            self._raw_tell(space)
        except OperationError:
            pass
Example #8
0
 def f(n):
     lst = ['a', 'b', 'c']
     lst = rgc.resizable_list_supporting_raw_ptr(lst)
     lst.append(chr(n))
     assert lst[3] == chr(n)
     assert lst[-1] == chr(n)
     #
     ll_list = rgc.ll_for_resizable_list(lst)
     assert lst[:] == ['a', 'b', 'c', chr(n)]
     assert ll_list.length == 4
     assert [ll_list.items[i] for i in range(4)] == ['a', 'b', 'c', chr(n)]
     #
     lst[-3] = 'X'
     assert ll_list.items[1] == 'X'
     ll_list.items[2] = 'Y'
     assert lst[-2] == 'Y'
     #
     return lst
Example #9
0
 def descr_init(self, space, w_source=None, encoding=None, errors=None):
     assert isinstance(self, W_BytearrayObject)
     data = [c for c in newbytesdata_w(space, w_source, encoding, errors)]
     self._data = resizable_list_supporting_raw_ptr(data)
     self._offset = 0
     _tweak_for_tests(self)
Example #10
0
def test_ListSupportingRawPtr_direct():
    lst = ['a', 'b', 'c']
    lst = rgc.resizable_list_supporting_raw_ptr(lst)

    def check_nonresizing():
        assert lst[1] == lst[-2] == 'b'
        lst[1] = 'X'
        assert lst[1] == 'X'
        lst[-1] = 'Y'
        assert lst[1:3] == ['X', 'Y']
        assert lst[-2:9] == ['X', 'Y']
        lst[1:2] = 'B'
        assert lst[:] == ['a', 'B', 'Y']
        assert list(iter(lst)) == ['a', 'B', 'Y']
        assert list(reversed(lst)) == ['Y', 'B', 'a']
        assert 'B' in lst
        assert 'b' not in lst
        assert p[0] == 'a'
        assert p[1] == 'B'
        assert p[2] == 'Y'
        assert lst + ['*'] == ['a', 'B', 'Y', '*']
        assert ['*'] + lst == ['*', 'a', 'B', 'Y']
        assert lst + lst == ['a', 'B', 'Y', 'a', 'B', 'Y']
        base = ['8']
        base += lst
        assert base == ['8', 'a', 'B', 'Y']
        assert lst == ['a', 'B', 'Y']
        assert ['a', 'B', 'Y'] == lst
        assert ['a', 'B', 'Z'] != lst
        assert ['a', 'B', 'Z'] >  lst
        assert ['a', 'B', 'Z'] >= lst
        assert lst * 2 == ['a', 'B', 'Y', 'a', 'B', 'Y']
        assert 2 * lst == ['a', 'B', 'Y', 'a', 'B', 'Y']
        assert lst.count('B') == 1
        assert lst.index('Y') == 2
        lst.reverse()
        assert lst == ['Y', 'B', 'a']
        lst.sort()
        assert lst == ['B', 'Y', 'a']
        lst.sort(reverse=True)
        assert lst == ['a', 'Y', 'B']
        lst[1] = 'b'
        lst[2] = 'c'
        assert list(lst) == ['a', 'b', 'c']

    p = lst
    check_nonresizing()
    assert lst._raw_items is None
    lst._nonmoving_raw_ptr_for_resizable_list()
    p = lst._raw_items
    check_nonresizing()
    assert lst._raw_items == p
    assert p[0] == 'a'
    assert p[1] == 'b'
    assert p[2] == 'c'

    def do_resizing_operation():
        del lst[1]
        yield ['a', 'c']

        lst[:2] = ['X']
        yield ['X', 'c']

        del lst[:2]
        yield ['c']

        x = lst
        x += ['t']
        yield ['a', 'b', 'c', 't']

        x = lst
        x *= 3
        yield ['a', 'b', 'c'] * 3

        lst.append('f')
        yield ['a', 'b', 'c', 'f']

        lst.extend('fg')
        yield ['a', 'b', 'c', 'f', 'g']

        lst.insert(1, 'k')
        yield ['a', 'k', 'b', 'c']

        n = lst.pop(1)
        assert n == 'b'
        yield ['a', 'c']

        lst.remove('c')
        yield ['a', 'b']

    assert lst == ['a', 'b', 'c']
    for expect in do_resizing_operation():
        assert lst == expect
        assert lst._raw_items is None
        lst = ['a', 'b', 'c']
        lst = rgc.resizable_list_supporting_raw_ptr(lst)
        lst._nonmoving_raw_ptr_for_resizable_list()
Example #11
0
 def __init__(self, n):
     self.data = resizable_list_supporting_raw_ptr(['\0'] * n)
     self.readonly = False
Example #12
0
    def _read_generic(self, space, n):
        """Generic read function: read from the stream until enough bytes are
           read, or until an EOF occurs or until read() would block."""
        # Must run with the lock held!
        current_size = self._readahead()
        if n <= current_size:
            return self._read_fast(n)

        result_buffer = resizable_list_supporting_raw_ptr(['\0'] * n)
        remaining = n
        written = 0
        if current_size:
            for i in range(current_size):
                result_buffer[written + i] = self.buffer[self.pos + i]
            remaining -= current_size
            written += current_size
            self.pos += current_size

        # Flush the write buffer if necessary
        if self.writable:
            self._flush_and_rewind_unlocked(space)
        self._reader_reset_buf()

        # Read whole blocks, and don't buffer them
        while remaining > 0:
            r = self.buffer_size * (remaining // self.buffer_size)
            if r == 0:
                break
            try:
                size = self._raw_read(space, result_buffer, written, r)
            except BlockingIOError:
                if written == 0:
                    return None
                size = 0
            if size == 0:
                return ''.join(result_buffer[:written])
            remaining -= size
            written += size

        self.pos = 0
        self.raw_pos = 0
        self.read_end = 0

        while remaining > 0 and self.read_end < self.buffer_size:
            try:
                size = self._fill_buffer(space)
            except BlockingIOError:
                # EOF or read() would block
                if written == 0:
                    return None
                size = 0
            if size == 0:
                break

            if remaining > 0:
                if size > remaining:
                    size = remaining
                for i in range(size):
                    result_buffer[written + i] = self.buffer[self.pos + i]
                self.pos += size

                written += size
                remaining -= size

        return ''.join(result_buffer[:written])
Example #13
0
 def __init__(self, data):
     check_list_of_chars(data)
     self.data = resizable_list_supporting_raw_ptr(data)
Example #14
0
 def __init__(self, data):
     check_list_of_chars(data)
     self.data = resizable_list_supporting_raw_ptr(data)