Skip to content

Commit fa448de

Browse files
authored
Fix wrapping into StopIteration of return values in generators and coroutines (#644) (#647)
1 parent aac875f commit fa448de

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

Lib/test/test_coroutines.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,21 @@ async def waiter(coro):
11031103
"coroutine is being awaited already"):
11041104
waiter(coro).send(None)
11051105

1106+
def test_await_16(self):
1107+
# See https://bugs.python.org/issue29600 for details.
1108+
1109+
async def f():
1110+
return ValueError()
1111+
1112+
async def g():
1113+
try:
1114+
raise KeyError
1115+
except:
1116+
return await f()
1117+
1118+
_, result = run_async(g())
1119+
self.assertIsNone(result.__context__)
1120+
11061121
def test_with_1(self):
11071122
class Manager:
11081123
def __init__(self, name):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.6.1 final?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29600: Fix wrapping coroutine return values in StopIteration.
14+
1315
- bpo-29723: The ``sys.path[0]`` initialization change for bpo-29139 caused a
1416
regression by revealing an inconsistency in how sys.path is initialized when
1517
executing ``__main__`` from a zipfile, directory, or other import location.

Objects/genobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ _PyGen_SetStopIterationValue(PyObject *value)
575575
PyObject *e;
576576

577577
if (value == NULL ||
578-
(!PyTuple_Check(value) &&
579-
!PyObject_TypeCheck(value, (PyTypeObject *) PyExc_StopIteration)))
578+
(!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
580579
{
581580
/* Delay exception instantiation if we can */
582581
PyErr_SetObject(PyExc_StopIteration, value);

0 commit comments

Comments
 (0)