1. Pypy
  2. Untitled project
  3. pypy

Commits

Armin Rigo  committed 53b334a

Start by changing the pure-Python implementation so that it
raises if it is used again after build().

  • Participants
  • Parent commits f6fcab1
  • Branches stringbuilder-perf

Comments (0)

Files changed (2)

File rpython/rlib/rstring.py

View file
  • Ignore whitespace
 
 
 class AbstractStringBuilder(object):
+    # This is not the real implementation!
+
     def __init__(self, init_size=INIT_SIZE):
-        self.l = []
-        self.size = 0
+        self._l = []
+        self._size = 0
 
     def _grow(self, size):
-        try:
-            self.size = ovfcheck(self.size + size)
-        except OverflowError:
-            raise MemoryError
+        self._size += size
 
     def append(self, s):
-        assert isinstance(s, self.tp)
-        self.l.append(s)
+        assert isinstance(s, self._tp)
+        self._l.append(s)
         self._grow(len(s))
 
     def append_slice(self, s, start, end):
-        assert isinstance(s, self.tp)
+        assert isinstance(s, self._tp)
         assert 0 <= start <= end <= len(s)
         s = s[start:end]
-        self.l.append(s)
+        self._l.append(s)
         self._grow(len(s))
 
     def append_multiple_char(self, c, times):
-        assert isinstance(c, self.tp)
-        self.l.append(c * times)
+        assert isinstance(c, self._tp)
+        self._l.append(c * times)
         self._grow(times)
 
     def append_charpsize(self, s, size):
         l = []
         for i in xrange(size):
             l.append(s[i])
-        self.l.append(self.tp("").join(l))
+        self._l.append(self._tp("").join(l))
         self._grow(size)
 
     def build(self):
-        return self.tp("").join(self.l)
+        result = self._tp("").join(self._l)
+        del self._l    # after build(), no further operation is allowed
+        return result
 
     def getlength(self):
-        return len(self.build())
+        return self._size
 
 
 class StringBuilder(AbstractStringBuilder):
-    tp = str
+    _tp = str
 
 
 class UnicodeBuilder(AbstractStringBuilder):
-    tp = unicode
+    _tp = unicode
 
 
 # ------------------------------------------------------------

File rpython/rlib/test/test_rstring.py

View file
  • Ignore whitespace
     s.append("a")
     s.append_slice("abc", 1, 2)
     s.append_multiple_char('d', 4)
-    assert s.build() == "aabcabdddd"
+    result = s.build()
+    assert result == "aabcabdddd"
+    py.test.raises(AttributeError, s.build)
+    py.test.raises(AttributeError, s.append, "x")
 
 def test_unicode_builder():
     s = UnicodeBuilder()
     s.append_slice(u'abcdef', 1, 2)
     assert s.getlength() == len('aabcb')
     s.append_multiple_char(u'd', 4)
-    assert s.build() == 'aabcbdddd'
-    assert isinstance(s.build(), unicode)
+    result = s.build()
+    assert result == 'aabcbdddd'
+    assert isinstance(result, unicode)
 
 
 class TestTranslates(BaseRtypingTest):