[pypy-svn] r26308 - in pypy/dist/pypy/translator/cl: . test

sanxiyn at codespeak.net sanxiyn at codespeak.net
Tue Apr 25 10:48:34 CEST 2006


Author: sanxiyn
Date: Tue Apr 25 10:48:29 2006
New Revision: 26308

Modified:
   pypy/dist/pypy/translator/cl/gencl.py
   pypy/dist/pypy/translator/cl/test/test_cltrans_oo.py
Log:
(dialtone, sanxiyn)
Declare superclasses. Tests. Add a disabled test to check isinstance().


Modified: pypy/dist/pypy/translator/cl/gencl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/gencl.py	(original)
+++ pypy/dist/pypy/translator/cl/gencl.py	Tue Apr 25 10:48:29 2006
@@ -2,7 +2,8 @@
 
 from pypy.tool.udir import udir
 from pypy.translator.translator import graphof
-from pypy.rpython.ootypesystem.ootype import Instance, List, _static_meth, _meth
+from pypy.rpython.ootypesystem.ootype import Instance, List, _static_meth, _meth, ROOT
+from pypy.rpython.ootypesystem.rclass import OBJECT
 from pypy.translator.cl.clrepr import repr_arg, repr_var, repr_const, repr_fun_name, repr_class_name
 
 
@@ -65,20 +66,23 @@
 
     def declare_class(self, cls):
         # cls is really type of Instance
-        name = cls._name
-        fields = cls._fields
-        fieldnames = ['('+field+')' for field in fields.keys()]
-        field_declaration = ' '.join(fieldnames)
-        class_declaration = "(defclass %s () (%s))" % (repr_class_name(name), field_declaration)
-        return class_declaration
+        name = repr_class_name(cls._name)
+        field_declaration = ['('+field+')' for field in cls._fields]
+        field_declaration = " ".join(field_declaration)
+        if cls._superclass in (OBJECT, ROOT):
+            class_declaration = "(defclass %s () (%s))" % (name, field_declaration)
+        else:
+            self.declare_class(cls._superclass)
+            supername = repr_class_name(cls._superclass._name)
+            class_declaration = "(defclass %s (%s) (%s))" % (name, supername, field_declaration)
+        self.gen.declarations.append(class_declaration)
 
     def op_new(self, result, _):
         cls = self.args[0].value
         if isinstance(cls, List):
             yield "(setf %s (make-array 0 :adjustable t))" % (result,)
         else:
-            declaration = self.declare_class(cls)
-            self.gen.declarations.append(declaration)
+            self.declare_class(cls)
             yield "(setf %s (make-instance '%s))" % (result, repr_class_name(cls._name))
 
     def op_oosend(self, result, *ignore):

Modified: pypy/dist/pypy/translator/cl/test/test_cltrans_oo.py
==============================================================================
--- pypy/dist/pypy/translator/cl/test/test_cltrans_oo.py	(original)
+++ pypy/dist/pypy/translator/cl/test/test_cltrans_oo.py	Tue Apr 25 10:48:29 2006
@@ -1,4 +1,4 @@
-from pypy.translator.cl.buildcl import make_cl_func
+from pypy.translator.cl.buildcl import make_cl_func, generate_cl_func
 
 def test_simple():
     class C:
@@ -25,6 +25,33 @@
     cl_inc = make_cl_func(inc, [int])
     assert cl_inc(5) == 6
 
+def test_inherit():
+    class Foo:
+        pass
+    class Bar(Foo):
+        pass
+    def check_inheritance():
+        obj = Bar()
+    code = generate_cl_func(check_inheritance)
+    print code
+    assert code.count("defclass") == 2
+
+def dont_test_isinstance():
+    class Foo:
+        pass
+    class Bar(Foo):
+        pass
+    class Baz:
+        pass
+    def check_isinstance(flag):
+        if flag:
+            obj = Bar()
+        else:
+            obj = Baz()
+        return isinstance(obj, Foo)
+    cl_check_isinstance = make_cl_func(check_isinstance, [bool])
+    assert cl_check_isinstance(True) == True
+
 def test_list_length():
     def list_length_one(number):
         lst = [number]



More information about the Pypy-commit mailing list