diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-04-04 15:45:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-04 15:45:10 (GMT) |
commit | 2eeac269dd1e04a2a179384576986c3e47895ee0 (patch) | |
tree | 08dfda1235ff1b18c6671031eae666f32c50baec /Lib/test/test_coroutines.py | |
parent | c51d8c9ba6211d77db639487501f89aa9b4bcbb1 (diff) | |
download | cpython-2eeac269dd1e04a2a179384576986c3e47895ee0.zip cpython-2eeac269dd1e04a2a179384576986c3e47895ee0.tar.gz cpython-2eeac269dd1e04a2a179384576986c3e47895ee0.tar.bz2 |
bpo-29922: Add more tests for error messages in 'async with'. (GH-6370)
Different paths are executed for normal exit and for leaving
the 'async with' block with 'break', 'continue' or 'return'.
Diffstat (limited to 'Lib/test/test_coroutines.py')
-rw-r--r-- | Lib/test/test_coroutines.py | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 9ad7ff9..10f7cca 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -1269,6 +1269,7 @@ class CoroutineTest(unittest.TestCase): def __aexit__(self, *e): return 444 + # Exit with exception async def foo(): async with CM(): 1/0 @@ -1296,19 +1297,58 @@ class CoroutineTest(unittest.TestCase): def __aexit__(self, *e): return 456 + # Normal exit async def foo(): nonlocal CNT async with CM(): CNT += 1 + with self.assertRaisesRegex( + TypeError, + "'async with' received an object from __aexit__ " + "that does not implement __await__: int"): + run_async(foo()) + self.assertEqual(CNT, 1) + # Exit with 'break' + async def foo(): + nonlocal CNT + for i in range(2): + async with CM(): + CNT += 1 + break + with self.assertRaisesRegex( + TypeError, + "'async with' received an object from __aexit__ " + "that does not implement __await__: int"): + run_async(foo()) + self.assertEqual(CNT, 2) + # Exit with 'continue' + async def foo(): + nonlocal CNT + for i in range(2): + async with CM(): + CNT += 1 + continue with self.assertRaisesRegex( TypeError, "'async with' received an object from __aexit__ " "that does not implement __await__: int"): run_async(foo()) + self.assertEqual(CNT, 3) - self.assertEqual(CNT, 1) + # Exit with 'return' + async def foo(): + nonlocal CNT + async with CM(): + CNT += 1 + return + with self.assertRaisesRegex( + TypeError, + "'async with' received an object from __aexit__ " + "that does not implement __await__: int"): + run_async(foo()) + self.assertEqual(CNT, 4) def test_with_9(self): |