[pypy-commit] pypy applevel-builder: Added append_slice to the applevel UnicodeBuilder, I think with some more inlining this could be done automatically.

alex_gaynor noreply at buildbot.pypy.org
Wed Jun 22 02:14:13 CEST 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: applevel-builder
Changeset: r45051:8aa2429df925
Date: 2011-06-21 17:18 -0700
http://bitbucket.org/pypy/pypy/changeset/8aa2429df925/

Log:	Added append_slice to the applevel UnicodeBuilder, I think with some
	more inlining this could be done automatically.

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
@@ -29,6 +29,13 @@
         else:
             self.builder.append(s)
 
+    @unwrap_spec(s=unicode, 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 OperationError(space.w_ValueError, space.wrap("bad start/stop"))
+        self.builder.append_slice(s, start, end)
+
     def descr_build(self, space):
         self._check_done(space)
         w_s = space.wrap(self.builder.build())
@@ -40,6 +47,7 @@
     __new__ = interp2app(W_UnicodeBuilder.descr__new__.im_func),
 
     append = interp2app(W_UnicodeBuilder.descr_append),
+    append_slice = interp2app(W_UnicodeBuilder.descr_append_slice),
     build = interp2app(W_UnicodeBuilder.descr_build),
 )
 W_UnicodeBuilder.typedef.acceptable_as_base_class = False
\ No newline at end of file
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
@@ -22,4 +22,13 @@
         b.append(u"abc")
         b.append(u"123")
         s = b.build()
-        assert s == u"abc123"
\ No newline at end of file
+        assert s == u"abc123"
+
+    def test_append_slice(self):
+        from __pypy__.builders import UnicodeBuilder
+        b = UnicodeBuilder()
+        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)
\ No newline at end of file


More information about the pypy-commit mailing list