Beispiel #1
0
    def test_bytebuffer(self):
        from __pypy__ import bytebuffer
        b = bytebuffer(12)
        assert isinstance(b, buffer)
        assert len(b) == 12
        b[3] = '!'
        b[5] = '?'
        assert b[2:7] == '\x00!\x00?\x00'
        b[9:] = '+-*'
        assert b[-1] == '*'
        assert b[-2] == '-'
        assert b[-3] == '+'
        exc = raises(TypeError, "b[3] = 'abc'")
        assert str(exc.value) == "right operand must be a single byte"
        exc = raises(TypeError, "b[3:5] = 'abc'")
        assert str(exc.value) == "right operand length must match slice length"
        exc = raises(TypeError, "b[3:7:2] = 'abc'")
        assert str(exc.value) == "right operand length must match slice length"

        b = bytebuffer(10)
        b[1:3] = 'xy'
        assert str(b) == "\x00xy" + "\x00" * 7
        b[4:8:2] = 'zw'
        assert str(b) == "\x00xy\x00z\x00w" + "\x00" * 3
        r = str(buffer(u'#'))
        b[6:6 + len(r)] = u'#'
        assert str(b[:6 + len(r)]) == "\x00xy\x00z\x00" + r
Beispiel #2
0
 def __delitem__(self, i):
     if isinstance(i, slice):
         seqlength = len(self)
         start, stop, step = i.indices(seqlength)
         if start < 0:
             start = 0
         if stop < start:
             stop = start
         assert stop <= seqlength
         if step != 1:
             sublist = self.tolist()    # fall-back
             del sublist[i]
             self._clear()
             self.fromlist(sublist)
             return
         dellength = stop - start
         boundary1 = start * self.itemsize
         boundary2 = stop * self.itemsize
         newdata = bytebuffer(len(self._data) - (boundary2-boundary1))
         newdata[:boundary1] = self._data[:boundary1]
         newdata[boundary1:] = self._data[boundary2:]
         self._data = newdata
     else:            
         seqlength = len(self)
         if i < 0:
             i += seqlength
         if not (0 <= i < seqlength):
             raise IndexError(i)
         boundary = i * self.itemsize
         newdata = bytebuffer(len(self._data) - self.itemsize)
         newdata[:boundary] = self._data[:boundary]
         newdata[boundary:] = self._data[boundary+self.itemsize:]
         self._data = newdata
Beispiel #3
0
 def __delitem__(self, i):
     if isinstance(i, slice):
         seqlength = len(self)
         start, stop, step = i.indices(seqlength)
         if start < 0:
             start = 0
         if stop < start:
             stop = start
         assert stop <= seqlength
         if step != 1:
             sublist = self.tolist()  # fall-back
             del sublist[i]
             self._clear()
             self.fromlist(sublist)
             return
         dellength = stop - start
         boundary1 = start * self.itemsize
         boundary2 = stop * self.itemsize
         newdata = bytebuffer(len(self._data) - (boundary2 - boundary1))
         newdata[:boundary1] = self._data[:boundary1]
         newdata[boundary1:] = self._data[boundary2:]
         self._data = newdata
     else:
         seqlength = len(self)
         if i < 0:
             i += seqlength
         if not (0 <= i < seqlength):
             raise IndexError(i)
         boundary = i * self.itemsize
         newdata = bytebuffer(len(self._data) - self.itemsize)
         newdata[:boundary] = self._data[:boundary]
         newdata[boundary:] = self._data[boundary + self.itemsize:]
         self._data = newdata
Beispiel #4
0
    def test_bytebuffer(self):
        from __pypy__ import bytebuffer
        b = bytebuffer(12)
        assert isinstance(b, buffer)
        assert len(b) == 12
        b[3] = '!'
        b[5] = '?'
        assert b[2:7] == '\x00!\x00?\x00'
        b[9:] = '+-*'
        assert b[-1] == '*'
        assert b[-2] == '-'
        assert b[-3] == '+'
        exc = raises(TypeError, "b[3] = 'abc'")
        assert str(exc.value) == "right operand must be a single byte"
        exc = raises(TypeError, "b[3:5] = 'abc'")
        assert str(exc.value) == "right operand length must match slice length"
        exc = raises(TypeError, "b[3:7:2] = 'abc'")
        assert str(exc.value) == "right operand length must match slice length"

        b = bytebuffer(10)
        b[1:3] = 'xy'
        assert str(b) == "\x00xy" + "\x00" * 7
        b[4:8:2] = 'zw'
        assert str(b) == "\x00xy\x00z\x00w" + "\x00" * 3
        r = str(buffer(u'#'))
        b[6:6+len(r)] = u'#'
        assert str(b[:6+len(r)]) == "\x00xy\x00z\x00" + r
Beispiel #5
0
def test_unpack_from():
    b = bytebuffer(19)
    sz = struct.calcsize("ii")
    b[2:2+sz] = struct.pack("ii", 17, 42)
    assert struct.unpack_from("ii", b, 2) == (17, 42)
    b[:sz] = struct.pack("ii", 18, 43)
    assert struct.unpack_from("ii", b) == (18, 43)
Beispiel #6
0
 def _fromiterable(self, iterable):
     iterable = tuple(iterable)
     n = len(iterable)
     boundary = len(self._data)
     newdata = bytebuffer(boundary + n * self.itemsize)
     newdata[:boundary] = self._data
     pack_into('%d%s' % (n, self.typecode), newdata, boundary, *iterable)
     self._data = newdata
Beispiel #7
0
 def _fromiterable(self, iterable):
     iterable = tuple(iterable)
     n = len(iterable)
     boundary = len(self._data)
     newdata = bytebuffer(boundary + n * self.itemsize)
     newdata[:boundary] = self._data
     pack_into('%d%s' % (n, self.typecode), newdata, boundary, *iterable)
     self._data = newdata
Beispiel #8
0
 def _frombuffer(self, s):
     length = len(s)
     if length % self.itemsize != 0:
         raise ValueError("string length not a multiple of item size")
     boundary = len(self._data)
     newdata = bytebuffer(boundary + length)
     newdata[:boundary] = self._data
     newdata[boundary:] = s
     self._data = newdata
Beispiel #9
0
 def _frombuffer(self, s):
     length = len(s)
     if length % self.itemsize != 0:
         raise ValueError("string length not a multiple of item size")
     boundary = len(self._data)
     newdata = bytebuffer(boundary + length)
     newdata[:boundary] = self._data
     newdata[boundary:] = s
     self._data = newdata
Beispiel #10
0
def test_pack_into():
    b = bytebuffer(19)
    sz = struct.calcsize("ii")
    struct.pack_into("ii", b, 2, 17, 42)
    assert b[:] == (b'\x00' * 2 +
                    struct.pack("ii", 17, 42) +
                    b'\x00' * (19-sz-2))
    m = memoryview(b)
    struct.pack_into("ii", m, 2, 17, 42)
Beispiel #11
0
    def test_bytebuffer(self):
        from __pypy__ import bytebuffer
        b = bytebuffer(12)
        assert len(b) == 12
        b[3] = ord(b'!')
        b[5] = ord(b'?')
        assert b[2:7] == b'\x00!\x00?\x00'
        b[9:] = b'+-*'
        assert b[-1] == ord(b'*')
        assert b[-2] == ord(b'-')
        assert b[-3] == ord(b'+')
        exc = raises(ValueError, "b[3:5] = b'abc'")
        assert str(exc.value) == "cannot modify size of memoryview object"

        b = bytebuffer(10)
        b[1:3] = b'xy'
        assert bytes(b) == b"\x00xy" + b"\x00" * 7
        b[4:8:2] = b'zw'
        assert bytes(b) == b"\x00xy\x00z\x00w" + b"\x00" * 3
Beispiel #12
0
 def test_bytebuffer(self):
     from __pypy__ import bytebuffer
     b = bytebuffer(12)
     assert isinstance(b, buffer)
     assert len(b) == 12
     b[3] = '!'
     b[5] = '?'
     assert b[2:7] == '\x00!\x00?\x00'
     b[9:] = '+-*'
     assert b[-1] == '*'
     assert b[-2] == '-'
     assert b[-3] == '+'
Beispiel #13
0
 def test_bytebuffer(self):
     from __pypy__ import bytebuffer
     b = bytebuffer(12)
     assert isinstance(b, buffer)
     assert len(b) == 12
     b[3] = '!'
     b[5] = '?'
     assert b[2:7] == '\x00!\x00?\x00'
     b[9:] = '+-*'
     assert b[-1] == '*'
     assert b[-2] == '-'
     assert b[-3] == '+'
Beispiel #14
0
    def test_bytebuffer(self):
        from __pypy__ import bytebuffer
        b = bytebuffer(12)
        assert len(b) == 12
        b[3] = b'!'
        b[5] = b'?'
        assert b[2:7] == b'\x00!\x00?\x00'
        b[9:] = b'+-*'
        assert b[-1] == b'*'
        assert b[-2] == b'-'
        assert b[-3] == b'+'
        exc = raises(ValueError, "b[3] = b'abc'")
        assert str(exc.value) == "cannot modify size of memoryview object"
        exc = raises(ValueError, "b[3:5] = b'abc'")
        assert str(exc.value) == "cannot modify size of memoryview object"
        raises(NotImplementedError, "b[3:7:2] = b'abc'")

        b = bytebuffer(10)
        b[1:3] = b'xy'
        assert bytes(b) == b"\x00xy" + b"\x00" * 7
        # XXX: supported in 3.3
        raises(NotImplementedError, "b[4:8:2] = b'zw'")
Beispiel #15
0
 def pop(self, i=-1):
     """Return the i-th element and delete it from the array. i defaults to
     -1."""
     seqlength = len(self)
     if i < 0:
         i += seqlength
     if not (0 <= i < seqlength):
         raise IndexError(i)
     boundary = i * self.itemsize
     result = unpack_from(self.typecode, self._data, boundary)[0]
     newdata = bytebuffer(len(self._data) - self.itemsize)
     newdata[:boundary] = self._data[:boundary]
     newdata[boundary:] = self._data[boundary + self.itemsize:]
     self._data = newdata
     return result
Beispiel #16
0
 def pop(self, i=-1):
     """Return the i-th element and delete it from the array. i defaults to
     -1."""
     seqlength = len(self)
     if i < 0:
         i += seqlength
     if not (0 <= i < seqlength):
         raise IndexError(i)
     boundary = i * self.itemsize
     result = unpack_from(self.typecode, self._data, boundary)[0]
     newdata = bytebuffer(len(self._data) - self.itemsize)
     newdata[:boundary] = self._data[:boundary]
     newdata[boundary:] = self._data[boundary+self.itemsize:]
     self._data = newdata
     return result
Beispiel #17
0
 def insert(self, i, x):
     """Insert a new item x into the array before position i."""
     seqlength = len(self)
     if i < 0:
         i += seqlength
         if i < 0:
             i = 0
     elif i > seqlength:
         i = seqlength
     boundary = i * self.itemsize
     data = pack(self.typecode, x)
     newdata = bytebuffer(len(self._data) + len(data))
     newdata[:boundary] = self._data[:boundary]
     newdata[boundary:boundary + self.itemsize] = data
     newdata[boundary + self.itemsize:] = self._data[boundary:]
     self._data = newdata
Beispiel #18
0
 def insert(self, i, x):
     """Insert a new item x into the array before position i."""
     seqlength = len(self)
     if i < 0:
         i += seqlength
         if i < 0:
             i = 0
     elif i > seqlength:
         i = seqlength
     boundary = i * self.itemsize
     data = pack(self.typecode, x)
     newdata = bytebuffer(len(self._data) + len(data))
     newdata[:boundary] = self._data[:boundary]
     newdata[boundary:boundary+self.itemsize] = data
     newdata[boundary+self.itemsize:] = self._data[boundary:]
     self._data = newdata
Beispiel #19
0
 def __new__(cls, typecode, initializer=[]):
     self = object.__new__(cls)
     if not isinstance(typecode, str) or len(typecode) != 1:
         raise TypeError("array() argument 1 must be char, not %s" %
                         type(typecode))
     if typecode not in TYPECODES:
         raise ValueError("bad typecode (must be one of %s)" %
                          ', '.join(TYPECODES))
     self._data = bytebuffer(0)
     self.typecode = typecode
     self.itemsize = calcsize(typecode)
     if isinstance(initializer, list):
         self.fromlist(initializer)
     elif isinstance(initializer, str):
         self.fromstring(initializer)
     elif isinstance(initializer, unicode) and self.typecode == "u":
         self.fromunicode(initializer)
     else:
         self.extend(initializer)
     return self
Beispiel #20
0
 def __setitem__(self, i, x):
     if isinstance(i, slice):
         if (not isinstance(x, array)
             or self.typecode != x.typecode):
             raise TypeError("can only assign array of same kind"
                             " to array slice")
         seqlength = len(self)
         start, stop, step = i.indices(seqlength)
         if step != 1:
             sublist = self.tolist()    # fall-back
             sublist[i] = x.tolist()
             self._clear()
             self.fromlist(sublist)
             return
         if start < 0:
             start = 0
         if stop < start:
             stop = start
         assert stop <= seqlength
         boundary1 = start * self.itemsize
         boundary2 = stop * self.itemsize
         boundary2new = boundary1 + len(x._data)
         if boundary2 == boundary2new:
             self._data[boundary1:boundary2] = x._data
         else:
             newdata = bytebuffer(len(self._data) + boundary2new-boundary2)
             newdata[:boundary1] = self._data[:boundary1]
             newdata[boundary1:boundary2new] = x._data
             newdata[boundary2new:] = self._data[boundary2:]
             self._data = newdata
     else:
         seqlength = len(self)
         if i < 0:
             i += seqlength
         if self.typecode == 'c':  # speed trick
             self._data[i] = x
             return
         if not (0 <= i < seqlength):
             raise IndexError(i)
         boundary = i * self.itemsize
         pack_into(self.typecode, self._data, boundary, x)
Beispiel #21
0
 def __setitem__(self, i, x):
     if isinstance(i, slice):
         if (not isinstance(x, array) or self.typecode != x.typecode):
             raise TypeError("can only assign array of same kind"
                             " to array slice")
         seqlength = len(self)
         start, stop, step = i.indices(seqlength)
         if step != 1:
             sublist = self.tolist()  # fall-back
             sublist[i] = x.tolist()
             self._clear()
             self.fromlist(sublist)
             return
         if start < 0:
             start = 0
         if stop < start:
             stop = start
         assert stop <= seqlength
         boundary1 = start * self.itemsize
         boundary2 = stop * self.itemsize
         boundary2new = boundary1 + len(x._data)
         if boundary2 == boundary2new:
             self._data[boundary1:boundary2] = x._data
         else:
             newdata = bytebuffer(
                 len(self._data) + boundary2new - boundary2)
             newdata[:boundary1] = self._data[:boundary1]
             newdata[boundary1:boundary2new] = x._data
             newdata[boundary2new:] = self._data[boundary2:]
             self._data = newdata
     else:
         seqlength = len(self)
         if i < 0:
             i += seqlength
         if self.typecode == 'c':  # speed trick
             self._data[i] = x
             return
         if not (0 <= i < seqlength):
             raise IndexError(i)
         boundary = i * self.itemsize
         pack_into(self.typecode, self._data, boundary, x)
Beispiel #22
0
 def __new__(cls, typecode, initializer=[], **extrakwds):
     self = object.__new__(cls)
     if cls is array and extrakwds:
         raise TypeError("array() does not take keyword arguments")
     if not isinstance(typecode, str) or len(typecode) != 1:
         raise TypeError(
                  "array() argument 1 must be char, not %s" % type(typecode))
     if typecode not in TYPECODES:
         raise ValueError(
               "bad typecode (must be one of %s)" % ', '.join(TYPECODES))
     self._data = bytebuffer(0)
     self.typecode = typecode
     self.itemsize = calcsize(typecode)
     if isinstance(initializer, list):
         self.fromlist(initializer)
     elif isinstance(initializer, str):
         self.fromstring(initializer)
     elif isinstance(initializer, unicode) and self.typecode == "u":
         self.fromunicode(initializer)
     else:
         self.extend(initializer)
     return self
Beispiel #23
0
 def __imul__(self, repeat):
     newdata = buffer(self._data) * repeat
     self._data = bytebuffer(len(newdata))
     self._data[:] = newdata
     return self
Beispiel #24
0
 def test_buffer_getslice_empty(self):
     from __pypy__ import bytebuffer
     b = bytebuffer(10)
     assert b[1:0] == b''
Beispiel #25
0
 def _clear(self):
     self._data = bytebuffer(0)
Beispiel #26
0
 def _clear(self):
     self._data = bytebuffer(0)
Beispiel #27
0
 def __copy__(self):
     a = array(self.typecode)
     a._data = bytebuffer(len(self._data))
     a._data[:] = self._data
     return a
Beispiel #28
0
 def __copy__(self):
     a = array(self.typecode)
     a._data = bytebuffer(len(self._data))
     a._data[:] = self._data
     return a
Beispiel #29
0
 def __imul__(self, repeat):
     newdata = buffer(self._data) * repeat
     self._data = bytebuffer(len(newdata))
     self._data[:] = newdata
     return self