[pypy-commit] pypy py3.5-bytearray: More tests

arigo pypy.commits at gmail.com
Sat Dec 3 06:40:35 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5-bytearray
Changeset: r88845:4af53ab59362
Date: 2016-12-03 12:39 +0100
http://bitbucket.org/pypy/pypy/changeset/4af53ab59362/

Log:	More tests

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -214,14 +214,15 @@
         _tweak_for_tests(self)
 
     def descr_repr(self, space):
-        s = self.getdata()
+        s, start, end, _ = self._convert_idx_params(space, None, None)
 
         # Good default if there are no replacements.
-        buf = StringBuilder(len("bytearray(b'')") + len(s))
+        buf = StringBuilder(len("bytearray(b'')") + (end - start))
 
         buf.append("bytearray(b")
         quote = "'"
-        for c in s:
+        for i in range(start, end):
+            c = s[i]
             if c == '"':
                 quote = "'"
                 break
@@ -229,7 +230,7 @@
                 quote = '"'
         buf.append(quote)
 
-        for i in range(len(s)):
+        for i in range(start, end):
             c = s[i]
 
             if c == '\\' or c == "'":
@@ -399,8 +400,8 @@
             if start == 0 and step == 1:
                 self._delete_from_start(slicelength)
             else:
-                _delitem_slice_helper(space, self.getdata(),
-                                      start, step, slicelength)
+                _delitem_slice_helper(space, self._data,
+                                      start + self._offset, step, slicelength)
         else:
             idx = space.getindex_w(w_idx, space.w_IndexError, "bytearray")
             idx = self._fixindex(space, idx)
@@ -411,7 +412,7 @@
 
     def _delete_from_start(self, n):
         self._offset += n
-        jit.conditional_call(self._offset > len(self._data) / 2 + 15,
+        jit.conditional_call(self._offset > len(self._data) / 2,
                              self._shrink_after_delete_from_start)
 
     def _shrink_after_delete_from_start(self):
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -632,17 +632,13 @@
     def test_dont_force_offset(self):
         def make(x=b'abcdefghij', shift=3):
             b = bytearray(b'?'*shift + x)
-            repr(b)                       # force 'b'
+            b + b''                       # force 'b'
             del b[:shift]                 # add shift to b._offset
             return b
         assert make(shift=0).__alloc__() == 11
         #
         x = make(shift=3)
         assert x.__alloc__() == 14
-        repr(x) 
-        assert x.__alloc__() == 11
-        #
-        x = make(shift=3)
         assert memoryview(x)[1] == ord('b')
         assert x.__alloc__() == 14
         assert len(x) == 10
@@ -653,6 +649,8 @@
         assert x[3:-3] == b'defg'
         assert x[-3:3:-1] == b'hgfe'
         assert x.__alloc__() == 14
+        assert repr(x) == "bytearray(b'abcdefghij')"
+        assert x.__alloc__() == 14
         #
         x = make(shift=3)
         x[3] = ord('D')
@@ -665,15 +663,31 @@
         assert x.__alloc__() == 13
         assert x == bytearray(b'abcDE/G*j')
         #
-        x = make(shift=3)
-        assert x.__alloc__() == 14
+        x = make(b'abcdefghijklmnopqrstuvwxyz', shift=11)
+        assert len(x) == 26
+        assert x.__alloc__() == 38
         del x[:1]
-        assert x.__alloc__() == 13
+        assert len(x) == 25
+        assert x.__alloc__() == 38
         del x[0:5]
-        assert x.__alloc__() == 8
+        assert len(x) == 20
+        assert x.__alloc__() == 38
         del x[0]
-        assert len(x) == 4
-        assert x.__alloc__() == 7
+        assert len(x) == 19
+        assert x.__alloc__() == 38
+        del x[0]                      # too much emptiness, forces now
+        assert len(x) == 18
+        assert x.__alloc__() == 19
+        #
+        x = make(b'abcdefghijklmnopqrstuvwxyz', shift=11)
+        del x[:9]                     # too much emptiness, forces now
+        assert len(x) == 17
+        assert x.__alloc__() == 18
+        #
+        x = make(b'abcdefghijklmnopqrstuvwxyz', shift=11)
+        assert x.__alloc__() == 38
         del x[1]
-        assert x.__alloc__() == 4      # forced
-        assert x == bytearray(b'gij')
+        assert x.__alloc__() == 37      # not forced, but the list shrank
+        del x[3:10:2]
+        assert x.__alloc__() == 33
+        assert x == bytearray(b'acdfhjlmnopqrstuvwxyz')


More information about the pypy-commit mailing list