[pypy-commit] pypy default: support builder-or-none

fijal noreply at buildbot.pypy.org
Mon Sep 12 13:20:43 CEST 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r47219:fa0be6dd8a1b
Date: 2011-09-12 13:09 +0200
http://bitbucket.org/pypy/pypy/changeset/fa0be6dd8a1b/

Log:	support builder-or-none

diff --git a/pypy/rlib/rstring.py b/pypy/rlib/rstring.py
--- a/pypy/rlib/rstring.py
+++ b/pypy/rlib/rstring.py
@@ -2,7 +2,8 @@
 """
 
 from pypy.annotation.model import (SomeObject, SomeString, s_None, SomeChar,
-    SomeInteger, SomeUnicodeCodePoint, SomeUnicodeString, SomePtr)
+    SomeInteger, SomeUnicodeCodePoint, SomeUnicodeString, SomePtr, SomePBC)
+from pypy.tool.pairtype import pair, pairtype
 from pypy.rpython.extregistry import ExtRegistryEntry
 
 
@@ -170,3 +171,14 @@
 class UnicodeBuilderEntry(BaseEntry, ExtRegistryEntry):
     _about_ = UnicodeBuilder
     use_unicode = True
+
+class __extend__(pairtype(SomeStringBuilder, SomePBC)):
+    def union((sb, p)):
+        assert p.const is None
+        return SomeStringBuilder(can_be_None=True)
+
+class __extend__(pairtype(SomePBC, SomeStringBuilder)):
+    def union((p, sb)):
+        assert p.const is None
+        return SomeStringBuilder(can_be_None=True)
+    
diff --git a/pypy/rlib/test/test_rstring.py b/pypy/rlib/test/test_rstring.py
--- a/pypy/rlib/test/test_rstring.py
+++ b/pypy/rlib/test/test_rstring.py
@@ -2,7 +2,6 @@
 
 from pypy.rlib.rstring import StringBuilder, UnicodeBuilder, split, rsplit
 
-
 def test_split():
     assert split("", 'x') == ['']
     assert split("a", "a", 1) == ['', '']
@@ -42,4 +41,5 @@
     assert s.getlength() == len('aabcb')
     s.append_multiple_char(u'd', 4)
     assert s.build() == 'aabcbdddd'
-    assert isinstance(s.build(), unicode)
\ No newline at end of file
+    assert isinstance(s.build(), unicode)
+        
diff --git a/pypy/rpython/lltypesystem/rbuilder.py b/pypy/rpython/lltypesystem/rbuilder.py
--- a/pypy/rpython/lltypesystem/rbuilder.py
+++ b/pypy/rpython/lltypesystem/rbuilder.py
@@ -4,7 +4,7 @@
 from pypy.rpython.annlowlevel import llstr
 from pypy.rpython.rptr import PtrRepr
 from pypy.rpython.lltypesystem import lltype, rstr
-from pypy.rpython.lltypesystem.lltype import staticAdtMethod
+from pypy.rpython.lltypesystem.lltype import staticAdtMethod, nullptr
 from pypy.rpython.lltypesystem.rstr import (STR, UNICODE, char_repr,
     string_repr, unichar_repr, unicode_repr)
 from pypy.rpython.rbuilder import AbstractStringBuilderRepr
@@ -54,6 +54,9 @@
 MAX = 16*1024*1024
 
 class BaseStringBuilderRepr(AbstractStringBuilderRepr):
+    def empty(self):
+        return nullptr(self.lowleveltype.TO)
+    
     @classmethod
     def ll_new(cls, init_size):
         if init_size < 0 or init_size > MAX:
@@ -123,6 +126,10 @@
             return ll_builder.buf
         return rgc.ll_shrink_array(ll_builder.buf, final_size)
 
+    @classmethod
+    def ll_is_true(cls, ll_builder):
+        return ll_builder != nullptr(cls.lowleveltype.TO)
+
 class StringBuilderRepr(BaseStringBuilderRepr):
     lowleveltype = lltype.Ptr(STRINGBUILDER)
     basetp = STR
diff --git a/pypy/rpython/ootypesystem/rbuilder.py b/pypy/rpython/ootypesystem/rbuilder.py
--- a/pypy/rpython/ootypesystem/rbuilder.py
+++ b/pypy/rpython/ootypesystem/rbuilder.py
@@ -7,6 +7,9 @@
 MAX = 16*1024*1024
 
 class BaseBuilderRepr(AbstractStringBuilderRepr):
+    def empty(self):
+        return ootype.null(self.lowleveltype)
+    
     @classmethod
     def ll_new(cls, init_size):
         if init_size < 0 or init_size > MAX:
@@ -36,6 +39,10 @@
     def ll_build(builder):
         return builder.ll_build()
 
+    @staticmethod
+    def ll_is_true(builder):
+        return bool(builder)
+
 class StringBuilderRepr(BaseBuilderRepr):
     lowleveltype = ootype.StringBuilder
     string_repr = string_repr
diff --git a/pypy/rpython/rbuilder.py b/pypy/rpython/rbuilder.py
--- a/pypy/rpython/rbuilder.py
+++ b/pypy/rpython/rbuilder.py
@@ -48,3 +48,13 @@
         vlist = hop.inputargs(self)
         hop.exception_cannot_occur()
         return hop.gendirectcall(self.ll_build, *vlist)
+
+    def rtype_is_true(self, hop):
+        vlist = hop.inputargs(self)
+        hop.exception_cannot_occur()
+        return hop.gendirectcall(self.ll_is_true, *vlist)
+        
+    def convert_const(self, value):
+        if not value is None:
+            raise TypeError("Prebuilt builedrs that are not none unsupported")
+        return self.empty()
diff --git a/pypy/rpython/test/test_rbuilder.py b/pypy/rpython/test/test_rbuilder.py
--- a/pypy/rpython/test/test_rbuilder.py
+++ b/pypy/rpython/test/test_rbuilder.py
@@ -84,6 +84,24 @@
         res = self.ll_to_string(self.interpret(func, [5]))
         assert res == "hello"
 
+    def test_builder_or_none(self):
+        def g(s):
+            if s:
+                s.append("3")
+            return bool(s)
+        
+        def func(i):
+            if i:
+                s = StringBuilder()
+            else:
+                s = None
+            return g(s)
+        res = self.interpret(func, [0])
+        assert not res
+        res = self.interpret(func, [1])
+        assert res
+
+
 class TestLLtype(BaseTestStringBuilder, LLRtypeMixin):
     pass
 
@@ -93,4 +111,4 @@
     def test_unicode_getlength(self):
         py.test.skip("getlength(): not implemented on ootype")
     def test_append_charpsize(self):
-        py.test.skip("append_charpsize(): not implemented on ootype")
\ No newline at end of file
+        py.test.skip("append_charpsize(): not implemented on ootype")


More information about the pypy-commit mailing list