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

arigo at codespeak.net arigo at codespeak.net
Sat Jun 18 20:41:55 CEST 2005


Author: arigo
Date: Sat Jun 18 20:41:53 2005
New Revision: 13585

Modified:
   pypy/dist/pypy/rpython/rpbc.py
   pypy/dist/pypy/rpython/test/test_rpbc.py
Log:
simple_call adding default arguments.


Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Sat Jun 18 20:41:53 2005
@@ -250,7 +250,25 @@
 
     def rtype_simple_call(self, hop):
         f, rinputs, rresult = self.function_signatures.itervalues().next()
-        vlist = hop.inputargs(self, *rinputs)
+        defaultclist = []
+        if len(rinputs) != hop.nb_args-1:  # argument count mismatch
+            assert not getattr(f._obj.graph, 'normalized_for_calls', False), (
+                "normalization bug")
+            assert len(self.function_signatures) == 1, "normalization bug too"
+            func, = self.function_signatures.keys()
+            defaults = func.func_defaults or ()
+            if len(rinputs) - len(defaults) <= hop.nb_args-1 <= len(rinputs):
+                rinputs = list(rinputs)
+                defaults = list(defaults)
+                while len(rinputs) != hop.nb_args-1:
+                    c = hop.inputconst(rinputs.pop(), defaults.pop())
+                    defaultclist.insert(0, c)
+            else:
+                if hop.nb_args-1 > len(rinputs):
+                    raise RTyperError("too many arguments in function call")
+                else:
+                    raise RTyperError("not enough arguments in function call")
+        vlist = hop.inputargs(self, *rinputs) + defaultclist
         if self.lowleveltype == Void:
             assert len(self.function_signatures) == 1
             vlist[0] = hop.inputconst(typeOf(f), f)

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 Jun 18 20:41:53 2005
@@ -127,3 +127,19 @@
         return MyBase.m(inst, 2)
     res = interpret(f, [])
     assert res == 42
+
+def test_call_defaults():
+    def g(a, b=2, c=3):
+        return a+b+c
+    def f1():
+        return g(1)
+    def f2():
+        return g(1, 10)
+    def f3():
+        return g(1, 10, 100)
+    res = interpret(f1, [])
+    assert res == 1+2+3
+    res = interpret(f2, [])
+    assert res == 1+10+3
+    res = interpret(f3, [])
+    assert res == 1+10+100



More information about the Pypy-commit mailing list