[pypy-svn] r77202 - in pypy/branch/resoperation-refactoring/pypy/jit/metainterp: . test

antocuni at codespeak.net antocuni at codespeak.net
Mon Sep 20 14:46:27 CEST 2010


Author: antocuni
Date: Mon Sep 20 14:46:25 2010
New Revision: 77202

Modified:
   pypy/branch/resoperation-refactoring/pypy/jit/metainterp/resoperation.py
   pypy/branch/resoperation-refactoring/pypy/jit/metainterp/test/test_resoperation.py
Log:
create common base classes for each combination of arity and {PlainResOp,
ResOpWithDescr, GuardResOp}, to avoid putting mixins in all the leaves



Modified: pypy/branch/resoperation-refactoring/pypy/jit/metainterp/resoperation.py
==============================================================================
--- pypy/branch/resoperation-refactoring/pypy/jit/metainterp/resoperation.py	(original)
+++ pypy/branch/resoperation-refactoring/pypy/jit/metainterp/resoperation.py	Mon Sep 20 14:46:25 2010
@@ -518,6 +518,18 @@
         opboolresult.append(boolresult)
     assert len(opclasses)==len(oparity)==len(opwithdescr)==len(opboolresult)==len(_oplist)
 
+def get_base_class(mixin, base):
+    try:
+        return get_base_class.cache[(mixin, base)]
+    except KeyError:
+        arity_name = mixin.__name__[:-2]  # remove the trailing "Op"
+        name = arity_name + base.__name__ # something like BinaryPlainResOp
+        bases = (mixin, base)
+        cls = type(name, bases, {})
+        get_base_class.cache[(mixin, base)] = cls
+        return cls
+get_base_class.cache = {}
+
 def create_class_for_op(name, opnum, arity, withdescr):
     arity2mixin = {
         0: NullaryOp,
@@ -540,7 +552,7 @@
         return opnum
 
     cls_name = '%s_OP' % name
-    bases = (mixin, baseclass)
+    bases = (get_base_class(mixin, baseclass),)
     dic = {'getopnum': getopnum}
     return type(cls_name, bases, dic)
 

Modified: pypy/branch/resoperation-refactoring/pypy/jit/metainterp/test/test_resoperation.py
==============================================================================
--- pypy/branch/resoperation-refactoring/pypy/jit/metainterp/test/test_resoperation.py	(original)
+++ pypy/branch/resoperation-refactoring/pypy/jit/metainterp/test/test_resoperation.py	Mon Sep 20 14:46:25 2010
@@ -42,6 +42,15 @@
     assert issubclass(cls, rop.UnaryOp)
     assert cls.getopnum.im_func(None) == rop.rop.GUARD_TRUE
 
+def test_mixins_in_common_base():
+    INT_ADD = rop.opclasses[rop.rop.INT_ADD]
+    assert len(INT_ADD.__bases__) == 1
+    BinaryPlainResOp = INT_ADD.__bases__[0]
+    assert BinaryPlainResOp.__name__ == 'BinaryPlainResOp'
+    assert BinaryPlainResOp.__bases__ == (rop.BinaryOp, rop.PlainResOp)
+    INT_SUB = rop.opclasses[rop.rop.INT_SUB]
+    assert INT_SUB.__bases__[0] is BinaryPlainResOp
+
 def test_instantiate():
     op = rop.ResOperation(rop.rop.INT_ADD, ['a', 'b'], 'c')
     assert op.getarglist() == ['a', 'b']



More information about the Pypy-commit mailing list