[pypy-commit] pypy refactor-str-types: Don't share an empty bytearray.

Manuel Jacob noreply at buildbot.pypy.org
Sun Jul 28 11:44:38 CEST 2013


Author: Manuel Jacob
Branch: refactor-str-types
Changeset: r65732:6c6d25371f1f
Date: 2013-07-27 23:02 +0200
http://bitbucket.org/pypy/pypy/changeset/6c6d25371f1f/

Log:	Don't share an empty bytearray.

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
@@ -34,6 +34,12 @@
     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)
 
@@ -47,7 +53,6 @@
         assert len(char) == 1
         return str(char)[0]
 
-    _empty = ''
     _builder = StringBuilder
 
     def _newlist_unwrapped(self, space, res):
@@ -270,8 +275,6 @@
     def descr_reverse(self, space):
         self.data.reverse()
 
-W_BytearrayObject.EMPTY = W_BytearrayObject([])
-
 
 bytearray_append  = SMM('append', 2)
 bytearray_extend  = SMM('extend', 2)
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -72,6 +72,12 @@
     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)
 
@@ -86,7 +92,6 @@
         assert len(char) == 1
         return str(char)[0]
 
-    _empty = ''
     _builder = StringBuilder
 
     def _isupper(self, ch):
diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -127,7 +127,7 @@
                 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)
@@ -139,13 +139,13 @@
             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)
@@ -167,7 +167,7 @@
         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)
 
@@ -175,7 +175,7 @@
     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]))
@@ -228,7 +228,7 @@
     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:
@@ -397,7 +397,7 @@
         size = len(list_w)
 
         if size == 0:
-            return self.EMPTY
+            return self._empty()
 
         if size == 1:
             w_s = list_w[0]
@@ -484,7 +484,7 @@
                                  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):
@@ -502,7 +502,7 @@
                                  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):
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -72,6 +72,12 @@
     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)
 
@@ -87,7 +93,6 @@
         assert len(char) == 1
         return unicode(char)[0]
 
-    _empty = u''
     _builder = UnicodeBuilder
 
     def _isupper(self, ch):


More information about the pypy-commit mailing list