| 1 |
|
| 2 |
import asyncio |
| 3 |
import traceback |
| 4 |
import unittest |
| 5 |
from asyncio import tasks |
| 6 |
|
| 7 |
|
| 8 |
def tearDownModule(): |
| 9 |
asyncio.set_event_loop_policy(None) |
| 10 |
|
| 11 |
|
| 12 |
class FutureTests: |
| 13 |
|
| 14 |
async def test_future_traceback(self): |
| 15 |
|
| 16 |
async def raise_exc(): |
| 17 |
raise TypeError(42) |
| 18 |
|
| 19 |
future = self.cls(raise_exc()) |
| 20 |
|
| 21 |
for _ in range(5): |
| 22 |
try: |
| 23 |
await future |
| 24 |
except TypeError as e: |
| 25 |
tb = ''.join(traceback.format_tb(e.__traceback__)) |
| 26 |
self.assertEqual(tb.count("await future"), 1) |
| 27 |
else: |
| 28 |
self.fail('TypeError was not raised') |
| 29 |
|
| 30 |
@unittest.skipUnless(hasattr(tasks, '_CTask'), |
| 31 |
'requires the C _asyncio module') |
| 32 |
class CFutureTests(FutureTests, unittest.IsolatedAsyncioTestCase): |
| 33 |
try: |
| 34 |
cls = tasks._CTask |
| 35 |
except AttributeError: |
| 36 |
cls = tasks._PyTask |
| 37 |
|
| 38 |
class PyFutureTests(FutureTests, unittest.IsolatedAsyncioTestCase): |
| 39 |
cls = tasks._PyTask |
| 40 |
|
| 41 |
class FutureReprTests(unittest.IsolatedAsyncioTestCase): |
| 42 |
|
| 43 |
async def test_recursive_repr_for_pending_tasks(self): |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
async def func(): |
| 49 |
return asyncio.all_tasks() |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
self.assertIn('...', repr(await asyncio.wait_for(func(), timeout=10))) |
| 55 |
|
| 56 |
|
| 57 |
if __name__ == '__main__': |
| 58 |
unittest.main() |