From d8d63e7f2236067c0236d19695fe27a6a9a24a30 Mon Sep 17 00:00:00 2001 From: Vitja Makarov Date: Sun, 9 Jan 2011 19:44:37 +0300 Subject: [PATCH] Support yield inside lambda --- Cython/Compiler/ParseTreeTransforms.py | 17 ++++++++++++----- tests/run/generators.pyx | 12 ++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 01be309d..4e759632 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -191,14 +191,20 @@ class PostParse(ScopeTrackingTransform): lambda_id = self.lambda_counter self.lambda_counter += 1 node.lambda_name = EncodedString(u'lambda%d' % lambda_id) - - body = Nodes.ReturnStatNode( - node.result_expr.pos, value = node.result_expr) + collector = YieldNodeCollector() + collector.visitchildren(node.result_expr) + if collector.yields or isinstance(node.result_expr, ExprNodes.YieldExprNode): + body = ExprNodes.YieldExprNode( + node.result_expr.pos, arg=node.result_expr) + body = Nodes.ExprStatNode(node.result_expr.pos, expr=body) + else: + body = Nodes.ReturnStatNode( + node.result_expr.pos, value=node.result_expr) node.def_node = Nodes.DefNode( node.pos, name=node.name, lambda_name=node.lambda_name, args=node.args, star_arg=node.star_arg, starstar_arg=node.starstar_arg, - body=body) + body=body, doc=None) self.visitchildren(node) return node @@ -1383,7 +1389,8 @@ class MarkClosureVisitor(CythonTransform): starstar_arg=node.starstar_arg, doc=node.doc, decorators=node.decorators, - gbody=gbody) + gbody=gbody, + lambda_name=node.lambda_name) return generator return node diff --git a/tests/run/generators.pyx b/tests/run/generators.pyx index 53047284..af8926c8 100644 --- a/tests/run/generators.pyx +++ b/tests/run/generators.pyx @@ -287,3 +287,15 @@ def test_nested_yield(): StopIteration """ yield (yield (yield 1)) + +def test_inside_lambda(): + """ + >>> obj = test_inside_lambda()() + >>> next(obj) + 1 + >>> obj.send('a') + 2 + >>> obj.send('b') + ('a', 'b') + """ + return lambda:((yield 1), (yield 2)) -- 2.26.2