Skip to content

Commit 51dabc4

Browse files
Martiuswebgvanrossum
authored andcommitted
make CoroWrapper.throw fully compatible with gen.throw (python#430)
1 parent 5016e76 commit 51dabc4

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

asyncio/coroutines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def send(self, *value):
120120
def send(self, value):
121121
return self.gen.send(value)
122122

123-
def throw(self, exc):
124-
return self.gen.throw(exc)
123+
def throw(self, type, value=None, traceback=None):
124+
return self.gen.throw(type, value, traceback)
125125

126126
def close(self):
127127
return self.gen.close()

tests/test_tasks.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,37 @@ def foo(): yield from []
17231723
wd['cw'] = cw # Would fail without __weakref__ slot.
17241724
cw.gen = None # Suppress warning from __del__.
17251725

1726+
def test_corowrapper_throw(self):
1727+
# Issue 429: CoroWrapper.throw must be compatible with gen.throw
1728+
def foo():
1729+
value = None
1730+
while True:
1731+
try:
1732+
value = yield value
1733+
except Exception as e:
1734+
value = e
1735+
1736+
exception = Exception("foo")
1737+
cw = asyncio.coroutines.CoroWrapper(foo())
1738+
cw.send(None)
1739+
self.assertIs(exception, cw.throw(exception))
1740+
1741+
cw = asyncio.coroutines.CoroWrapper(foo())
1742+
cw.send(None)
1743+
self.assertIs(exception, cw.throw(Exception, exception))
1744+
1745+
cw = asyncio.coroutines.CoroWrapper(foo())
1746+
cw.send(None)
1747+
exception = cw.throw(Exception, "foo")
1748+
self.assertIsInstance(exception, Exception)
1749+
self.assertEqual(exception.args, ("foo", ))
1750+
1751+
cw = asyncio.coroutines.CoroWrapper(foo())
1752+
cw.send(None)
1753+
exception = cw.throw(Exception, "foo", None)
1754+
self.assertIsInstance(exception, Exception)
1755+
self.assertEqual(exception.args, ("foo", ))
1756+
17261757
@unittest.skipUnless(PY34,
17271758
'need python 3.4 or later')
17281759
def test_log_destroyed_pending_task(self):

0 commit comments

Comments
 (0)