Author: Maciej Fijalkowski <[email protected]>
Branch: 
Changeset: r78640:993ddbf39539
Date: 2015-07-23 21:51 +0200
http://bitbucket.org/pypy/pypy/changeset/993ddbf39539/

Log:    (arigo, fijal) hack to support prebuilt string builders

diff --git a/rpython/rlib/rstring.py b/rpython/rlib/rstring.py
--- a/rpython/rlib/rstring.py
+++ b/rpython/rlib/rstring.py
@@ -494,18 +494,22 @@
     # This is not the real implementation!
 
     def __init__(self, init_size=INIT_SIZE):
+        "NOT_RPYTHON"
         self._l = []
         self._size = 0
 
     def _grow(self, size):
+        "NOT_RPYTHON"
         self._size += size
 
     def append(self, s):
+        "NOT_RPYTHON"
         assert isinstance(s, self._tp)
         self._l.append(s)
         self._grow(len(s))
 
     def append_slice(self, s, start, end):
+        "NOT_RPYTHON"
         assert isinstance(s, self._tp)
         assert 0 <= start <= end <= len(s)
         s = s[start:end]
@@ -513,11 +517,13 @@
         self._grow(len(s))
 
     def append_multiple_char(self, c, times):
+        "NOT_RPYTHON"
         assert isinstance(c, self._tp)
         self._l.append(c * times)
         self._grow(times)
 
     def append_charpsize(self, s, size):
+        "NOT_RPYTHON"
         assert size >= 0
         l = []
         for i in xrange(size):
@@ -526,12 +532,14 @@
         self._grow(size)
 
     def build(self):
+        "NOT_RPYTHON"
         result = self._tp("").join(self._l)
         assert len(result) == self._size
         self._l = [result]
         return result
 
     def getlength(self):
+        "NOT_RPYTHON"
         return self._size
 
 
@@ -672,11 +680,22 @@
     _about_ = StringBuilder
     use_unicode = False
 
-
 class UnicodeBuilderEntry(BaseEntry, ExtRegistryEntry):
     _about_ = UnicodeBuilder
     use_unicode = True
 
+class PrebuiltStringBuilderEntry(ExtRegistryEntry):
+    _type_ = StringBuilder
+
+    def compute_annotation(self):
+        return SomeStringBuilder()
+
+class PrebuiltUnicodeBuilderEntry(ExtRegistryEntry):
+    _type_ = UnicodeBuilder
+
+    def compute_annotation(self):
+        return SomeUnicodeBuilder()
+
 
 #___________________________________________________________________
 # Support functions for SomeString.no_nul
diff --git a/rpython/rtyper/lltypesystem/rbuilder.py 
b/rpython/rtyper/lltypesystem/rbuilder.py
--- a/rpython/rtyper/lltypesystem/rbuilder.py
+++ b/rpython/rtyper/lltypesystem/rbuilder.py
@@ -10,6 +10,8 @@
     string_repr, unichar_repr, unicode_repr)
 from rpython.rtyper.rbuilder import AbstractStringBuilderRepr
 from rpython.tool.sourcetools import func_with_new_name
+from rpython.rtyper.annlowlevel import llstr, llunicode
+        
 
 
 # ------------------------------------------------------------
@@ -413,6 +415,7 @@
 class StringBuilderRepr(BaseStringBuilderRepr):
     lowleveltype = lltype.Ptr(STRINGBUILDER)
     basetp = STR
+    convert_to_ll = staticmethod(llstr)
     string_repr = string_repr
     char_repr = char_repr
     raw_ptr_repr = PtrRepr(
@@ -435,6 +438,7 @@
 class UnicodeBuilderRepr(BaseStringBuilderRepr):
     lowleveltype = lltype.Ptr(UNICODEBUILDER)
     basetp = UNICODE
+    convert_to_ll = staticmethod(llunicode)
     string_repr = unicode_repr
     char_repr = unichar_repr
     raw_ptr_repr = PtrRepr(
diff --git a/rpython/rtyper/rbuilder.py b/rpython/rtyper/rbuilder.py
--- a/rpython/rtyper/rbuilder.py
+++ b/rpython/rtyper/rbuilder.py
@@ -55,6 +55,9 @@
         return hop.gendirectcall(self.ll_bool, *vlist)
 
     def convert_const(self, value):
-        if not value is None:
-            raise TypeError("Prebuilt builedrs that are not none unsupported")
-        return self.empty()
+        if value is None:
+            return self.empty()
+        s = value.build()
+        ll_obj = self.ll_new(len(s))
+        self.ll_append(ll_obj, self.convert_to_ll(s))
+        return ll_obj
diff --git a/rpython/rtyper/test/test_rbuilder.py 
b/rpython/rtyper/test/test_rbuilder.py
--- a/rpython/rtyper/test/test_rbuilder.py
+++ b/rpython/rtyper/test/test_rbuilder.py
@@ -194,3 +194,23 @@
         assert not res
         res = self.interpret(func, [1])
         assert res
+
+    def test_prebuilt_string_builder(self):
+        s = StringBuilder(100)
+        s.append("abc")
+        
+        def f():
+            return len(s.build())
+
+        res = self.interpret(f, [])
+        assert res == 3
+
+    def test_prebuilt_unicode_builder(self):
+        s = UnicodeBuilder(100)
+        s.append(u"abc")
+        
+        def f():
+            return len(s.build())
+
+        res = self.interpret(f, [])
+        assert res == 3
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to