Commits

Alex Gaynor committed 48cf370 Merge

Merged in enumerate-rstr (pull request #126)

Make enumerate() work with rstr.

Comments (0)

Files changed (6)

rpython/annotator/test/test_annrpython.py

File contents unchanged.

rpython/rtyper/lltypesystem/rlist.py

 
     def __init__(self, r_list):
         self.r_list = r_list
+        self.external_item_repr = r_list.external_item_repr
         self.lowleveltype = Ptr(GcStruct('listiter',
                                          ('list', r_list.lowleveltype),
                                          ('index', Signed)))

rpython/rtyper/lltypesystem/rstr.py

     def __init__(self):
         self.ll_striter = ll_striter
         self.ll_strnext = ll_strnext
+        self.ll_getnextindex = ll_getnextindex
 
 class StringIteratorRepr(BaseStringIteratorRepr):
 
+    external_item_repr = char_repr
     lowleveltype = Ptr(GcStruct('stringiter',
                                 ('string', string_repr.lowleveltype),
                                 ('index', Signed)))
 
 class UnicodeIteratorRepr(BaseStringIteratorRepr):
 
+    external_item_repr = unichar_repr
     lowleveltype = Ptr(GcStruct('unicodeiter',
                                 ('string', unicode_repr.lowleveltype),
                                 ('index', Signed)))
     iter.index = index + 1
     return chars[index]
 
+def ll_getnextindex(iter):
+    return iter.index
+
 string_repr.iterator_repr = StringIteratorRepr()
 unicode_repr.iterator_repr = UnicodeIteratorRepr()
 

rpython/rtyper/ootypesystem/rstr.py

 
 
 class StringIteratorRepr(AbstractStringIteratorRepr):
+    external_item_repr = char_repr
     lowleveltype = ootype.Record({'string': string_repr.lowleveltype,
                                   'index': ootype.Signed})
 
     def __init__(self):
         self.ll_striter = ll_striter
         self.ll_strnext = ll_strnext
+        self.ll_getnextindex = ll_getnextindex
 
 class UnicodeIteratorRepr(AbstractStringIteratorRepr):
+    external_item_repr = unichar_repr
     lowleveltype = ootype.Record({'string': unicode_repr.lowleveltype,
                                   'index': ootype.Signed})
 
     iter.index = index + 1
     return string.ll_stritem_nonneg(index)
 
+def ll_getnextindex(iter):
+    return iter.index
+
 
 StringRepr.string_iterator_repr = StringIteratorRepr()
 UnicodeRepr.string_iterator_repr = UnicodeIteratorRepr()

rpython/rtyper/rrange.py

         v_index = hop.gendirectcall(self.ll_getnextindex, v_enumerate)
         hop2 = hop.copy()
         hop2.args_r = [self.r_baseiter]
-        r_item_src = self.r_baseiter.r_list.external_item_repr
+        r_item_src = self.r_baseiter.external_item_repr
         r_item_dst = hop.r_result.items_r[1]
         v_item = self.r_baseiter.rtype_next(hop2)
         v_item = hop.llops.convertvar(v_item, r_item_src, r_item_dst)

rpython/rtyper/test/test_rstr.py

         got = self.interpret(f, [7])
         assert self.ll_to_string(got) == 'None'
 
+    def test_enumerate(self):
+        const = self.const
+        def fn(n):
+            s = const('abcde')
+            for i, x in enumerate(s):
+                if i == n:
+                    return x
+            return 'x'
+        res = self.interpret(fn, [2])
+        assert res == 'c'
+
 
 def FIXME_test_str_to_pystringobj():
     def f(n):