]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23192: Fixed generator lambdas. Patch by Bruno Cauet.
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 11 Mar 2015 16:20:35 +0000 (18:20 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 11 Mar 2015 16:20:35 +0000 (18:20 +0200)
Lib/test/test_generators.py
Misc/ACKS
Misc/NEWS
Python/compile.c

index 7825a77339054faf81f9f91a52ac6384995cae7e..5c455cdd816c45a0177637d059fc140bb538ad49 100644 (file)
@@ -49,6 +49,26 @@ class FinalizationTest(unittest.TestCase):
         self.assertTrue(finalized)
         self.assertEqual(gc.garbage, old_garbage)
 
+    def test_lambda_generator(self):
+        # Issue #23192: Test that a lambda returning a generator behaves
+        # like the equivalent function
+        f = lambda: (yield 1)
+        def g(): return (yield 1)
+
+        # test 'yield from'
+        f2 = lambda: (yield from g())
+        def g2(): return (yield from g())
+
+        f3 = lambda: (yield from f())
+        def g3(): return (yield from f())
+
+        for gen_fun in (f, g, f2, g2, f3, g3):
+            gen = gen_fun()
+            self.assertEqual(next(gen), 1)
+            with self.assertRaises(StopIteration) as cm:
+                gen.send(2)
+            self.assertEqual(cm.exception.value, 2)
+
 
 class ExceptionTest(unittest.TestCase):
     # Tests for the issue #23353: check that the currently handled exception
index 20598abb74d361198f4683d6d6a1e1e2ffe85a2d..cf25d6f8015be275fbca1bfae22a610e3033cc12 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -216,6 +216,7 @@ Pierre Carrier
 Terry Carroll
 Edward Catmur
 Lorenzo M. Catucci
+Bruno Cauet
 Donn Cave
 Charles Cazabon
 Jesús Cea Avión
index a6073b8c43f711ba8664396fedb41edb119ff0e0..2d69b765557bd2623634bdfcf18b5478bd49790f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: tba
 Core and Builtins
 -----------------
 
+- Issue #23192: Fixed generator lambdas.  Patch by Bruno Cauet.
+
 - Issue #23629: Fix the default __sizeof__ implementation for variable-sized
   objects.
 
index b07c15647e4f432c5355c236dec98f2875b99de9..3d7152cd28cb4882468b7849c98f9772d0eda6db 100644 (file)
@@ -1897,12 +1897,12 @@ compiler_lambda(struct compiler *c, expr_ty e)
     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
     VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
     if (c->u->u_ste->ste_generator) {
-        ADDOP_IN_SCOPE(c, POP_TOP);
+        co = assemble(c, 0);
     }
     else {
         ADDOP_IN_SCOPE(c, RETURN_VALUE);
+        co = assemble(c, 1);
     }
-    co = assemble(c, 1);
     qualname = c->u->u_qualname;
     Py_INCREF(qualname);
     compiler_exit_scope(c);