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

pedronis at codespeak.net pedronis at codespeak.net
Wed Aug 30 14:27:56 CEST 2006


Author: pedronis
Date: Wed Aug 30 14:27:55 2006
New Revision: 31832

Modified:
   pypy/dist/pypy/rpython/objectmodel.py
   pypy/dist/pypy/rpython/test/test_objectmodel.py
Log:
specialize decorator factory to use as in:

@specialize.memo()

or

@specialize.arg(0)

we are going to use decorators in some of the jit related code,
the non-decorator style is quite cumbersome to refactor.



Modified: pypy/dist/pypy/rpython/objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/objectmodel.py	Wed Aug 30 14:27:55 2006
@@ -5,6 +5,45 @@
 
 import sys, new
 
+# specialize is a decorator factory for attaching _annspecialcase_
+# attributes to functions: for example
+#
+# f._annspecialcase_ = 'specialize:memo' can be expressed with:
+# @specialize.memo()
+# def f(...
+#
+# f._annspecialcase_ = 'specialize:arg(0)' can be expressed with:
+# @specialize.arg(0)
+# def f(...
+#
+
+class _AttachSpecialization(object):
+
+    def __init__(self, tag):
+        self.tag = tag
+
+    def __call__(self, *args):
+        if not args:
+            args = ""
+        else:
+            args = "("+','.join([repr(arg) for arg in args]) +")"
+        specialcase = "specialize:%s%s" % (self.tag, args)
+        
+        def specialize_decorator(func):
+            func._annspecialcase_ = specialcase
+            return func
+
+        return specialize_decorator
+        
+class _Specialize(object):
+
+    def __getattr__(self, name):
+        return _AttachSpecialization(name)
+        
+specialize = _Specialize()
+
+# ____________________________________________________________
+
 class Symbolic(object):
 
     def annotation(self):
@@ -54,6 +93,8 @@
     
 malloc_zero_filled = CDefinedIntSymbolic('MALLOC_ZERO_FILLED', default=0)
 
+# ____________________________________________________________
+
 def instantiate(cls):
     "Create an empty instance of 'cls'."
     if isinstance(cls, type):

Modified: pypy/dist/pypy/rpython/test/test_objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/test/test_objectmodel.py	Wed Aug 30 14:27:55 2006
@@ -315,3 +315,20 @@
 
 class TestOOtype(BaseTestObjectModel, OORtypeMixin):
     pass
+
+
+def test_specialize_decorator():
+    def f():
+        pass
+
+    specialize.memo()(f)
+    
+    assert f._annspecialcase_ == 'specialize:memo'
+
+    specialize.arg(0)(f)
+
+    assert f._annspecialcase_ == 'specialize:arg(0)'
+
+    specialize.arg(1)(f)
+
+    assert f._annspecialcase_ == 'specialize:arg(1)'



More information about the Pypy-commit mailing list