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

pedronis at codespeak.net pedronis at codespeak.net
Sat Jul 23 00:06:09 CEST 2005


Author: pedronis
Date: Sat Jul 23 00:06:08 2005
New Revision: 14944

Modified:
   pypy/dist/pypy/rpython/normalizecalls.py
   pypy/dist/pypy/rpython/rpbc.py
   pypy/dist/pypy/rpython/test/test_rpbc.py
Log:
some support for multiple functions with multiple patterns as long as the functions have identical signatures (defaults 
included); the parser contains some instances of this, it is reasonable enough to support. With test.

 --This line, and those below, will be ignored--

M    rpython/rpbc.py
M    rpython/test/test_rpbc.py
M    rpython/normalizecalls.py


Modified: pypy/dist/pypy/rpython/normalizecalls.py
==============================================================================
--- pypy/dist/pypy/rpython/normalizecalls.py	(original)
+++ pypy/dist/pypy/rpython/normalizecalls.py	Sat Jul 23 00:06:08 2005
@@ -67,11 +67,21 @@
                           if classdef is None and
                 not isinstance(func, (type, types.ClassType, types.MethodType))]
         if len(functions) > 1:  # otherwise, nothing to do
+            fully_normalized = True
             if len(family.patterns) > 1:
-                raise TyperError("don't support multiple call patterns "
-                                 "to multiple functions for now %r" % (
-                    functions))
-            pattern, = family.patterns
+                argspec = inspect.getargspec(functions[0])
+                for func in functions:
+                    if inspect.getargspec(func) != argspec:
+                        raise TyperError("don't support multiple call patterns "
+                                 "to multiple functions with different signatures for now %r" % (
+                                functions))
+                args, varargs, varkwds, _ = argspec
+                assert varargs is None, "XXX not implemented"
+                assert varkwds is None, "XXX not implemented"
+                pattern = (len(args), (), False, False)
+                fully_normalized = False
+            else:
+                pattern, = family.patterns
             shape_cnt, shape_keys, shape_star, shape_stst = pattern
             assert not shape_star, "XXX not implemented"
             assert not shape_stst, "XXX not implemented"
@@ -150,7 +160,7 @@
                     # finished
                     checkgraph(graph)
                     annotator.annotated[newblock] = annotator.annotated[oldblock]
-                graph.normalized_for_calls = True
+                graph.normalized_for_calls = fully_normalized
                 # convert the return value too
                 annotator.setbinding(graph.getreturnvar(), generalizedresult)
 

Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Sat Jul 23 00:06:08 2005
@@ -340,6 +340,14 @@
         rresult = Void
     return f, rinputs, rresult
 
+def samesig(funcs):
+    import inspect
+    argspec = inspect.getargspec(funcs[0])
+    for func in funcs:
+        if inspect.getargspec(func) != argspec:
+            return False
+    return True
+
 
 class FunctionsPBCRepr(Repr):
     """Representation selected for a PBC of function(s)."""
@@ -389,8 +397,10 @@
             vlist = hop.inputargs(self, *rinputs)
         else:
             # if not normalized, should be a call to a known function
-            assert len(self.function_signatures()) == 1, "normalization bug"
-            func, = self.function_signatures().keys()
+            # or to functions all with same signature
+            funcs = self.function_signatures().keys()
+            assert samesig(funcs), "normalization bug"
+            func = funcs[0]
             vlist = [hop.inputarg(self, arg=0)]
             vlist += callparse.callparse('simple_call', func, rinputs, hop)
 
@@ -412,8 +422,10 @@
             vlist = vlist[:1] + vlist[2:]
         else:
             # if not normalized, should be a call to a known function
-            assert len(self.function_signatures()) == 1, "normalization bug"
-            func, = self.function_signatures().keys()
+            # or to functions all with same signature
+            funcs = self.function_signatures().keys()
+            assert samesig(funcs), "normalization bug"
+            func = funcs[0]
             vlist = [hop.inputarg(self, arg=0)] 
             vlist += callparse.callparse('call_args', func, rinputs, hop)
 

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	Sat Jul 23 00:06:08 2005
@@ -763,3 +763,29 @@
 
     res = interpret(f, [3, 0])
     assert res == 5
+
+def test_various_patterns_but_one_signature_method():
+    class A:
+        def meth(self, a, b=0):
+            raise NotImplementedError
+    class B(A):
+        def meth(self, a, b=0):
+            return a+b
+        
+    class C(A):
+        def meth(self, a, b=0):
+            return a*b
+    def f(i):
+        if i == 0:
+            x = B()
+        else:
+            x = C()
+        r1 = x.meth(1)
+        r2 = x.meth(3, 2)
+        r3 = x.meth(7, b=11)
+        return r1+r2+r3
+    res = interpret(f, [0])
+    assert res == 1+3+2+7+11
+    res = interpret(f, [1])
+    assert res == 3*2+11*7
+    



More information about the Pypy-commit mailing list