[pypy-svn] r7298 - in pypy/trunk/src/pypy/translator: . test

mwh at codespeak.net mwh at codespeak.net
Tue Nov 16 17:48:55 CET 2004


Author: mwh
Date: Tue Nov 16 17:48:54 2004
New Revision: 7298

Modified:
   pypy/trunk/src/pypy/translator/genc.h
   pypy/trunk/src/pypy/translator/genc.py
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_ctrans.py
Log:
Support multiple inheritance in genc.py.
Also, raise Exception, not TypeError, in nameof as the latter get
annoyingly eaten by list.extend (GRRR!)
Tests.


Modified: pypy/trunk/src/pypy/translator/genc.h
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.h	(original)
+++ pypy/trunk/src/pypy/translator/genc.h	Tue Nov 16 17:48:54 2004
@@ -105,9 +105,9 @@
 
 /*** classes ***/
 
-#define SETUP_CLASS(t, name, base)				\
+/*#define SETUP_CLASS(t, name, base)				\
 	t = PyObject_CallFunction((PyObject*) &PyType_Type,	\
-				  "s(O){}", name, base)
+				  "s(O){}", name, base)*/
 
 #define SETUP_CLASS_ATTR(t, attr, value)	\
 	(PyObject_SetAttrString(t, attr, value) >= 0)

Modified: pypy/trunk/src/pypy/translator/genc.py
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.py	(original)
+++ pypy/trunk/src/pypy/translator/genc.py	Tue Nov 16 17:48:54 2004
@@ -59,7 +59,7 @@
                     if meth:
                         break
                 else:
-                    raise TypeError, "nameof(%r)" % (obj,)
+                    raise Exception, "nameof(%r)" % (obj,)
                 name = meth(obj)
             self.cnames[key] = name
             return name
@@ -153,19 +153,16 @@
         return name
 
     def nameof_classobj(self, cls):
+        if issubclass(cls, Exception) and cls.__module__ == 'exceptions':
+            return 'PyExc_%s'%cls.__name__
         name = self.uniquename('gcls_' + cls.__name__)
         bases = [base for base in cls.__bases__ if base is not object]
-        assert len(bases) <= 1, "%r needs multiple inheritance" % (cls,)
-        if bases:
-            base = bases[0]
-        else:
-            base = object
-        base = self.nameof(base)
+        basenames = [self.nameof(base) for base in bases]
         def initclassobj():
             content = cls.__dict__.items()
             content.sort()
             for key, value in content:
-                if key.startswith('__') and key != '__init__':
+                if key.startswith('__'):
                     if key in ['__module__', '__doc__', '__dict__',
                                '__weakref__', '__repr__']:
                         continue
@@ -174,8 +171,15 @@
                 yield 'INITCHK(SETUP_CLASS_ATTR(%s, "%s", %s))' % (
                     name, key, self.nameof(value))
         self.globaldecl.append('static PyObject* %s;' % name)
-        self.initcode.append('INITCHK(SETUP_CLASS(%s, "%s", %s))' % (
-            name, cls.__name__, base))
+
+        baseargs = ", ".join(basenames)
+        if baseargs:
+            baseargs = ', '+baseargs
+        self.initcode.append('INITCHK(%s = PyObject_CallFunction((PyObject*) &PyType_Type,'
+                             %(name,))
+        self.initcode.append('\t\t"s(%s){}", "%s"%s))'
+                             %("O"*len(bases), cls.__name__, baseargs))
+        
         self.latercode.append(initclassobj())
         return name
 

Modified: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/snippet.py	(original)
+++ pypy/trunk/src/pypy/translator/test/snippet.py	Tue Nov 16 17:48:54 2004
@@ -429,6 +429,19 @@
 
 global_bi = BadInit(1)
 
+class MI_A(object):
+    a = 1
+class MI_B(MI_A):
+    b = 2
+class MI_C(MI_A):
+    c = 3
+class MI_D(MI_B, MI_C):
+    d = 4
+
+def multiple_inheritance():
+    i = MI_D()
+    return i.a + i.b + i.c + i.d
+
 def global_badinit():
     return global_bi.read()
     

Modified: pypy/trunk/src/pypy/translator/test/test_ctrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_ctrans.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_ctrans.py	Tue Nov 16 17:48:54 2004
@@ -117,6 +117,10 @@
 ##         global_badinit = self.build_cfunc(snippet.global_badinit)
 ##         self.assertEquals(global_badinit(), 1)
 
+    def test_multiple_inheritance(self):
+        multiple_inheritance = self.build_cfunc(snippet.multiple_inheritance)
+        self.assertEquals(multiple_inheritance(), 1+2+3+4)
+
 class TypedTestCase(testit.IntTestCase):
 
     def getcompiled(self, func):



More information about the Pypy-commit mailing list