[pypy-svn] r25340 - in pypy/dist/pypy: annotation rpython rpython/lltypesystem rpython/ootypesystem rpython/ootypesystem/test rpython/test

nik at codespeak.net nik at codespeak.net
Wed Apr 5 11:04:51 CEST 2006


Author: nik
Date: Wed Apr  5 11:04:49 2006
New Revision: 25340

Modified:
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/rpython/lltypesystem/rlist.py
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rlist.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rlist.py
Log:
make list iterators work in ootypesystem. this included some other
fixes along the way:
- make sure list types are cached so we can rely on their identity hash
- make annotation of lists in ll helpers correct


Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Wed Apr  5 11:04:49 2006
@@ -561,7 +561,7 @@
     except TypeError:
         s = None    # unhashable T, e.g. a Ptr(GcForwardReference())
     if s is None:
-        if isinstance(T, ootype.Instance):
+        if isinstance(T, (ootype.Instance, ootype.List)):
             return SomeOOInstance(T)
         elif isinstance(T, ootype.StaticMethod):
             return SomeOOStaticMeth(T)

Modified: pypy/dist/pypy/rpython/lltypesystem/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rlist.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rlist.py	Wed Apr  5 11:04:49 2006
@@ -3,8 +3,9 @@
 from pypy.objspace.flow.model import Constant
 from pypy.rpython.error import TyperError
 from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
-from pypy.rpython.rmodel import IteratorRepr, externalvsinternal
-from pypy.rpython.rlist import AbstractListRepr, rtype_newlist
+from pypy.rpython.rmodel import externalvsinternal
+from pypy.rpython.rlist import AbstractListRepr, AbstractListIteratorRepr, \
+        rtype_newlist
 from pypy.rpython.rlist import dum_nocheck, dum_checkidx
 from pypy.rpython.rslice import SliceRepr
 from pypy.rpython.rslice import startstop_slice_repr, startonly_slice_repr
@@ -53,9 +54,6 @@
     def _setup_repr_final(self):
         self.list_builder.setup(self)
 
-    def recast(self, llops, v):
-        return llops.convertvar(v, self.item_repr, self.external_item_repr)
-
     def convert_const(self, listobj):
         # get object from bound list method
         #listobj = getattr(listobj, '__self__', listobj)
@@ -974,25 +972,15 @@
 #
 #  Iteration.
 
-class ListIteratorRepr(IteratorRepr):
+class ListIteratorRepr(AbstractListIteratorRepr):
 
     def __init__(self, r_list):
         self.r_list = r_list
         self.lowleveltype = Ptr(GcStruct('listiter',
                                          ('list', r_list.lowleveltype),
                                          ('index', Signed)))
-
-    def newiter(self, hop):
-        v_lst, = hop.inputargs(self.r_list)
-        citerptr = hop.inputconst(Void, self.lowleveltype)
-        return hop.gendirectcall(ll_listiter, citerptr, v_lst)
-
-    def rtype_next(self, hop):
-        v_iter, = hop.inputargs(self)
-        hop.has_implicit_exception(StopIteration) # record that we know about it
-        hop.exception_is_here()
-        v_res = hop.gendirectcall(ll_listnext, v_iter)
-        return self.r_list.recast(hop.llops, v_res)
+        self.ll_listiter = ll_listiter
+        self.ll_listnext = ll_listnext
 
 def ll_listiter(ITERPTR, lst):
     iter = malloc(ITERPTR.TO)

Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Wed Apr  5 11:04:49 2006
@@ -180,7 +180,7 @@
         # to map to their native list implementations.
         self._METHODS = frozendict({
             # "name": Meth([ARGUMENT1_TYPE, ARGUMENT2_TYPE, ...], RESULT_TYPE)
-            "length": Meth([], Unsigned),
+            "length": Meth([], Signed),
             "append": Meth([ITEMTYPE], Void),
             "getitem": Meth([Signed], ITEMTYPE),
             "setitem": Meth([Signed, ITEMTYPE], Void),

Modified: pypy/dist/pypy/rpython/ootypesystem/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rlist.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rlist.py	Wed Apr  5 11:04:49 2006
@@ -1,6 +1,8 @@
 from pypy.annotation.pairtype import pairtype
-from pypy.rpython.rlist import AbstractListRepr, rtype_newlist
-from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst, externalvsinternal
+from pypy.rpython.rlist import AbstractListRepr, AbstractListIteratorRepr, \
+        rtype_newlist
+from pypy.rpython.rmodel import Repr, IntegerRepr
+from pypy.rpython.rmodel import inputconst, externalvsinternal
 from pypy.rpython.ootypesystem import ootype
 
 class BaseListRepr(AbstractListRepr):
@@ -13,7 +15,7 @@
             assert callable(item_repr)
             self._item_repr_computer = item_repr
         else:
-            self.lowleveltype = ootype.List(item_repr.lowleveltype)
+            self.lowleveltype = list_type(item_repr)
             self.external_item_repr, self.item_repr = \
                     externalvsinternal(rtyper, item_repr)
         self.listitem = listitem
@@ -24,7 +26,7 @@
         if 'item_repr' not in self.__dict__:
             self.external_item_repr, self.item_repr = \
                     externalvsinternal(self.rtyper, self._item_repr_computer())
-            self.lowleveltype = ootype.List(self.item_repr.lowleveltype)
+            self.lowleveltype = list_type(self.item_repr)
 
     def send_message(self, hop, message, can_raise=False):
         v_args = hop.inputargs(self, *hop.args_r[1:])
@@ -40,9 +42,23 @@
     def rtype_method_append(self, hop):
         return self.send_message(hop, "append")
 
+    def make_iterator_repr(self):
+        return ListIteratorRepr(self)
+
 ListRepr = BaseListRepr
 FixedSizeListRepr = BaseListRepr
 
+_list_types = {}
+
+def list_type(item_repr):
+    key = item_repr.lowleveltype
+    if _list_types.has_key(key):
+        return _list_types[key]
+    else:
+        LIST = ootype.List(key)
+        _list_types[key] = LIST
+        return LIST
+
 class __extend__(pairtype(BaseListRepr, IntegerRepr)):
 
     def rtype_getitem((r_list, r_int), hop):
@@ -63,3 +79,41 @@
                 resulttype=ootype.Void)
     return v_result
 
+# ____________________________________________________________
+#
+#  Iteration.
+
+_list_iter_types = {}
+
+def list_iter_type(r_list):
+    key = r_list.lowleveltype
+    if _list_iter_types.has_key(key):
+        return _list_iter_types[key]
+    else:
+        INST = ootype.Instance("ListIter", ootype.ROOT,
+                {"list": r_list.lowleveltype, "index": ootype.Signed})
+        _list_iter_types[key] = INST
+        return INST
+
+class ListIteratorRepr(AbstractListIteratorRepr):
+
+    def __init__(self, r_list):
+        self.r_list = r_list
+        self.lowleveltype = list_iter_type(r_list)
+        self.ll_listiter = ll_listiter
+        self.ll_listnext = ll_listnext
+
+def ll_listiter(ITER, lst):
+    iter = ootype.new(ITER)
+    iter.list = lst
+    iter.index = 0
+    return iter
+
+def ll_listnext(iter):
+    l = iter.list
+    index = iter.index
+    if index >= l.length():
+        raise StopIteration
+    iter.index = index + 1
+    return l.getitem(index)
+

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py	Wed Apr  5 11:04:49 2006
@@ -1,5 +1,7 @@
 from pypy import conftest
 from pypy.rpython.ootypesystem.ootype import *
+from pypy.rpython.ootypesystem.rlist import ListRepr
+from pypy.rpython.rint import signed_repr
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow import FlowObjSpace
 from pypy.translator.translator import TranslationContext, graphof
@@ -106,7 +108,7 @@
 
     g = gengraph(oof, [])
     rettype = g.getreturnvar().concretetype
-    assert rettype == Unsigned
+    assert rettype == Signed
 
 def test_list_append():
     LT = List(Signed)
@@ -118,7 +120,7 @@
 
     g = gengraph(oof, [])
     rettype = g.getreturnvar().concretetype
-    assert rettype == Unsigned
+    assert rettype == Signed
 
 def test_list_getitem_setitem():
     LT = List(Signed)
@@ -146,3 +148,22 @@
 
     res = interpret(oof, [], type_system='ootype')
     assert res is -1
+
+def test_list_lltype_identity():
+    t = TranslationContext()
+    t.buildannotator()
+    rtyper = t.buildrtyper()
+    repr1 = ListRepr(rtyper, signed_repr)
+    repr2 = ListRepr(rtyper, signed_repr)
+    assert repr1.lowleveltype is repr2.lowleveltype
+
+def test_list_annotation():
+    LIST = List(Signed)
+
+    def oof(lst):
+        return lst.length()
+
+    lst = new(LIST)
+    lst.append(1)
+    res = interpret(oof, [lst], type_system='ootype')
+    assert res == 1

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Wed Apr  5 11:04:49 2006
@@ -1,5 +1,6 @@
 from pypy.annotation import model as annmodel
-from pypy.rpython.rmodel import Repr, inputconst
+from pypy.rpython.rmodel import Repr, IteratorRepr, inputconst
+from pypy.rpython.lltypesystem import lltype
 from pypy.rpython import robject
 
 
@@ -29,7 +30,8 @@
 
 class AbstractListRepr(Repr):
 
-    pass
+    def recast(self, llops, v):
+        return llops.convertvar(v, self.item_repr, self.external_item_repr)
 
 def rtype_newlist(hop):
     nb_args = hop.nb_args
@@ -51,3 +53,21 @@
 def dum_checkidx(): pass
 def dum_nocheck(): pass
 
+# ____________________________________________________________
+#
+#  Iteration.
+
+class AbstractListIteratorRepr(IteratorRepr):
+
+    def newiter(self, hop):
+        v_lst, = hop.inputargs(self.r_list)
+        citerptr = hop.inputconst(lltype.Void, self.lowleveltype)
+        return hop.gendirectcall(self.ll_listiter, citerptr, v_lst)
+
+    def rtype_next(self, hop):
+        v_iter, = hop.inputargs(self)
+        hop.has_implicit_exception(StopIteration) # record that we know about it
+        hop.exception_is_here()
+        v_res = hop.gendirectcall(self.ll_listnext, v_iter)
+        return self.r_list.recast(hop.llops, v_res)
+

Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Wed Apr  5 11:04:49 2006
@@ -194,26 +194,26 @@
             l = [5]
             l.append(6)
             return len(l)
-        res = interpret(dummyfn, [])
+        res = interpret(dummyfn, [], type_system=self.ts)
         assert res == 2
 
-def test_iterate():
-    def dummyfn():
-        total = 0
-        for x in [1, 3, 5, 7, 9]:
-            total += x
-        return total
-    res = interpret(dummyfn, [])
-    assert res == 25
-    def dummyfn():
-        total = 0
-        l = [1, 3, 5, 7]
-        l.append(9)
-        for x in l:
-            total += x
-        return total
-    res = interpret(dummyfn, [])
-    assert res == 25
+    def test_iterate(self):
+        def dummyfn():
+            total = 0
+            for x in [1, 3, 5, 7, 9]:
+                total += x
+            return total
+        res = interpret(dummyfn, [], type_system=self.ts)
+        assert res == 25
+        def dummyfn():
+            total = 0
+            l = [1, 3, 5, 7]
+            l.append(9)
+            for x in l:
+                total += x
+            return total
+        res = interpret(dummyfn, [], type_system=self.ts)
+        assert res == 25
 
 def test_recursive():
     def dummyfn(N):



More information about the Pypy-commit mailing list