summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib_async.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-06-29 08:28:15 (GMT)
committerGitHub <noreply@github.com>2021-06-29 08:28:15 (GMT)
commit6cb145d23f5cf69b6d7414877d142747cd3d134c (patch)
tree0617efcd6911c14a1b8003bb9bee5fc68e521103 /Lib/test/test_contextlib_async.py
parent20a88004bae8ead66a205a125e1fe979376fc3ea (diff)
downloadcpython-6cb145d23f5cf69b6d7414877d142747cd3d134c.zip
cpython-6cb145d23f5cf69b6d7414877d142747cd3d134c.tar.gz
cpython-6cb145d23f5cf69b6d7414877d142747cd3d134c.tar.bz2
bpo-44471: Change error type for bad objects in ExitStack.enter_context() (GH-26820)
A TypeError is now raised instead of an AttributeError in ExitStack.enter_context() and AsyncExitStack.enter_async_context() for objects which do not support the context manager or asynchronous context manager protocols correspondingly.
Diffstat (limited to 'Lib/test/test_contextlib_async.py')
-rw-r--r--Lib/test/test_contextlib_async.py34
1 files changed, 33 insertions, 1 deletions
diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py
index cbc82df..7904abf 100644
--- a/Lib/test/test_contextlib_async.py
+++ b/Lib/test/test_contextlib_async.py
@@ -483,7 +483,7 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
1/0
@_async_test
- async def test_async_enter_context(self):
+ async def test_enter_async_context(self):
class TestCM(object):
async def __aenter__(self):
result.append(1)
@@ -505,6 +505,26 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
self.assertEqual(result, [1, 2, 3, 4])
@_async_test
+ async def test_enter_async_context_errors(self):
+ class LacksEnterAndExit:
+ pass
+ class LacksEnter:
+ async def __aexit__(self, *exc_info):
+ pass
+ class LacksExit:
+ async def __aenter__(self):
+ pass
+
+ async with self.exit_stack() as stack:
+ with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
+ await stack.enter_async_context(LacksEnterAndExit())
+ with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
+ await stack.enter_async_context(LacksEnter())
+ with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
+ await stack.enter_async_context(LacksExit())
+ self.assertFalse(stack._exit_callbacks)
+
+ @_async_test
async def test_async_exit_exception_chaining(self):
# Ensure exception chaining matches the reference behaviour
async def raise_exc(exc):
@@ -536,6 +556,18 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
self.assertIsInstance(inner_exc, ValueError)
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
+ @_async_test
+ async def test_instance_bypass_async(self):
+ class Example(object): pass
+ cm = Example()
+ cm.__aenter__ = object()
+ cm.__aexit__ = object()
+ stack = self.exit_stack()
+ with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
+ await stack.enter_async_context(cm)
+ stack.push_async_exit(cm)
+ self.assertIs(stack._exit_callbacks[-1][1], cm)
+
class TestAsyncNullcontext(unittest.TestCase):
@_async_test