Commit 8c5adff1 authored by Stefan Behnel's avatar Stefan Behnel

fix *args/**kwargs in lambda function declaration: was messed up with annotation syntax

parent 915d8861
...@@ -2568,23 +2568,23 @@ def p_varargslist(s, terminator=')', annotated=1): ...@@ -2568,23 +2568,23 @@ def p_varargslist(s, terminator=')', annotated=1):
if s.sy == '*': if s.sy == '*':
s.next() s.next()
if s.sy == 'IDENT': if s.sy == 'IDENT':
star_arg = p_py_arg_decl(s) star_arg = p_py_arg_decl(s, annotated=annotated)
if s.sy == ',': if s.sy == ',':
s.next() s.next()
args.extend(p_c_arg_list(s, in_pyfunc = 1, args.extend(p_c_arg_list(s, in_pyfunc = 1,
nonempty_declarators = 1, kw_only = 1)) nonempty_declarators = 1, kw_only = 1, annotated = annotated))
elif s.sy != terminator: elif s.sy != terminator:
s.error("Syntax error in Python function argument list") s.error("Syntax error in Python function argument list")
if s.sy == '**': if s.sy == '**':
s.next() s.next()
starstar_arg = p_py_arg_decl(s) starstar_arg = p_py_arg_decl(s, annotated=annotated)
return (args, star_arg, starstar_arg) return (args, star_arg, starstar_arg)
def p_py_arg_decl(s): def p_py_arg_decl(s, annotated = 1):
pos = s.position() pos = s.position()
name = p_ident(s) name = p_ident(s)
annotation = None annotation = None
if s.sy == ':': if annotated and s.sy == ':':
s.next() s.next()
annotation = p_test(s) annotation = p_test(s)
return Nodes.PyArgDeclNode(pos, name = name, annotation = annotation) return Nodes.PyArgDeclNode(pos, name = name, annotation = annotation)
......
...@@ -76,3 +76,35 @@ def make_typed_lambda_lambda(int x): ...@@ -76,3 +76,35 @@ def make_typed_lambda_lambda(int x):
""" """
return lambda int y : \ return lambda int y : \
lambda int z:x+y+z lambda int z:x+y+z
def pass_lambda(f):
"""
>>> def f(a, lfunc): return lfunc(a,2)
>>> pass_lambda(f)
12
"""
return f(1, lambda a, b : a*10+b)
def pass_lambda_with_args(f):
"""
>>> def f(a, lfunc): return lfunc(a,2,3)
>>> pass_lambda_with_args(f)
123
"""
return f(1, lambda a, *args : (a*10 + args[0])*10 + args[1])
def pass_lambda_with_args_kwargs(f):
"""
>>> def f(a, lfunc): return lfunc(a,2,3, b=4)
>>> pass_lambda_with_args_kwargs(f)
1234
"""
return f(1, lambda a, *args, **kwargs : ((a*10 + args[0])*10 + args[1])*10 + kwargs['b'])
def pass_lambda_with_args_kwargs_kwonly_args(f):
"""
>>> def f(a, lfunc): return lfunc(a,2,3, b=4, c=5)
>>> pass_lambda_with_args_kwargs_kwonly_args(f)
12345
"""
return f(1, lambda a, *args, b, **kwargs : (((a*10 + args[0])*10 + args[1])*10 + b)*10 + kwargs['c'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment