[issue35877] parenthesis is mandatory for named expressions in while statement

Karthikeyan Singaravelan report at bugs.python.org
Fri Feb 1 10:49:32 EST 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

> Emily, I think this would be as simple as making a tiny change to Grammar/Grammar and running make regen-grammar. Can you take care of that?

Thanks, can confirm that this fixes the issue. I changed test to namedexpr_test for while statement in grammar and ran `make regen-grammar` and `make`. The reported program runs fine and Lib/test/test_named_expressions.py also passes.

➜  cpython git:(master) ✗ cat /tmp/foo.py
import re

with open("/tmp/foo.py") as f:
    while line := f.readline():
        if match := re.search(r"foo", line):
            print(match.string.strip("\n"))
➜  cpython git:(master) ✗ ./python.exe /tmp/foo.py
with open("/tmp/foo.py") as f:
        if match := re.search(r"foo", line):


Patch : 

➜  cpython git:(master) ✗ git diff | cat
diff --git a/Grammar/Grammar b/Grammar/Grammar
index e65a688e4c..a425978059 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -72,7 +72,7 @@ assert_stmt: 'assert' test [',' test]
 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
 async_stmt: 'async' (funcdef | with_stmt | for_stmt)
 if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
-while_stmt: 'while' test ':' suite ['else' ':' suite]
+while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]
 for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite]
 try_stmt: ('try' ':' suite
            ((except_clause ':' suite)+
diff --git a/Python/graminit.c b/Python/graminit.c
index 6e0f19891b..5cdde2789c 100644
--- a/Python/graminit.c
+++ b/Python/graminit.c
@@ -971,7 +971,7 @@ static arc arcs_42_0[1] = {
     {103, 1},
 };
 static arc arcs_42_1[1] = {
-    {26, 2},
+    {99, 2},
 };
 static arc arcs_42_2[1] = {
     {27, 3},

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35877>
_______________________________________


More information about the Python-bugs-list mailing list