[pypy-commit] pypy default: __pypy__.StringBuilder: after build(), we can continue to append more

arigo pypy.commits at gmail.com
Fri Jun 2 05:55:06 EDT 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r91487:2dbc1ac2dab9
Date: 2017-06-02 11:54 +0200
http://bitbucket.org/pypy/pypy/changeset/2dbc1ac2dab9/

Log:	__pypy__.StringBuilder: after build(), we can continue to append
	more strings to the same builder. This is supported since
	2ff5087aca28 in RPython.

diff --git a/pypy/module/__pypy__/interp_builders.py b/pypy/module/__pypy__/interp_builders.py
--- a/pypy/module/__pypy__/interp_builders.py
+++ b/pypy/module/__pypy__/interp_builders.py
@@ -18,31 +18,25 @@
             else:
                 self.builder = builder_cls(size)
 
-        def _check_done(self, space):
-            if self.builder is None:
-                raise oefmt(space.w_ValueError,
-                            "Can't operate on a built builder")
-
         @unwrap_spec(size=int)
         def descr__new__(space, w_subtype, size=-1):
             return W_Builder(space, size)
 
         @unwrap_spec(s=unwrap)
         def descr_append(self, space, s):
-            self._check_done(space)
             self.builder.append(s)
 
         @unwrap_spec(s=unwrap, start=int, end=int)
         def descr_append_slice(self, space, s, start, end):
-            self._check_done(space)
             if not 0 <= start <= end <= len(s):
                 raise oefmt(space.w_ValueError, "bad start/stop")
             self.builder.append_slice(s, start, end)
 
         def descr_build(self, space):
-            self._check_done(space)
             w_s = getattr(space, newmethod)(self.builder.build())
-            self.builder = None
+            # after build(), we can continue to append more strings
+            # to the same builder.  This is supported since
+            # 2ff5087aca28 in RPython.
             return w_s
 
         def descr_len(self, space):
diff --git a/pypy/module/__pypy__/test/test_builders.py b/pypy/module/__pypy__/test/test_builders.py
--- a/pypy/module/__pypy__/test/test_builders.py
+++ b/pypy/module/__pypy__/test/test_builders.py
@@ -9,8 +9,9 @@
         b.append(u"1")
         s = b.build()
         assert s == u"abc1231"
-        raises(ValueError, b.build)
-        raises(ValueError, b.append, u"123")
+        assert b.build() == s
+        b.append(u"123")
+        assert b.build() == s + u"123"
 
     def test_preallocate(self):
         from __pypy__.builders import UnicodeBuilder
@@ -26,8 +27,9 @@
         b.append_slice(u"abcdefgh", 2, 5)
         raises(ValueError, b.append_slice, u"1", 2, 1)
         s = b.build()
-        assert s == "cde"
-        raises(ValueError, b.append_slice, u"abc", 1, 2)
+        assert s == u"cde"
+        b.append_slice(u"abc", 1, 2)
+        assert b.build() == u"cdeb"
 
     def test_stringbuilder(self):
         from __pypy__.builders import StringBuilder
@@ -37,6 +39,6 @@
         assert len(b) == 6
         b.append("you and me")
         s = b.build()
-        raises(ValueError, len, b)
+        assert len(b) == 16
         assert s == "abc123you and me"
-        raises(ValueError, b.build)
+        assert b.build() == s


More information about the pypy-commit mailing list