Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/reference/simple_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ The :keyword:`return` statement
pair: class; definition

.. productionlist::
return_stmt: "return" [`expression_list`]
return_stmt: "return" [`starred_expression`]

:keyword:`return` may only occur syntactically nested in a function definition,
not within a nested class definition.

If an expression list is present, it is evaluated, else ``None`` is substituted.
If an expression is present, it is evaluated, else ``None`` is substituted.

:keyword:`return` leaves the current function call with the expression list (or
``None``) as return value.
Expand Down
2 changes: 1 addition & 1 deletion Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Lib/lib2to3/Grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 | ',' test [',' test]]]
import_stmt: import_name | import_from
Expand Down
5 changes: 4 additions & 1 deletion Lib/lib2to3/tests/data/py3_test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,15 @@ def test_inner(extra_burning_oil = 1, count=0):
test_inner()

def testReturn(self):
# 'return' [testlist]
# 'return' [testlist_star_expr]
def g1(): return
def g2(): return 1
def g3(): return *(1, 2), 3
g1()
x = g2()
g3()
check_syntax_error(self, "class foo:return 1")
check_syntax_error(self, "def f(): return *(1, 2, 3)")

def testYield(self):
check_syntax_error(self, "class foo:yield 1")
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,15 @@ 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(): return *(1, 2), 3
g1()
x = g2()
g3()
check_syntax_error(self, "class foo:return 1")
check_syntax_error(self, "def f(): return *(1, 2, 3)")

def test_break_in_finally(self):
count = 0
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_flags_passed(self):
def check_suite(self, s):
self.roundtrip(parser.suite, s)

def test_return_statement(self):
self.check_suite("def f(): return *(1, 2), 3")

def test_yield_statement(self):
self.check_suite("def f(): yield 1")
self.check_suite("def f(): yield")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow unparenthesized star-unpacking expressions in return statements.
2 changes: 1 addition & 1 deletion Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -3096,7 +3096,7 @@ ast_for_flow_stmt(struct compiling *c, const node *n)
| yield_stmt
break_stmt: 'break'
continue_stmt: 'continue'
return_stmt: 'return' [testlist]
return_stmt: 'return' [testlist_star_expr]
yield_stmt: yield_expr
yield_expr: 'yield' testlist | 'yield' 'from' test
raise_stmt: 'raise' [test [',' test [',' test]]]
Expand Down
2 changes: 1 addition & 1 deletion Python/graminit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down