[pypy-svn] r43292 - in pypy/branch/pypy-string-formatting/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 12 14:35:32 CEST 2007


Author: arigo
Date: Sat May 12 14:35:31 2007
New Revision: 43292

Modified:
   pypy/branch/pypy-string-formatting/rpython/rlist.py
   pypy/branch/pypy-string-formatting/rpython/test/test_rlist.py
Log:
Support for "lst += string" where lst is a list of unicode characters.


Modified: pypy/branch/pypy-string-formatting/rpython/rlist.py
==============================================================================
--- pypy/branch/pypy-string-formatting/rpython/rlist.py	(original)
+++ pypy/branch/pypy-string-formatting/rpython/rlist.py	Sat May 12 14:35:31 2007
@@ -354,9 +354,9 @@
 class __extend__(pairtype(AbstractListRepr, AbstractStringRepr)):
 
     def rtype_inplace_add((r_lst1, r_str2), hop):
-        if r_lst1.item_repr.lowleveltype != Char:
+        if r_lst1.item_repr.lowleveltype not in (Char, UniChar):
             raise TyperError('"lst += string" only supported with a list '
-                             'of chars')
+                             'of chars or unichars')
         string_repr = r_lst1.rtyper.type_system.rstr.string_repr
         v_lst1, v_str2 = hop.inputargs(r_lst1, string_repr)
         c_strlen  = hop.inputconst(Void, string_repr.ll.ll_strlen)
@@ -366,9 +366,9 @@
         return v_lst1
 
     def rtype_extend_with_str_slice((r_lst1, r_str2), hop):
-        if r_lst1.item_repr.lowleveltype != Char:
+        if r_lst1.item_repr.lowleveltype not in (Char, UniChar):
             raise TyperError('"lst += string" only supported with a list '
-                             'of chars')
+                             'of chars or unichars')
         rs = r_lst1.rtyper.type_system.rslice
         string_repr = r_lst1.rtyper.type_system.rstr.string_repr
         c_strlen  = hop.inputconst(Void, string_repr.ll.ll_strlen)
@@ -392,9 +392,9 @@
 class __extend__(pairtype(AbstractListRepr, AbstractCharRepr)):
 
     def rtype_extend_with_char_count((r_lst1, r_chr2), hop):
-        if r_lst1.item_repr.lowleveltype != Char:
+        if r_lst1.item_repr.lowleveltype not in (Char, UniChar):
             raise TyperError('"lst += string" only supported with a list '
-                             'of chars')
+                             'of chars or unichars')
         char_repr = r_lst1.rtyper.type_system.rstr.char_repr
         v_lst1, v_chr, v_count = hop.inputargs(r_lst1, char_repr, Signed)
         hop.gendirectcall(ll_extend_with_char_count, v_lst1, v_chr, v_count)
@@ -500,6 +500,12 @@
             return nullptr(ITEM.TO)
     return None
 
+def listItemType(lst):
+    LIST = typeOf(lst)
+    if isinstance(LIST, Ptr):    # lltype
+        LIST = LIST.TO
+    return LIST.ITEM
+
 
 def ll_copy(RESLIST, l):
     length = l.ll_length()
@@ -755,7 +761,10 @@
     i = start
     j = len1
     while i < len2:
-        lst.ll_setitem_fast(j, getstritem(s, i))
+        c = getstritem(s, i)
+        if listItemType(lst) is UniChar:
+            c = unichr(ord(c))
+        lst.ll_setitem_fast(j, c)
         i += 1
         j += 1
 
@@ -774,7 +783,10 @@
     i = start
     j = len1
     while i < stop:
-        lst.ll_setitem_fast(j, getstritem(s, i))
+        c = getstritem(s, i)
+        if listItemType(lst) is UniChar:
+            c = unichr(ord(c))
+        lst.ll_setitem_fast(j, c)
         i += 1
         j += 1
 
@@ -787,7 +799,10 @@
     i = 0
     j = len1
     while i < len2m1:
-        lst.ll_setitem_fast(j, getstritem(s, i))
+        c = getstritem(s, i)
+        if listItemType(lst) is UniChar:
+            c = unichr(ord(c))
+        lst.ll_setitem_fast(j, c)
         i += 1
         j += 1
 
@@ -798,6 +813,8 @@
     newlength = len1 + count
     lst._ll_resize_ge(newlength)
     j = len1
+    if listItemType(lst) is UniChar:
+        char = unichr(ord(char))
     while j < newlength:
         lst.ll_setitem_fast(j, char)
         j += 1

Modified: pypy/branch/pypy-string-formatting/rpython/test/test_rlist.py
==============================================================================
--- pypy/branch/pypy-string-formatting/rpython/test/test_rlist.py	(original)
+++ pypy/branch/pypy-string-formatting/rpython/test/test_rlist.py	Sat May 12 14:35:31 2007
@@ -1181,6 +1181,15 @@
         res = self.interpret(f, [58])
         assert self.ll_to_string(res) == 'abhello58'
 
+    def test_unicharlist_extension_1(self):
+        def f(n):
+            s = 'hello%d' % n
+            l = [u'a', u'b']
+            l += s
+            return ''.join([chr(ord(c)) for c in l])
+        res = self.interpret(f, [58])
+        assert self.ll_to_string(res) == 'abhello58'
+
     def test_extend_a_non_char_list_1(self):
         def f(n):
             s = 'hello%d' % n
@@ -1199,6 +1208,16 @@
         res = self.interpret(f, [9381701, 3])
         assert self.ll_to_string(res) == 'ablo9381701'
 
+    def test_unicharlist_extension_2(self):
+        def f(n, i):
+            s = 'hello%d' % n
+            assert 0 <= i <= len(s)
+            l = [u'a', u'b']
+            l += s[i:]
+            return ''.join([chr(ord(c)) for c in l])
+        res = self.interpret(f, [9381701, 3])
+        assert self.ll_to_string(res) == 'ablo9381701'
+
     def test_extend_a_non_char_list_2(self):
         def f(n, i):
             s = 'hello%d' % n
@@ -1218,6 +1237,16 @@
         res = self.interpret(f, [9381701, 3, 7])
         assert self.ll_to_string(res) == 'ablo93'
 
+    def test_unicharlist_extension_3(self):
+        def f(n, i, j):
+            s = 'hello%d' % n
+            assert 0 <= i <= j <= len(s)
+            l = [u'a', u'b']
+            l += s[i:j]
+            return ''.join([chr(ord(c)) for c in l])
+        res = self.interpret(f, [9381701, 3, 7])
+        assert self.ll_to_string(res) == 'ablo93'
+
     def test_charlist_extension_4(self):
         def f(n):
             s = 'hello%d' % n
@@ -1227,6 +1256,15 @@
         res = self.interpret(f, [9381701])
         assert self.ll_to_string(res) == 'abhello938170'
 
+    def test_unicharlist_extension_4(self):
+        def f(n):
+            s = 'hello%d' % n
+            l = [u'a', u'b']
+            l += s[:-1]
+            return ''.join([chr(ord(c)) for c in l])
+        res = self.interpret(f, [9381701])
+        assert self.ll_to_string(res) == 'abhello938170'
+
     def test_charlist_extension_5(self):
         def f(count):
             l = ['a', 'b']
@@ -1237,6 +1275,16 @@
         res = self.interpret(f, [0])
         assert self.ll_to_string(res) == 'ab'
 
+    def test_unicharlist_extension_5(self):
+        def f(count):
+            l = [u'a', u'b']
+            l += '.' * count     # NON-UNICODE-char * count
+            return ''.join([chr(ord(c)) for c in l])
+        res = self.interpret(f, [7])
+        assert self.ll_to_string(res) == 'ab.......'
+        res = self.interpret(f, [0])
+        assert self.ll_to_string(res) == 'ab'
+
     def test_charlist_extension_6(self):
         def f(count):
             l = ['a', 'b']


More information about the pypy-svn mailing list