1. Pypy
  2. Untitled project
  3. pypy

Commits

Manuel Jacob  committed 6c6d253

Don't share an empty bytearray.

  • Participants
  • Parent commits d6b1adc
  • Branches refactor-str-types

Comments (0)

Files changed (4)

File pypy/objspace/std/bytearrayobject.py

View file
  • Ignore whitespace
     def _new(self, value):
         return W_BytearrayObject(_make_data(value))
 
+    def _new_from_list(self, value):
+        return W_BytearrayObject(value)
+
+    def _empty(self):
+        return W_BytearrayObject([])
+
     def _len(self):
         return len(self.data)
 
         assert len(char) == 1
         return str(char)[0]
 
-    _empty = ''
     _builder = StringBuilder
 
     def _newlist_unwrapped(self, space, res):
     def descr_reverse(self, space):
         self.data.reverse()
 
-W_BytearrayObject.EMPTY = W_BytearrayObject([])
-
 
 bytearray_append  = SMM('append', 2)
 bytearray_extend  = SMM('extend', 2)

File pypy/objspace/std/bytesobject.py

View file
  • Ignore whitespace
     def _new(self, value):
         return W_BytesObject(value)
 
+    def _new_from_list(self, value):
+        return W_BytesObject(''.join(value))
+
+    def _empty(self):
+        return W_BytesObject.EMPTY
+
     def _len(self):
         return len(self._value)
 
         assert len(char) == 1
         return str(char)[0]
 
-    _empty = ''
     _builder = StringBuilder
 
     def _isupper(self, ch):

File pypy/objspace/std/stringmethods.py

View file
  • Ignore whitespace
                 return space.w_NotImplemented
             raise
         if times <= 0:
-            return self.EMPTY
+            return self._empty()
         if self._len() == 1:
             return self._new(self._val(space)[0] * times)
         return self._new(self._val(space) * times)
             length = len(selfvalue)
             start, stop, step, sl = w_index.indices4(space, length)
             if sl == 0:
-                return self.EMPTY
+                return self._empty()
             elif step == 1:
                 assert start >= 0 and stop >= 0
                 return self._sliced(space, selfvalue, start, stop, self)
             else:
-                str = self._empty.join([selfvalue[start + i*step] for i in range(sl)])
-            return self._new(str)
+                ret = [selfvalue[start + i*step] for i in range(sl)]
+                return self._new_from_list(ret)
 
         index = space.getindex_w(w_index, space.w_IndexError, "string index")
         selfvalue = self._val(space)
         start, stop = normalize_simple_slice(space, len(selfvalue), w_start,
                                              w_stop)
         if start == stop:
-            return self.EMPTY
+            return self._empty()
         else:
             return self._sliced(space, selfvalue, start, stop, self)
 
     def descr_capitalize(self, space):
         value = self._val(space)
         if len(value) == 0:
-            return self.EMPTY
+            return self._empty()
 
         builder = self._builder(len(value))
         builder.append(self._upper(value[0]))
     def descr_expandtabs(self, space, tabsize=8):
         value = self._val(space)
         if not value:
-            return self.EMPTY
+            return self._empty()
 
         splitted = value.split(self._chr('\t'))
         try:
         size = len(list_w)
 
         if size == 0:
-            return self.EMPTY
+            return self._empty()
 
         if size == 1:
             w_s = list_w[0]
                                  space.wrap("empty separator"))
         pos = value.find(sub)
         if pos == -1:
-            return space.newtuple([self, self.EMPTY, self.EMPTY])
+            return space.newtuple([self, self._empty(), self._empty()])
         else:
             from pypy.objspace.std.bytearrayobject import W_BytearrayObject
             if isinstance(self, W_BytearrayObject):
                                  space.wrap("empty separator"))
         pos = value.rfind(sub)
         if pos == -1:
-            return space.newtuple([self.EMPTY, self.EMPTY, self])
+            return space.newtuple([self._empty(), self._empty(), self])
         else:
             from pypy.objspace.std.bytearrayobject import W_BytearrayObject
             if isinstance(self, W_BytearrayObject):

File pypy/objspace/std/unicodeobject.py

View file
  • Ignore whitespace
     def _new(self, value):
         return W_UnicodeObject(value)
 
+    def _new_from_list(self, value):
+        return W_UnicodeObject(u''.join(value))
+
+    def _empty(self):
+        return W_UnicodeObject.EMPTY
+
     def _len(self):
         return len(self._value)
 
         assert len(char) == 1
         return unicode(char)[0]
 
-    _empty = u''
     _builder = UnicodeBuilder
 
     def _isupper(self, ch):