[pypy-svn] r7449 - in pypy/trunk/src/pypy/translator: . test

arigo at codespeak.net arigo at codespeak.net
Fri Nov 19 16:18:28 CET 2004


Author: arigo
Date: Fri Nov 19 16:18:27 2004
New Revision: 7449

Modified:
   pypy/trunk/src/pypy/translator/genc.py
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_ctrans.py
Log:
Support for default arguments in generated C.  Wrote tests.


Modified: pypy/trunk/src/pypy/translator/genc.py
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.py	(original)
+++ pypy/trunk/src/pypy/translator/genc.py	Fri Nov 19 16:18:27 2004
@@ -374,6 +374,7 @@
 ##             func.__name__)
         f = self.f
         body = list(self.cfunction_body(func))
+        name_of_defaults = [self.nameof(x) for x in (func.func_defaults or ())]
         self.gen_global_declarations()
 
         # print header
@@ -411,28 +412,28 @@
             print >> f, '\t\tPy_DECREF(%s);' % vararg
             print >> f, '\t\treturn NULL;'
             print >> f, '\t}'
-            lst = ['args',
-                   '"%s"' % func.__name__,
-                   '%d' % len(positional_args),
-                   '%d' % len(positional_args),
-                   ]
-            lst += ['&' + a.name for a in positional_args]
-            print >> f, '\tif (!PyArg_UnpackTuple(%s)) {' % ', '.join(lst)
-            print >> f, '\t\tPy_DECREF(args);'
-            print >> f, '\t\tPy_DECREF(%s);' % vararg
-            print >> f, '\t\treturn NULL;'
-            print >> f, '\t}'
-            print >> f, '\tPy_DECREF(args);'
+            tail = """{
+\t\tPy_DECREF(args);
+\t\tPy_DECREF(%s);
+\t\treturn NULL;
+\t}
+\tPy_DECREF(args);""" % vararg
         else:
             positional_args = graph.getargs()
-            lst = ['args',
-                   '"%s"' % func.__name__,
-                   '%d' % len(positional_args),
-                   '%d' % len(positional_args),
-                   ]
-            lst += ['&' + a.name for a in positional_args]
-            print >> f, '\tif (!PyArg_UnpackTuple(%s))' % ', '.join(lst)
-            print >> f, '\t\treturn NULL;'
+            tail = '\n\t\treturn NULL;'
+        min_number_of_args = len(positional_args) - len(name_of_defaults)
+        for i in range(len(name_of_defaults)):
+            print >> f, '\t%s = %s;' % (
+                positional_args[min_number_of_args+i],
+                name_of_defaults[i])
+        lst = ['args',
+               '"%s"' % func.__name__,
+               '%d' % min_number_of_args,
+               '%d' % len(positional_args),
+               ]
+        lst += ['&' + a.name for a in positional_args]
+        print >> f, '\tif (!PyArg_UnpackTuple(%s))' % ', '.join(lst),
+        print >> f, tail
 
         # generate an incref for each input argument
         for v in positional_args:

Modified: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/snippet.py	(original)
+++ pypy/trunk/src/pypy/translator/test/snippet.py	Fri Nov 19 16:18:27 2004
@@ -480,6 +480,19 @@
 def call_star_args(z):
     return star_args(z, 5, 10, 15, 20)
 
+def default_args(x, y=2, z=3):
+    return x+y+z
+
+def call_default_args(u):
+    return default_args(111, u)
+
+def default_and_star_args(x, y=2, z=3, *more):
+    return x+y+z+len(more)
+
+def call_default_and_star_args(u):
+    return (default_and_star_args(111, u),
+            default_and_star_args(-1000, -2000, -3000, -4000, -5000))
+
 def powerset(setsize=int):
     """Powerset
 

Modified: pypy/trunk/src/pypy/translator/test/test_ctrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_ctrans.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_ctrans.py	Fri Nov 19 16:18:27 2004
@@ -130,6 +130,16 @@
         call_star_args = self.build_cfunc(snippet.call_star_args)
         self.assertEquals(call_star_args(42), 52)
 
+    def test_call_default_args(self):
+        call_default_args = self.build_cfunc(snippet.call_default_args)
+        self.assertEquals(call_default_args(42), 111+42+3)
+
+    def test_call_default_and_star_args(self):
+        call_default_and_star_args = self.build_cfunc(
+            snippet.call_default_and_star_args)
+        self.assertEquals(call_default_and_star_args(42),
+                          (111+42+3+0, -1000-2000-3000+2))
+
 class TypedTestCase(testit.IntTestCase):
 
     def getcompiled(self, func):



More information about the Pypy-commit mailing list