Commit 4f6db40e authored by Stefan Behnel's avatar Stefan Behnel

support for lambda inside of generator expressions

parent 9c1d1a26
......@@ -558,8 +558,7 @@ class Scope(object):
def declare_lambda_function(self, func_cname, pos):
# Add an entry for an anonymous Python function.
entry = self.declare_var(None, py_object_type, pos,
cname=func_cname, visibility='private')
entry = self.declare(None, func_cname, py_object_type, pos, 'private')
entry.name = EncodedString(func_cname)
entry.func_cname = func_cname
entry.signature = pyfunction_signature
......@@ -1401,6 +1400,12 @@ class GeneratorExpressionScope(Scope):
self.entries[name] = entry
return entry
def declare_lambda_function(self, func_cname, pos):
return self.outer_scope.declare_lambda_function(func_cname, pos)
def add_lambda_def(self, def_node):
return self.outer_scope.add_lambda_def(def_node)
class ClosureScope(LocalScope):
......
# cython: language_level=3
# mode: run
# tag: generators, python3
cimport cython
......@@ -89,6 +91,16 @@ def list_comp():
assert x == 'abc' # don't leak in Py3 code
return result
def list_comp_with_lambda():
"""
>>> list_comp_with_lambda()
[0, 4, 8]
"""
x = 'abc'
result = [x*2 for x in range(5) if (lambda x:x % 2)(x) == 0]
assert x == 'abc' # don't leak in Py3 code
return result
module_level_lc = [ module_level_loopvar*2 for module_level_loopvar in range(4) ]
def list_comp_module_level():
"""
......
# mode: run
# tag: generators, lambda
def genexpr():
"""
>>> genexpr()
[0, 2, 4, 6, 8]
"""
x = 'abc'
result = list( x*2 for x in range(5) )
assert x == 'abc' # don't leak
return result
def genexpr_if():
"""
>>> genexpr_if()
[0, 4, 8]
"""
x = 'abc'
result = list( x*2 for x in range(5) if x % 2 == 0 )
assert x == 'abc' # don't leak
return result
def genexpr_with_lambda():
"""
>>> genexpr_with_lambda()
[0, 4, 8]
"""
x = 'abc'
result = list( x*2 for x in range(5) if (lambda x:x % 2)(x) == 0 )
assert x == 'abc' # don't leak
return result
def genexpr_of_lambdas(int N):
"""
>>> [ (f(), g()) for f,g in genexpr_of_lambdas(5) ]
[(0, 0), (1, 2), (2, 4), (3, 6), (4, 8)]
"""
return ( ((lambda : x), (lambda : x*2)) for x in range(N) )
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