[pypy-svn] r38502 - in pypy/dist/pypy: annotation objspace/std rpython/test translator/goal

pedronis at codespeak.net pedronis at codespeak.net
Sun Feb 11 19:26:01 CET 2007


Author: pedronis
Date: Sun Feb 11 19:25:53 2007
New Revision: 38502

Modified:
   pypy/dist/pypy/annotation/specialize.py
   pypy/dist/pypy/objspace/std/intobject.py
   pypy/dist/pypy/rpython/test/test_rpbc.py
   pypy/dist/pypy/translator/goal/ann_override.py
Log:
oops, this code was using wrap on a tuple, it could get away with it because it was the only place left but well.



Modified: pypy/dist/pypy/annotation/specialize.py
==============================================================================
--- pypy/dist/pypy/annotation/specialize.py	(original)
+++ pypy/dist/pypy/annotation/specialize.py	Sun Feb 11 19:25:53 2007
@@ -373,6 +373,16 @@
 ##    return funcdesc.cachedgraph(s1_type, alt_name='memo_%s' % funcdesc.name, 
 ##                                         builder=builder)
 
+def make_constgraphbuilder(n, v=None, factory=None):
+    def constgraphbuilder(translator, ignore):
+        args = ','.join(["arg%d" % i for i in range(n)])
+        if factory is not None:
+            v = factory()
+        miniglobals = {'v': v}
+        exec "constf = lambda %s: v" % args in miniglobals
+        return translator.buildflowgraph(miniglobals['constf'])
+    return constgraphbuilder
+
 def specialize_argvalue(funcdesc, args_s, *argindices):
     key = tuple([args_s[i].const for i in argindices])
     return funcdesc.cachedgraph(key)

Modified: pypy/dist/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/intobject.py	(original)
+++ pypy/dist/pypy/objspace/std/intobject.py	Sun Feb 11 19:25:53 2007
@@ -167,7 +167,8 @@
                                 space.wrap("integer modulo"))
     # no overflow possible
     m = x % y
-    return space.wrap((z,m))
+    w = space.wrap
+    return space.newtuple([w(z), w(m)])
 
 def div__Int_Int(space, w_int1, w_int2):
     return _floordiv(space, w_int1, w_int2)

Modified: pypy/dist/pypy/rpython/test/test_rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rpbc.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rpbc.py	Sun Feb 11 19:25:53 2007
@@ -3,6 +3,7 @@
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 
+
 class MyBase:
     def m(self, x):
         return self.z + x
@@ -1542,7 +1543,58 @@
         assert self.ll_to_string(item0) == "hello"
         assert item1 == 623
 
+    def test_folding_specialize_support(self):
+
+        class S(object):
+            
+            def w(s, x):
+                if isinstance(x, int):
+                    return x
+                if isinstance(x, str):
+                    return len(x)
+                return -1
+            w._annspecialcase_ = "specialize:w"
+
+            def _freeze_(self):
+                return True
+
+        s = S()
+
+        def f(i, n):
+            w = s.w
+            if i == 0:
+                return w(0)
+            elif i == 1:
+                return w("abc")
+            elif i == 2:
+                return w(3*n)
+            elif i == 3:
+                return w(str(n))
+            return -1
+
+        class P(policy.AnnotatorPolicy):
+            allow_someobjects = False
+
+            def specialize__w(pol, funcdesc, args_s):
+                typ = args_s[1].knowntype
+                if args_s[0].is_constant() and args_s[1].is_constant():
+                    x = args_s[1].const
+                    v = s.w(x)
+                    builder = specialize.make_constgraphbuilder(2, v)
+                    return funcdesc.cachedgraph(x, builder=builder)
+                return funcdesc.cachedgraph(typ)
 
+        p = P()
+        
+        res = self.interpret(f, [0, 66], policy=p)
+        assert res == 0
+        res = self.interpret(f, [1, 66], policy=p)
+        assert res == 3
+        res = self.interpret(f, [2, 4], policy=p)
+        assert res == 12
+        res = self.interpret(f, [3, 5555], policy=p)
+        assert res == 4
+        
 class TestLLtype(BaseTestRPBC, LLRtypeMixin):
     pass
 

Modified: pypy/dist/pypy/translator/goal/ann_override.py
==============================================================================
--- pypy/dist/pypy/translator/goal/ann_override.py	(original)
+++ pypy/dist/pypy/translator/goal/ann_override.py	Sun Feb 11 19:25:53 2007
@@ -8,6 +8,10 @@
 from pypy.annotation import specialize
 from pypy.interpreter import baseobjspace
 
+def isidentifier(s):
+    if not s: return False
+    s = s.replace('_', 'x')
+    return s[0].isalpha() and s.isalnum()
 
 # patch - mostly for debugging, to enfore some signatures
 baseobjspace.ObjSpace.newbool.im_func._annenforceargs_ = Sig(lambda s1,s2: s1,
@@ -45,7 +49,14 @@
         def builder(translator, func):
             return translator.buildflowgraph(yield_thread)
         return funcdesc.cachedgraph(None, builder=builder)
-     
+
+    def count(self, kind):
+        try:
+            counters = self.foldedwraps
+        except AttributeError:
+            counters = self.foldedwraps = {}
+        counters[kind] = counters.get(kind, 0) + 1
+
     def specialize__wrap(pol,  funcdesc, args_s):
         from pypy.interpreter.baseobjspace import Wrappable
         from pypy.annotation.classdef import ClassDef
@@ -56,6 +67,19 @@
             typ = Wrappable
         else:
             assert not issubclass(typ, Wrappable)
+            if args_s[0].is_constant() and args_s[1].is_constant():
+                if typ in (str, bool, int, float):
+                    space = args_s[0].const
+                    x = args_s[1].const
+                    def fold():
+                        if typ is str and isidentifier(x):
+                            pol.count('identifier')
+                            return space.new_interned_str(x)
+                        else:
+                            pol.count(typ)
+                            return space.wrap(x)
+                    builder = specialize.make_constgraphbuilder(2, factory=fold)
+                    return funcdesc.cachedgraph(x, builder=builder)
         return funcdesc.cachedgraph(typ)
     
     def attach_lookup(pol, t, attr):



More information about the Pypy-commit mailing list