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
b8ab9d3f
Commit
b8ab9d3f
authored
Oct 06, 2017
by
Yury Selivanov
Committed by
GitHub
Oct 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-31708: Allow async generator expressions in synchronous functions (#3905)
parent
faa135ac
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
9 deletions
+52
-9
Doc/reference/expressions.rst
Doc/reference/expressions.rst
+10
-8
Lib/test/test_asyncgen.py
Lib/test/test_asyncgen.py
+32
-0
Lib/test/test_coroutines.py
Lib/test/test_coroutines.py
+8
-0
Misc/NEWS.d/next/Core and Builtins/2017-10-06-02-10-48.bpo-31708.66CCVU.rst
...ore and Builtins/2017-10-06-02-10-48.bpo-31708.66CCVU.rst
+1
-0
Python/compile.c
Python/compile.c
+1
-1
No files found.
Doc/reference/expressions.rst
View file @
b8ab9d3f
...
...
@@ -326,14 +326,16 @@ range(10) for y in bar(x))``.
The parentheses can be omitted on calls with only one argument. See section
:ref:`calls` for details.
Since Python 3.6, if the generator appears in an :keyword:`async def` function,
then :keyword:`async for` clauses and :keyword:`await` expressions are permitted
as with an asynchronous comprehension. If a generator expression
contains either :keyword:`async for` clauses or :keyword:`await` expressions
it is called an :dfn:`asynchronous generator expression`.
An asynchronous generator expression yields a new asynchronous
generator object, which is an asynchronous iterator
(see :ref:`async-iterators`).
If a generator expression contains either :keyword:`async for`
clauses or :keyword:`await` expressions it is called an
:dfn:`asynchronous generator expression`. An asynchronous generator
expression returns a new asynchronous generator object,
which is an asynchronous iterator (see :ref:`async-iterators`).
.. versionchanged:: 3.7
Prior to Python 3.7, asynchronous generator expressions could
only appear in :keyword:`async def` coroutines. Starting
with 3.7, any function can use asynchronous generator expressions.
.. _yieldexpr:
...
...
Lib/test/test_asyncgen.py
View file @
b8ab9d3f
...
...
@@ -1037,5 +1037,37 @@ class AsyncGenAsyncioTest(unittest.TestCase):
t
.
cancel
()
self
.
loop
.
run_until_complete
(
asyncio
.
sleep
(
0.1
,
loop
=
self
.
loop
))
def
test_async_gen_expression_01
(
self
):
async
def
arange
(
n
):
for
i
in
range
(
n
):
await
asyncio
.
sleep
(
0.01
,
loop
=
self
.
loop
)
yield
i
def
make_arange
(
n
):
# This syntax is legal starting with Python 3.7
return
(
i
*
2
async
for
i
in
arange
(
n
))
async
def
run
():
return
[
i
async
for
i
in
make_arange
(
10
)]
res
=
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
res
,
[
i
*
2
for
i
in
range
(
10
)])
def
test_async_gen_expression_02
(
self
):
async
def
wrap
(
n
):
await
asyncio
.
sleep
(
0.01
,
loop
=
self
.
loop
)
return
n
def
make_arange
(
n
):
# This syntax is legal starting with Python 3.7
return
(
i
*
2
for
i
in
range
(
n
)
if
await
wrap
(
i
))
async
def
run
():
return
[
i
async
for
i
in
make_arange
(
10
)]
res
=
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
res
,
[
i
*
2
for
i
in
range
(
1
,
10
)])
if
__name__
==
"__main__"
:
unittest
.
main
()
Lib/test/test_coroutines.py
View file @
b8ab9d3f
...
...
@@ -149,6 +149,14 @@ class AsyncBadSyntaxTest(unittest.TestCase):
[i async for i in els]
"""
,
"""def bar():
{i: i async for i in els}
"""
,
"""def bar():
{i async for i in els}
"""
,
"""def bar():
[await i for i in els]
"""
,
...
...
Misc/NEWS.d/next/Core and Builtins/2017-10-06-02-10-48.bpo-31708.66CCVU.rst
0 → 100644
View file @
b8ab9d3f
Allow use of asynchronous generator expressions in synchronous functions.
Python/compile.c
View file @
b8ab9d3f
...
...
@@ -3974,7 +3974,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
is_async_generator
=
c
->
u
->
u_ste
->
ste_coroutine
;
if
(
is_async_generator
&&
!
is_async_function
)
{
if
(
is_async_generator
&&
!
is_async_function
&&
type
!=
COMP_GENEXP
)
{
if
(
e
->
lineno
>
c
->
u
->
u_lineno
)
{
c
->
u
->
u_lineno
=
e
->
lineno
;
c
->
u
->
u_lineno_set
=
0
;
...
...
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