[pypy-svn] r25246 - in pypy/dist/pypy/rpython/ootypesystem: . test

nik at codespeak.net nik at codespeak.net
Mon Apr 3 13:06:10 CEST 2006


Author: nik
Date: Mon Apr  3 13:06:01 2006
New Revision: 25246

Added:
   pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
Log:
a first stab at bringing lists to ootype. a list of e.g. integers is of type
ootype.List(Signed) in ootypesystem. oo lists have a yet to be fully discovered
interface defined in ootype.LIST_METHODS. for now, the interface consists of
only a length method that can already be rtyped.


Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Mon Apr  3 13:06:01 2006
@@ -168,6 +168,27 @@
 
     def __init__(self, args, result):
         StaticMethod.__init__(self, args, result)
+
+
+class List(OOType):
+
+    def __init__(self, ITEMTYPE):
+        self._ITEMTYPE = ITEMTYPE
+
+    def __str__(self):
+        return '%s(%s)' % (self.__class__.__name__, self._ITEMTYPE)
+
+    def _lookup(self, meth_name):
+        METH = LIST_METHODS.get(meth_name)
+        meth = None
+        if METH is not None:
+            meth = _meth(METH, _name=meth_name,
+                    _callable=getattr(_list, meth_name))
+        return self, meth
+
+    def _example(self):
+        return new(self)
+
 # ____________________________________________________________
 
 class _class(object):
@@ -403,7 +424,7 @@
         _callable.__init__(self, METHOD, **attrs)
 
     def _bound(self, DEFINST, inst):
-        assert isinstance(inst, _instance)
+        assert isinstance(inst, _instance) or isinstance(inst, _list)
         return _bound_meth(DEFINST, inst, self)
 
 class _bound_meth(object):
@@ -416,8 +437,42 @@
        callb, checked_args = self.meth._checkargs(args)
        return callb(self.inst, *checked_args)
 
-def new(INSTANCE):
-    return make_instance(INSTANCE)
+
+# This defines the abstract list interface that backends will have to map to
+# their native list implementations.
+LIST_METHODS = frozendict({
+    # "method name": Meth([ARGUMENT1_TYPE, ARGUMENT2_TYPE, ...], RESULT_TYPE)
+    "length": Meth([], Unsigned),
+})
+
+class _list(object):
+
+    def __init__(self, LIST):
+        self._TYPE = LIST 
+        self._list = []
+
+    def __getattribute__(self, name):
+        TYPE = object.__getattribute__(self, "_TYPE")
+        _, meth = TYPE._lookup(name)
+        if meth is not None:
+            return meth._bound(TYPE, self)
+
+        return object.__getattribute__(self, name)
+
+    # The following are implementations of the abstract list interface for
+    # use by the llinterpreter and ootype tests. There are NOT_RPYTHON
+    # because the annotator is not supposed to follow them.
+
+    def length(self):
+        # NOT_RPYTHON
+        return len(self._list)
+
+
+def new(TYPE):
+    if isinstance(TYPE, Instance):
+        return make_instance(TYPE)
+    elif isinstance(TYPE, List):
+        return _list(TYPE)
 
 def runtimenew(class_):
     assert isinstance(class_, _class)

Added: pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py	Mon Apr  3 13:06:01 2006
@@ -0,0 +1,12 @@
+from pypy.rpython.ootypesystem.ootype import *
+
+def test_new():
+    LT = List(Signed)
+    l = new(LT)
+    assert typeOf(l) == LT
+
+def test_len():
+    LT = List(Signed)
+    l = new(LT)
+    assert l.length() == 0
+

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	Mon Apr  3 13:06:01 2006
@@ -1,4 +1,4 @@
-
+from pypy import conftest
 from pypy.rpython.ootypesystem.ootype import *
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow import FlowObjSpace
@@ -8,10 +8,10 @@
 def gengraph(f, args=[], viewBefore=False, viewAfter=False):
     t = TranslationContext()
     t.buildannotator().build_types(f, args)
-    if viewBefore:
+    if viewBefore or conftest.option.view:
         t.view()
     t.buildrtyper(type_system="ootype").specialize()
-    if viewAfter:
+    if viewAfter or conftest.option.view:
         t.view()
     return graphof(t, f)
 
@@ -97,3 +97,14 @@
     rettype = g.getreturnvar().concretetype
     assert rettype == Bool
 
+def test_list_len():
+    LT = List(Signed)
+
+    def oof():
+        l = new(LT)
+        return l.length()
+
+    g = gengraph(oof, [])
+    rettype = g.getreturnvar().concretetype
+    assert rettype == Signed
+



More information about the Pypy-commit mailing list