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
dd246d5d
Commit
dd246d5d
authored
Nov 16, 2016
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #28721: Fix asynchronous generators aclose() and athrow()
parent
92c4bef7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
154 additions
and
3 deletions
+154
-3
Lib/test/test_asyncgen.py
Lib/test/test_asyncgen.py
+140
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/genobject.c
Objects/genobject.c
+11
-3
No files found.
Lib/test/test_asyncgen.py
View file @
dd246d5d
...
...
@@ -450,6 +450,41 @@ class AsyncGenAsyncioTest(unittest.TestCase):
self
.
loop
.
run_until_complete
(
run
())
def
test_async_gen_asyncio_anext_06
(
self
):
DONE
=
0
# test synchronous generators
def
foo
():
try
:
yield
except
:
pass
g
=
foo
()
g
.
send
(
None
)
with
self
.
assertRaises
(
StopIteration
):
g
.
send
(
None
)
# now with asynchronous generators
async
def
gen
():
nonlocal
DONE
try
:
yield
except
:
pass
DONE
=
1
async
def
run
():
nonlocal
DONE
g
=
gen
()
await
g
.
asend
(
None
)
with
self
.
assertRaises
(
StopAsyncIteration
):
await
g
.
asend
(
None
)
DONE
+=
10
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
DONE
,
11
)
def
test_async_gen_asyncio_anext_tuple
(
self
):
async
def
foo
():
try
:
...
...
@@ -594,6 +629,76 @@ class AsyncGenAsyncioTest(unittest.TestCase):
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
DONE
,
1
)
def
test_async_gen_asyncio_aclose_10
(
self
):
DONE
=
0
# test synchronous generators
def
foo
():
try
:
yield
except
:
pass
g
=
foo
()
g
.
send
(
None
)
g
.
close
()
# now with asynchronous generators
async
def
gen
():
nonlocal
DONE
try
:
yield
except
:
pass
DONE
=
1
async
def
run
():
nonlocal
DONE
g
=
gen
()
await
g
.
asend
(
None
)
await
g
.
aclose
()
DONE
+=
10
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
DONE
,
11
)
def
test_async_gen_asyncio_aclose_11
(
self
):
DONE
=
0
# test synchronous generators
def
foo
():
try
:
yield
except
:
pass
yield
g
=
foo
()
g
.
send
(
None
)
with
self
.
assertRaisesRegex
(
RuntimeError
,
'ignored GeneratorExit'
):
g
.
close
()
# now with asynchronous generators
async
def
gen
():
nonlocal
DONE
try
:
yield
except
:
pass
yield
DONE
+=
1
async
def
run
():
nonlocal
DONE
g
=
gen
()
await
g
.
asend
(
None
)
with
self
.
assertRaisesRegex
(
RuntimeError
,
'ignored GeneratorExit'
):
await
g
.
aclose
()
DONE
+=
10
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
DONE
,
10
)
def
test_async_gen_asyncio_asend_01
(
self
):
DONE
=
0
...
...
@@ -801,6 +906,41 @@ class AsyncGenAsyncioTest(unittest.TestCase):
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
DONE
,
1
)
def
test_async_gen_asyncio_athrow_03
(
self
):
DONE
=
0
# test synchronous generators
def
foo
():
try
:
yield
except
:
pass
g
=
foo
()
g
.
send
(
None
)
with
self
.
assertRaises
(
StopIteration
):
g
.
throw
(
ValueError
)
# now with asynchronous generators
async
def
gen
():
nonlocal
DONE
try
:
yield
except
:
pass
DONE
=
1
async
def
run
():
nonlocal
DONE
g
=
gen
()
await
g
.
asend
(
None
)
with
self
.
assertRaises
(
StopAsyncIteration
):
await
g
.
athrow
(
ValueError
)
DONE
+=
10
self
.
loop
.
run_until_complete
(
run
())
self
.
assertEqual
(
DONE
,
11
)
def
test_async_gen_asyncio_athrow_tuple
(
self
):
async
def
gen
():
try
:
...
...
Misc/NEWS
View file @
dd246d5d
...
...
@@ -31,6 +31,9 @@ Core and Builtins
- Issue #26182: Fix a refleak in code that raises DeprecationWarning.
- Issue #28721: Fix asynchronous generators aclose() and athrow() to
handle StopAsyncIteration propagation properly.
Library
-------
...
...
Objects/genobject.c
View file @
dd246d5d
...
...
@@ -1931,9 +1931,17 @@ yield_close:
return
NULL
;
check_error:
if
(
PyErr_ExceptionMatches
(
PyExc_StopAsyncIteration
)
||
PyErr_ExceptionMatches
(
PyExc_GeneratorExit
)
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_StopAsyncIteration
))
{
o
->
agt_state
=
AWAITABLE_STATE_CLOSED
;
if
(
o
->
agt_args
==
NULL
)
{
/* when aclose() is called we don't want to propagate
StopAsyncIteration; just raise StopIteration, signalling
that 'aclose()' is done. */
PyErr_Clear
();
PyErr_SetNone
(
PyExc_StopIteration
);
}
}
else
if
(
PyErr_ExceptionMatches
(
PyExc_GeneratorExit
))
{
o
->
agt_state
=
AWAITABLE_STATE_CLOSED
;
PyErr_Clear
();
/* ignore these errors */
PyErr_SetNone
(
PyExc_StopIteration
);
...
...
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