[pypy-commit] pypy py3k: support keyword-only args on lambdas

gutworth noreply at buildbot.pypy.org
Thu Mar 15 04:35:44 CET 2012


Author: Benjamin Peterson <benjamin at python.org>
Branch: py3k
Changeset: r53661:c8427075f5fc
Date: 2012-03-14 22:35 -0500
http://bitbucket.org/pypy/pypy/changeset/c8427075f5fc/

Log:	support keyword-only args on lambdas

diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -354,10 +354,15 @@
         self.update_position(lam.lineno)
         args = lam.args
         assert isinstance(args, ast.arguments)
+        kw_default_count = 0
+        if args.kwonlyargs:
+            kw_default_count = self._visit_kwonlydefaults(args)
         self.visit_sequence(args.defaults)
         default_count = len(args.defaults) if args.defaults is not None else 0
         code = self.sub_scope(LambdaCodeGenerator, "<lambda>", lam, lam.lineno)
-        self._make_function(code, default_count)
+        oparg = default_count
+        oparg |= kw_default_count << 8
+        self._make_function(code, oparg)
 
     def visit_ClassDef(self, cls):
         self.update_position(cls.lineno, True)
@@ -1243,6 +1248,8 @@
         assert isinstance(args, ast.arguments)
         if args.args:
             self.argcount = len(args.args)
+        if args.kwonlyargs:
+            self.kwonlyargcount = len(args.kwonlyargs)
         # Prevent a string from being the first constant and thus a docstring.
         self.add_const(self.space.w_None)
         lam.body.walkabout(self)
diff --git a/pypy/interpreter/test/test_interpreter.py b/pypy/interpreter/test/test_interpreter.py
--- a/pypy/interpreter/test/test_interpreter.py
+++ b/pypy/interpreter/test/test_interpreter.py
@@ -341,3 +341,10 @@
         assert mixedargs_sum.__code__.co_varnames == ("a", "b", "k1", "k2", "args")
         assert mixedargs_sum(1, k1=2) == 1 + 2
         """
+
+    def test_kwonlyargs_lambda(self):
+        """
+        l = lambda x, y, *, k=20: x+y+k
+        assert l(1, 2) == 1 + 2 + 20
+        assert l(1, 2, k=10) == 1 + 2 + 10
+        """


More information about the pypy-commit mailing list