[Python-checkins] bpo-29922: Add more tests for error messages in 'async with'. (GH-6370)
Miss Islington (bot)
webhook-mailer at python.org
Wed Apr 4 13:18:59 EDT 2018
https://github.com/python/cpython/commit/1487cd14bb1e04de5b98fffc5ec41c6cf6b5d5f1
commit: 1487cd14bb1e04de5b98fffc5ec41c6cf6b5d5f1
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-04-04T10:18:56-07:00
summary:
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'.
(cherry picked from commit 2eeac269dd1e04a2a179384576986c3e47895ee0)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
M Lib/test/test_coroutines.py
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index 769879f082c5..d0b44c44bd54 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -1243,6 +1243,7 @@ class CM:
def __aexit__(self, *e):
return 444
+ # Exit with exception
async def foo():
async with CM():
1/0
@@ -1270,19 +1271,58 @@ class CM:
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):
More information about the Python-checkins
mailing list