[pypy-svn] r61896 - in pypy/trunk/pypy/rpython: . test

afa at codespeak.net afa at codespeak.net
Sat Feb 14 23:42:29 CET 2009


Author: afa
Date: Sat Feb 14 23:42:28 2009
New Revision: 61896

Modified:
   pypy/trunk/pypy/rpython/annlowlevel.py
   pypy/trunk/pypy/rpython/test/test_annlowlevel.py
Log:
Annotation helpers for unicode strings.


Modified: pypy/trunk/pypy/rpython/annlowlevel.py
==============================================================================
--- pypy/trunk/pypy/rpython/annlowlevel.py	(original)
+++ pypy/trunk/pypy/rpython/annlowlevel.py	Sat Feb 14 23:42:28 2009
@@ -381,10 +381,15 @@
 
 # ____________________________________________________________
 
-if 1:
+def make_string_entries(strtype):
+    assert strtype in (str, unicode)
+
     def hlstr(ll_s):
         if hasattr(ll_s, 'chars'):
-            return ''.join(ll_s.chars)
+            if strtype is str:
+                return ''.join(ll_s.chars)
+            else:
+                return u''.join(ll_s.chars)
         else:
             return ll_s._str
 
@@ -392,7 +397,10 @@
         _about_ = hlstr
 
         def compute_result_annotation(self, s_ll_str):
-            return annmodel.SomeString()
+            if strtype is str:
+                return annmodel.SomeString()
+            else:
+                return annmodel.SomeUnicodeString()
 
         def specialize_call(self, hop):
             hop.exception_cannot_occur()
@@ -402,9 +410,12 @@
                              resulttype = hop.r_result.lowleveltype)
 
     def llstr(s):
-        from pypy.rpython.lltypesystem.rstr import mallocstr
+        from pypy.rpython.lltypesystem.rstr import mallocstr, mallocunicode
         # XXX not sure what to do with ootypesystem
-        ll_s = mallocstr(len(s))
+        if strtype is str:
+            ll_s = mallocstr(len(s))
+        else:
+            ll_s = mallocunicode(len(s))
         for i, c in enumerate(s):
             ll_s.chars[i] = c
         return ll_s
@@ -413,8 +424,11 @@
         _about_ = llstr
 
         def compute_result_annotation(self, s_str):
-            from pypy.rpython.lltypesystem.rstr import STR
-            return annmodel.lltype_to_annotation(lltype.Ptr(STR))
+            from pypy.rpython.lltypesystem.rstr import STR, UNICODE
+            if strtype is str:
+                return annmodel.lltype_to_annotation(lltype.Ptr(STR))
+            else:
+                return annmodel.lltype_to_annotation(lltype.Ptr(UNICODE))
 
         def specialize_call(self, hop):
             hop.exception_cannot_occur()
@@ -423,6 +437,11 @@
             return hop.genop('same_as', [v_ll_str],
                              resulttype = hop.r_result.lowleveltype)
 
+    return hlstr, llstr
+
+hlstr,     llstr     = make_string_entries(str)
+hlunicode, llunicode = make_string_entries(unicode)
+
 # ____________________________________________________________
 
 def cast_object_to_ptr(PTR, object):

Modified: pypy/trunk/pypy/rpython/test/test_annlowlevel.py
==============================================================================
--- pypy/trunk/pypy/rpython/test/test_annlowlevel.py	(original)
+++ pypy/trunk/pypy/rpython/test/test_annlowlevel.py	Sat Feb 14 23:42:28 2009
@@ -3,8 +3,9 @@
 """
 
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
-from pypy.rpython.lltypesystem.rstr import mallocstr
+from pypy.rpython.lltypesystem.rstr import mallocstr, mallocunicode
 from pypy.rpython.annlowlevel import hlstr, llstr
+from pypy.rpython.annlowlevel import hlunicode, llunicode
 
 class TestLLType(BaseRtypingTest, LLRtypeMixin):
     def test_hlstr(self):
@@ -29,3 +30,24 @@
         res = self.interpret(f, [self.string_to_ll("abc")])
         assert res == 3
     
+    def test_hlunicode(self):
+        s = mallocunicode(3)
+        s.chars[0] = u"a"
+        s.chars[1] = u"b"
+        s.chars[2] = u"c"
+        assert hlunicode(s) == u"abc"
+
+    def test_llunicode(self):
+        s = llunicode(u"abc")
+        assert len(s.chars) == 3
+        assert s.chars[0] == u"a"
+        assert s.chars[1] == u"b"
+        assert s.chars[2] == u"c"
+
+    def test_llunicode_compile(self):
+        def f(arg):
+            s = llunicode(hlunicode(arg))
+            return len(s.chars)
+
+        res = self.interpret(f, [self.unicode_to_ll(u"abc")])
+        assert res == 3



More information about the Pypy-commit mailing list