From 53ffbddcbe8186e7b0dd182c724a665ffe71eafd Mon Sep 17 00:00:00 2001 From: David Cuthbert Date: Thu, 16 Nov 2017 02:04:25 -0800 Subject: [PATCH 1/6] Fix return statements to allow unpacking without parenthesis. --- Grammar/Grammar | 2 +- Lib/test/test_grammar.py | 8 +++++++- Python/graminit.c | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Grammar/Grammar b/Grammar/Grammar index 7d3dd0b86dc69a..7befa9abf17cc6 100644 --- a/Grammar/Grammar +++ b/Grammar/Grammar @@ -50,7 +50,7 @@ pass_stmt: 'pass' flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt break_stmt: 'break' continue_stmt: 'continue' -return_stmt: 'return' [testlist] +return_stmt: 'return' [testlist_star_expr] yield_stmt: yield_expr raise_stmt: 'raise' [test ['from' test]] import_stmt: import_name | import_from diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 78918ae250c4a3..213228d84b6924 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -824,11 +824,17 @@ def test_inner(extra_burning_oil = 1, count=0): test_inner() def test_return(self): - # 'return' [testlist] + # 'return' [testlist_star_expr] def g1(): return def g2(): return 1 + def g3(): + z = [2, 3] + return 1, *z + g1() x = g2() + y = g3() + self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return") check_syntax_error(self, "class foo:return 1") def test_break_in_finally(self): diff --git a/Python/graminit.c b/Python/graminit.c index 5770e8f6a9418b..5f4c2fc70d42d4 100644 --- a/Python/graminit.c +++ b/Python/graminit.c @@ -613,7 +613,7 @@ static arc arcs_25_0[1] = { {75, 1}, }; static arc arcs_25_1[2] = { - {9, 2}, + {47, 2}, {0, 1}, }; static arc arcs_25_2[1] = { From 02bd4f031372149e475187f9183f33f48b573a33 Mon Sep 17 00:00:00 2001 From: David Cuthbert Date: Wed, 22 Nov 2017 15:03:12 -0800 Subject: [PATCH 2/6] Fix yield statements to allow unpacking without parenthesis. --- Grammar/Grammar | 2 +- Python/graminit.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Grammar/Grammar b/Grammar/Grammar index 7befa9abf17cc6..e232df979e2dae 100644 --- a/Grammar/Grammar +++ b/Grammar/Grammar @@ -147,4 +147,4 @@ comp_if: 'if' test_nocond [comp_iter] encoding_decl: NAME yield_expr: 'yield' [yield_arg] -yield_arg: 'from' test | testlist +yield_arg: 'from' test | testlist_star_expr diff --git a/Python/graminit.c b/Python/graminit.c index 5f4c2fc70d42d4..0a681f7b797f48 100644 --- a/Python/graminit.c +++ b/Python/graminit.c @@ -1900,7 +1900,7 @@ static state states_85[3] = { }; static arc arcs_86_0[2] = { {77, 1}, - {9, 2}, + {47, 2}, }; static arc arcs_86_1[1] = { {26, 2}, @@ -2087,7 +2087,7 @@ static dfa dfas[87] = { {341, "yield_expr", 0, 3, states_85, "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\200\000"}, {342, "yield_arg", 0, 3, states_86, - "\000\040\200\000\000\000\000\000\000\040\010\000\000\000\020\002\000\300\220\050\037\000\000"}, + "\000\040\200\000\002\000\000\000\000\040\010\000\000\000\020\002\000\300\220\050\037\000\000"}, }; static label labels[177] = { {0, "EMPTY"}, From 47e3ac375fef7b53bd480a4103551d7fd9c02773 Mon Sep 17 00:00:00 2001 From: David Cuthbert Date: Wed, 22 Nov 2017 15:21:27 -0800 Subject: [PATCH 3/6] Add test for yield tuple unpacking. --- Lib/test/test_grammar.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 213228d84b6924..ce9a4a402731fc 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -987,6 +987,8 @@ def g(): f((yield 1)) def g(): f((yield 1), 1) def g(): f((yield from ())) def g(): f((yield from ()), 1) + # Do not require parenthesis for tuple unpacking + def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest check_syntax_error(self, "def g(): f(yield 1)") check_syntax_error(self, "def g(): f(yield 1, 1)") check_syntax_error(self, "def g(): f(yield from ())") From 31a0c8fdefad1cd228bbee859d07c2c011b89aca Mon Sep 17 00:00:00 2001 From: David Cuthbert Date: Wed, 22 Nov 2017 15:44:06 -0800 Subject: [PATCH 4/6] Add news blurb for yield and return tuple unpacking. --- .../Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst b/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst new file mode 100644 index 00000000000000..e29d3af13aa6f4 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst @@ -0,0 +1,2 @@ +Tuple unpacking is now allowed without parenthesis in yield and return +statements, e.g. ``yield 1, 2, 3, *rest``. From f84da46384eca471da982560a7829acc68d53639 Mon Sep 17 00:00:00 2001 From: Jordan Chapman Date: Sat, 15 Sep 2018 21:18:06 -0400 Subject: [PATCH 5/6] Add equality test for tuple unpacking in yield --- Lib/test/test_grammar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index ce9a4a402731fc..462e77a0be554a 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -989,6 +989,7 @@ def g(): f((yield from ())) def g(): f((yield from ()), 1) # Do not require parenthesis for tuple unpacking def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest + self.assertEquals(list(g()), [(1, 2, 3, 4, 5, 6)]) check_syntax_error(self, "def g(): f(yield 1)") check_syntax_error(self, "def g(): f(yield 1, 1)") check_syntax_error(self, "def g(): f(yield from ())") From 67303cadfaeb84a6c741fcff359639f58be9f841 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 21 Sep 2018 14:49:27 -0700 Subject: [PATCH 6/6] Touch up news entry --- .../2017-11-22-15-43-14.bpo-32117.-vloh8.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst b/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst index e29d3af13aa6f4..9685680ee53e23 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2017-11-22-15-43-14.bpo-32117.-vloh8.rst @@ -1,2 +1,3 @@ -Tuple unpacking is now allowed without parenthesis in yield and return -statements, e.g. ``yield 1, 2, 3, *rest``. +Iterable unpacking is now allowed without parenthesis in yield and return +statements, e.g. ``yield 1, 2, 3, *rest``. Thanks to David Cuthbert for the +change and jChapman for added tests.