Commits

Tyler Wade committed 2eef951

Fix underlying list of bytearrays being reused inappropriately

  • Participants
  • Parent commits c686b0a

Comments (0)

Files changed (3)

File pypy/objspace/std/bytearrayobject.py

         return ''.join(self.data)
 
     def _new(self, value):
+        if value is self.data:
+            value = value[:]
         return W_BytearrayObject(value)
 
     def _new_from_buffer(self, buffer):

File pypy/objspace/std/stringmethods.py

         d = width - len(value)
         if d > 0:
             fillchar = self._multi_chr(fillchar[0])
-            value += d * fillchar
+            value = value + fillchar * d
 
         return self._new(value)
 

File pypy/objspace/std/test/test_bytearrayobject.py

         check(bytearray('abc').rstrip(memoryview('c')), 'ab')
         check(bytearray('aba').strip('a'), 'b')
 
+    def test_xjust_no_mutate(self):
+        # a previous regression
+        b = bytearray(b'')
+        assert b.ljust(1) == bytearray(b' ')
+        assert not len(b)
+
+        b2 = b.ljust(0)
+        b2 += b' '
+        assert not len(b)
+
+        b2 = b.rjust(0)
+        b2 += b' '
+        assert not len(b)
+
     def test_split(self):
         # methods that should return a sequence of bytearrays
         def check(result, expected):