Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
07ca9afa
Commit
07ca9afa
authored
Feb 04, 2018
by
Serhiy Storchaka
Committed by
GitHub
Feb 04, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-10544: Disallow "yield" in comprehensions and generator expressions. (GH-4564)
parent
8b5fa289
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
32 additions
and
49 deletions
+32
-49
Doc/reference/expressions.rst
Doc/reference/expressions.rst
+9
-14
Doc/whatsnew/3.8.rst
Doc/whatsnew/3.8.rst
+9
-0
Lib/test/support/__init__.py
Lib/test/support/__init__.py
+2
-2
Lib/test/test_grammar.py
Lib/test/test_grammar.py
+3
-9
Misc/NEWS.d/next/Core and Builtins/2017-11-26-00-59-22.bpo-10544.fHOM3V.rst
...ore and Builtins/2017-11-26-00-59-22.bpo-10544.fHOM3V.rst
+2
-0
Python/symtable.c
Python/symtable.c
+7
-24
No files found.
Doc/reference/expressions.rst
View file @
07ca9afa
...
...
@@ -196,8 +196,7 @@ they may depend on the values obtained from the leftmost iterable. For example:
To ensure the comprehension always results in a container of the appropriate
type, ``yield`` and ``yield from`` expressions are prohibited in the implicitly
nested scope (in Python 3.7, such expressions emit :exc:`DeprecationWarning`
when compiled, in Python 3.8+ they will emit :exc:`SyntaxError`).
nested scope.
Since Python 3.6, in an :keyword:`async def` function, an :keyword:`async for`
clause may be used to iterate over a :term:`asynchronous iterator`.
...
...
@@ -214,8 +213,8 @@ See also :pep:`530`.
.. versionadded:: 3.6
Asynchronous comprehensions were introduced.
..
deprecated:: 3.7
``yield`` and ``yield from``
depreca
ted in the implicitly nested scope.
..
versionchanged:: 3.8
``yield`` and ``yield from``
prohibi
ted in the implicitly nested scope.
.. _lists:
...
...
@@ -350,9 +349,7 @@ The parentheses can be omitted on calls with only one argument. See section
To avoid interfering with the expected operation of the generator expression
itself, ``yield`` and ``yield from`` expressions are prohibited in the
implicitly defined generator (in Python 3.7, such expressions emit
:exc:`DeprecationWarning` when compiled, in Python 3.8+ they will emit
:exc:`SyntaxError`).
implicitly defined generator.
If a generator expression contains either :keyword:`async for`
clauses or :keyword:`await` expressions it is called an
...
...
@@ -368,8 +365,8 @@ which is an asynchronous iterator (see :ref:`async-iterators`).
only appear in :keyword:`async def` coroutines. Starting
with 3.7, any function can use asynchronous generator expressions.
..
deprecated:: 3.7
``yield`` and ``yield from``
depreca
ted in the implicitly nested scope.
..
versionchanged:: 3.8
``yield`` and ``yield from``
prohibi
ted in the implicitly nested scope.
.. _yieldexpr:
...
...
@@ -401,12 +398,10 @@ coroutine function to be an asynchronous generator. For example::
Due to their side effects on the containing scope, ``yield`` expressions
are not permitted as part of the implicitly defined scopes used to
implement comprehensions and generator expressions (in Python 3.7, such
expressions emit :exc:`DeprecationWarning` when compiled, in Python 3.8+
they will emit :exc:`SyntaxError`)..
implement comprehensions and generator expressions.
..
deprecated:: 3.7
Yield expressions
depreca
ted in the implicitly nested scopes used to
..
versionchanged:: 3.8
Yield expressions
prohibi
ted in the implicitly nested scopes used to
implement comprehensions and generator expressions.
Generator functions are described below, while asynchronous generator
...
...
Doc/whatsnew/3.8.rst
View file @
07ca9afa
...
...
@@ -113,6 +113,15 @@ This section lists previously described changes and other bugfixes
that may require changes to your code.
Changes in Python behavior
--------------------------
* Yield expressions (both ``yield`` and ``yield from`` clauses) are now disallowed
in comprehensions and generator expressions (aside from the iterable expression
in the leftmost :keyword:`for` clause).
(Contributed by Serhiy Storchaka in :issue:`10544`.)
Changes in the Python API
-------------------------
...
...
Lib/test/support/__init__.py
View file @
07ca9afa
...
...
@@ -1061,8 +1061,8 @@ def make_bad_fd():
file
.
close
()
unlink
(
TESTFN
)
def
check_syntax_error
(
testcase
,
statement
,
*
,
lineno
=
None
,
offset
=
None
):
with
testcase
.
assertRaises
(
SyntaxError
)
as
cm
:
def
check_syntax_error
(
testcase
,
statement
,
errtext
=
''
,
*
,
lineno
=
None
,
offset
=
None
):
with
testcase
.
assertRaises
Regex
(
SyntaxError
,
errtext
)
as
cm
:
compile
(
statement
,
'<test string>'
,
'exec'
)
err
=
cm
.
exception
testcase
.
assertIsNotNone
(
err
.
lineno
)
...
...
Lib/test/test_grammar.py
View file @
07ca9afa
...
...
@@ -251,6 +251,8 @@ class CNS:
class
GrammarTests
(
unittest
.
TestCase
):
check_syntax_error
=
check_syntax_error
# single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
# XXX can't test in a script -- this rule is only used when interactive
...
...
@@ -920,15 +922,7 @@ class GrammarTests(unittest.TestCase):
def
g
():
[
x
for
x
in
[(
yield
1
)]]
def
g
():
[
x
for
x
in
[(
yield
from
())]]
def
check
(
code
,
warntext
):
with
self
.
assertWarnsRegex
(
DeprecationWarning
,
warntext
):
compile
(
code
,
'<test string>'
,
'exec'
)
import
warnings
with
warnings
.
catch_warnings
():
warnings
.
filterwarnings
(
'error'
,
category
=
DeprecationWarning
)
with
self
.
assertRaisesRegex
(
SyntaxError
,
warntext
):
compile
(
code
,
'<test string>'
,
'exec'
)
check
=
self
.
check_syntax_error
check
(
"def g(): [(yield x) for x in ()]"
,
"'yield' inside list comprehension"
)
check
(
"def g(): [x for x in () if not (yield x)]"
,
...
...
Misc/NEWS.d/next/Core and Builtins/2017-11-26-00-59-22.bpo-10544.fHOM3V.rst
0 → 100644
View file @
07ca9afa
Yield expressions are now disallowed in comprehensions and generator
expressions except the expression for the outermost iterable.
Python/symtable.c
View file @
07ca9afa
...
...
@@ -1754,35 +1754,18 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
VISIT
(
st
,
expr
,
value
);
VISIT
(
st
,
expr
,
elt
);
if
(
st
->
st_cur
->
ste_generator
)
{
Py
Object
*
msg
=
PyUnicode_FromString
(
Py
Err_SetString
(
PyExc_SyntaxError
,
(
e
->
kind
==
ListComp_kind
)
?
"'yield' inside list comprehension"
:
(
e
->
kind
==
SetComp_kind
)
?
"'yield' inside set comprehension"
:
(
e
->
kind
==
DictComp_kind
)
?
"'yield' inside dict comprehension"
:
"'yield' inside generator expression"
);
if
(
msg
==
NULL
)
{
symtable_exit_block
(
st
,
(
void
*
)
e
);
return
0
;
}
if
(
PyErr_WarnExplicitObject
(
PyExc_DeprecationWarning
,
msg
,
st
->
st_filename
,
st
->
st_cur
->
ste_lineno
,
NULL
,
NULL
)
==
-
1
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_DeprecationWarning
))
{
/* Replace the DeprecationWarning exception with a SyntaxError
to get a more accurate error report */
PyErr_Clear
();
PyErr_SetObject
(
PyExc_SyntaxError
,
msg
);
PyErr_SyntaxLocationObject
(
st
->
st_filename
,
st
->
st_cur
->
ste_lineno
,
st
->
st_cur
->
ste_col_offset
);
}
Py_DECREF
(
msg
);
symtable_exit_block
(
st
,
(
void
*
)
e
);
return
0
;
}
Py_DECREF
(
msg
);
PyErr_SyntaxLocationObject
(
st
->
st_filename
,
st
->
st_cur
->
ste_lineno
,
st
->
st_cur
->
ste_col_offset
);
symtable_exit_block
(
st
,
(
void
*
)
e
);
return
0
;
}
st
->
st_cur
->
ste_generator
|
=
is_generator
;
st
->
st_cur
->
ste_generator
=
is_generator
;
return
symtable_exit_block
(
st
,
(
void
*
)
e
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment