Issue
I would like to ask question on handling asyncio.CancelledError from IsolatedAsyncioTestCase, which is provided for testing asyncio on py3.8
Given following testcases
import unittest
from unittest import IsolatedAsyncioTestCase
import asyncio
class Test(IsolatedAsyncioTestCase):
def setUp(self):
print('setup')
async def asyncSetUp(self):
print('async setup')
async def test_response(self):
print('test_response')
self.addAsyncCleanup(self.on_cleanup)
def tearDown(self):
print('teardown')
async def asyncTearDown(self):
print('before async teardown')
fut = asyncio.Future()
fut.cancel()
await fut
print('after async teardown')
async def on_cleanup(self):
print('clean up')
if __name__ == "__main__":
unittest.main()
It got sucked on tearDown
root@1318a3fe59d0:/# python --version
Python 3.8.0
root@1318a3fe59d0:/# python /workspace/b.py
setup
async setup
test_response
before async teardown
....
After digging into the error stack (by pressing CTRL+C to terminate the process), and source codes from github, these are codes related to this error.
- https://github.com/python/cpython/blob/7264e92b718d307cc499b2f10eab7644b00f0499/Lib/unittest/async_case.py#L126
- https://github.com/python/cpython/blob/7264e92b718d307cc499b2f10eab7644b00f0499/Lib/unittest/async_case.py#L105
Error asyncio.CancelledError has been selectively raised but not other exception. So, I am wondering how to catch and handle this error instead of hanging into the terminal.
Solution
Recently, I found a similar issue from python's ticket system.
Hopefully, it was resolved on 2020-12-16. Moreover, by checking what they modified in 3.8 branch, they change how they catch exceptions.
So, as long as you have latest python, which include this fix, you should not have this issue again.
Answered By - Mond Wan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.