Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7c9150

Browse files
authored
Fix wrapping into StopIteration of return values in generators and coroutines (#644)
1 parent 2b27e2e commit b7c9150

2 files changed

Lines changed: 16 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):

Objects/genobject.c

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

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

0 commit comments

Comments
 (0)