mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-110378: Move to IsolatedAsyncTestCase
in test_contextlib_async.py
(#110379)
This commit is contained in:
parent
8e56d551ce
commit
6780d63ae5
1 changed files with 5 additions and 51 deletions
|
@ -2,7 +2,6 @@ import asyncio
|
||||||
from contextlib import (
|
from contextlib import (
|
||||||
asynccontextmanager, AbstractAsyncContextManager,
|
asynccontextmanager, AbstractAsyncContextManager,
|
||||||
AsyncExitStack, nullcontext, aclosing, contextmanager)
|
AsyncExitStack, nullcontext, aclosing, contextmanager)
|
||||||
import functools
|
|
||||||
from test import support
|
from test import support
|
||||||
import unittest
|
import unittest
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -11,21 +10,12 @@ from test.test_contextlib import TestBaseExitStack
|
||||||
|
|
||||||
support.requires_working_socket(module=True)
|
support.requires_working_socket(module=True)
|
||||||
|
|
||||||
def _async_test(func):
|
|
||||||
"""Decorator to turn an async function into a test case."""
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
coro = func(*args, **kwargs)
|
|
||||||
asyncio.run(coro)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
def tearDownModule():
|
def tearDownModule():
|
||||||
asyncio.set_event_loop_policy(None)
|
asyncio.set_event_loop_policy(None)
|
||||||
|
|
||||||
|
|
||||||
class TestAbstractAsyncContextManager(unittest.TestCase):
|
class TestAbstractAsyncContextManager(unittest.IsolatedAsyncioTestCase):
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_enter(self):
|
async def test_enter(self):
|
||||||
class DefaultEnter(AbstractAsyncContextManager):
|
class DefaultEnter(AbstractAsyncContextManager):
|
||||||
async def __aexit__(self, *args):
|
async def __aexit__(self, *args):
|
||||||
|
@ -37,7 +27,6 @@ class TestAbstractAsyncContextManager(unittest.TestCase):
|
||||||
async with manager as context:
|
async with manager as context:
|
||||||
self.assertIs(manager, context)
|
self.assertIs(manager, context)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_slots(self):
|
async def test_slots(self):
|
||||||
class DefaultAsyncContextManager(AbstractAsyncContextManager):
|
class DefaultAsyncContextManager(AbstractAsyncContextManager):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
@ -49,7 +38,6 @@ class TestAbstractAsyncContextManager(unittest.TestCase):
|
||||||
manager = DefaultAsyncContextManager()
|
manager = DefaultAsyncContextManager()
|
||||||
manager.var = 42
|
manager.var = 42
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_async_gen_propagates_generator_exit(self):
|
async def test_async_gen_propagates_generator_exit(self):
|
||||||
# A regression test for https://bugs.python.org/issue33786.
|
# A regression test for https://bugs.python.org/issue33786.
|
||||||
|
|
||||||
|
@ -104,9 +92,8 @@ class TestAbstractAsyncContextManager(unittest.TestCase):
|
||||||
self.assertFalse(issubclass(NoneAexit, AbstractAsyncContextManager))
|
self.assertFalse(issubclass(NoneAexit, AbstractAsyncContextManager))
|
||||||
|
|
||||||
|
|
||||||
class AsyncContextManagerTestCase(unittest.TestCase):
|
class AsyncContextManagerTestCase(unittest.IsolatedAsyncioTestCase):
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_plain(self):
|
async def test_contextmanager_plain(self):
|
||||||
state = []
|
state = []
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -120,7 +107,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
state.append(x)
|
state.append(x)
|
||||||
self.assertEqual(state, [1, 42, 999])
|
self.assertEqual(state, [1, 42, 999])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_finally(self):
|
async def test_contextmanager_finally(self):
|
||||||
state = []
|
state = []
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -138,7 +124,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
raise ZeroDivisionError()
|
raise ZeroDivisionError()
|
||||||
self.assertEqual(state, [1, 42, 999])
|
self.assertEqual(state, [1, 42, 999])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_traceback(self):
|
async def test_contextmanager_traceback(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def f():
|
async def f():
|
||||||
|
@ -194,7 +179,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
|
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
|
||||||
self.assertEqual(frames[0].line, 'raise stop_exc')
|
self.assertEqual(frames[0].line, 'raise stop_exc')
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_no_reraise(self):
|
async def test_contextmanager_no_reraise(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def whee():
|
async def whee():
|
||||||
|
@ -204,7 +188,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
# Calling __aexit__ should not result in an exception
|
# Calling __aexit__ should not result in an exception
|
||||||
self.assertFalse(await ctx.__aexit__(TypeError, TypeError("foo"), None))
|
self.assertFalse(await ctx.__aexit__(TypeError, TypeError("foo"), None))
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_trap_yield_after_throw(self):
|
async def test_contextmanager_trap_yield_after_throw(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def whoo():
|
async def whoo():
|
||||||
|
@ -217,7 +200,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
await ctx.__aexit__(TypeError, TypeError('foo'), None)
|
await ctx.__aexit__(TypeError, TypeError('foo'), None)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_trap_no_yield(self):
|
async def test_contextmanager_trap_no_yield(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def whoo():
|
async def whoo():
|
||||||
|
@ -227,7 +209,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
await ctx.__aenter__()
|
await ctx.__aenter__()
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_trap_second_yield(self):
|
async def test_contextmanager_trap_second_yield(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def whoo():
|
async def whoo():
|
||||||
|
@ -238,7 +219,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
await ctx.__aexit__(None, None, None)
|
await ctx.__aexit__(None, None, None)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_non_normalised(self):
|
async def test_contextmanager_non_normalised(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def whoo():
|
async def whoo():
|
||||||
|
@ -252,7 +232,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
with self.assertRaises(SyntaxError):
|
with self.assertRaises(SyntaxError):
|
||||||
await ctx.__aexit__(RuntimeError, None, None)
|
await ctx.__aexit__(RuntimeError, None, None)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_except(self):
|
async def test_contextmanager_except(self):
|
||||||
state = []
|
state = []
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -270,7 +249,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
raise ZeroDivisionError(999)
|
raise ZeroDivisionError(999)
|
||||||
self.assertEqual(state, [1, 42, 999])
|
self.assertEqual(state, [1, 42, 999])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_except_stopiter(self):
|
async def test_contextmanager_except_stopiter(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def woohoo():
|
async def woohoo():
|
||||||
|
@ -297,7 +275,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
self.fail(f'{stop_exc} was suppressed')
|
self.fail(f'{stop_exc} was suppressed')
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_contextmanager_wrap_runtimeerror(self):
|
async def test_contextmanager_wrap_runtimeerror(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def woohoo():
|
async def woohoo():
|
||||||
|
@ -342,14 +319,12 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
self.assertEqual(baz.__doc__, "Whee!")
|
self.assertEqual(baz.__doc__, "Whee!")
|
||||||
|
|
||||||
@support.requires_docstrings
|
@support.requires_docstrings
|
||||||
@_async_test
|
|
||||||
async def test_instance_docstring_given_cm_docstring(self):
|
async def test_instance_docstring_given_cm_docstring(self):
|
||||||
baz = self._create_contextmanager_attribs()(None)
|
baz = self._create_contextmanager_attribs()(None)
|
||||||
self.assertEqual(baz.__doc__, "Whee!")
|
self.assertEqual(baz.__doc__, "Whee!")
|
||||||
async with baz:
|
async with baz:
|
||||||
pass # suppress warning
|
pass # suppress warning
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_keywords(self):
|
async def test_keywords(self):
|
||||||
# Ensure no keyword arguments are inhibited
|
# Ensure no keyword arguments are inhibited
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -358,7 +333,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
async with woohoo(self=11, func=22, args=33, kwds=44) as target:
|
async with woohoo(self=11, func=22, args=33, kwds=44) as target:
|
||||||
self.assertEqual(target, (11, 22, 33, 44))
|
self.assertEqual(target, (11, 22, 33, 44))
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_recursive(self):
|
async def test_recursive(self):
|
||||||
depth = 0
|
depth = 0
|
||||||
ncols = 0
|
ncols = 0
|
||||||
|
@ -385,7 +359,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
self.assertEqual(ncols, 10)
|
self.assertEqual(ncols, 10)
|
||||||
self.assertEqual(depth, 0)
|
self.assertEqual(depth, 0)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_decorator(self):
|
async def test_decorator(self):
|
||||||
entered = False
|
entered = False
|
||||||
|
|
||||||
|
@ -404,7 +377,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
await test()
|
await test()
|
||||||
self.assertFalse(entered)
|
self.assertFalse(entered)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_decorator_with_exception(self):
|
async def test_decorator_with_exception(self):
|
||||||
entered = False
|
entered = False
|
||||||
|
|
||||||
|
@ -427,7 +399,6 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
await test()
|
await test()
|
||||||
self.assertFalse(entered)
|
self.assertFalse(entered)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_decorating_method(self):
|
async def test_decorating_method(self):
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -462,7 +433,7 @@ class AsyncContextManagerTestCase(unittest.TestCase):
|
||||||
self.assertEqual(test.b, 2)
|
self.assertEqual(test.b, 2)
|
||||||
|
|
||||||
|
|
||||||
class AclosingTestCase(unittest.TestCase):
|
class AclosingTestCase(unittest.IsolatedAsyncioTestCase):
|
||||||
|
|
||||||
@support.requires_docstrings
|
@support.requires_docstrings
|
||||||
def test_instance_docs(self):
|
def test_instance_docs(self):
|
||||||
|
@ -470,7 +441,6 @@ class AclosingTestCase(unittest.TestCase):
|
||||||
obj = aclosing(None)
|
obj = aclosing(None)
|
||||||
self.assertEqual(obj.__doc__, cm_docstring)
|
self.assertEqual(obj.__doc__, cm_docstring)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_aclosing(self):
|
async def test_aclosing(self):
|
||||||
state = []
|
state = []
|
||||||
class C:
|
class C:
|
||||||
|
@ -482,7 +452,6 @@ class AclosingTestCase(unittest.TestCase):
|
||||||
self.assertEqual(x, y)
|
self.assertEqual(x, y)
|
||||||
self.assertEqual(state, [1])
|
self.assertEqual(state, [1])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_aclosing_error(self):
|
async def test_aclosing_error(self):
|
||||||
state = []
|
state = []
|
||||||
class C:
|
class C:
|
||||||
|
@ -496,7 +465,6 @@ class AclosingTestCase(unittest.TestCase):
|
||||||
1 / 0
|
1 / 0
|
||||||
self.assertEqual(state, [1])
|
self.assertEqual(state, [1])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_aclosing_bpo41229(self):
|
async def test_aclosing_bpo41229(self):
|
||||||
state = []
|
state = []
|
||||||
|
|
||||||
|
@ -522,7 +490,7 @@ class AclosingTestCase(unittest.TestCase):
|
||||||
self.assertEqual(state, [1])
|
self.assertEqual(state, [1])
|
||||||
|
|
||||||
|
|
||||||
class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
class TestAsyncExitStack(TestBaseExitStack, unittest.IsolatedAsyncioTestCase):
|
||||||
class SyncAsyncExitStack(AsyncExitStack):
|
class SyncAsyncExitStack(AsyncExitStack):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run_coroutine(coro):
|
def run_coroutine(coro):
|
||||||
|
@ -561,13 +529,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
('__aexit__', 'cb_suppress = cb(*exc_details)'),
|
('__aexit__', 'cb_suppress = cb(*exc_details)'),
|
||||||
]
|
]
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(self.loop)
|
|
||||||
self.addCleanup(self.loop.close)
|
|
||||||
self.addCleanup(asyncio.set_event_loop_policy, None)
|
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_async_callback(self):
|
async def test_async_callback(self):
|
||||||
expected = [
|
expected = [
|
||||||
((), {}),
|
((), {}),
|
||||||
|
@ -610,7 +571,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
stack.push_async_callback(callback=_exit, arg=3)
|
stack.push_async_callback(callback=_exit, arg=3)
|
||||||
self.assertEqual(result, [])
|
self.assertEqual(result, [])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_async_push(self):
|
async def test_async_push(self):
|
||||||
exc_raised = ZeroDivisionError
|
exc_raised = ZeroDivisionError
|
||||||
async def _expect_exc(exc_type, exc, exc_tb):
|
async def _expect_exc(exc_type, exc, exc_tb):
|
||||||
|
@ -646,7 +606,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
self.assertIs(stack._exit_callbacks[-1][1], _expect_exc)
|
self.assertIs(stack._exit_callbacks[-1][1], _expect_exc)
|
||||||
1/0
|
1/0
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_enter_async_context(self):
|
async def test_enter_async_context(self):
|
||||||
class TestCM(object):
|
class TestCM(object):
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
|
@ -668,7 +627,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(result, [1, 2, 3, 4])
|
self.assertEqual(result, [1, 2, 3, 4])
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_enter_async_context_errors(self):
|
async def test_enter_async_context_errors(self):
|
||||||
class LacksEnterAndExit:
|
class LacksEnterAndExit:
|
||||||
pass
|
pass
|
||||||
|
@ -688,7 +646,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
await stack.enter_async_context(LacksExit())
|
await stack.enter_async_context(LacksExit())
|
||||||
self.assertFalse(stack._exit_callbacks)
|
self.assertFalse(stack._exit_callbacks)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_async_exit_exception_chaining(self):
|
async def test_async_exit_exception_chaining(self):
|
||||||
# Ensure exception chaining matches the reference behaviour
|
# Ensure exception chaining matches the reference behaviour
|
||||||
async def raise_exc(exc):
|
async def raise_exc(exc):
|
||||||
|
@ -720,7 +677,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
self.assertIsInstance(inner_exc, ValueError)
|
self.assertIsInstance(inner_exc, ValueError)
|
||||||
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
|
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_async_exit_exception_explicit_none_context(self):
|
async def test_async_exit_exception_explicit_none_context(self):
|
||||||
# Ensure AsyncExitStack chaining matches actual nested `with` statements
|
# Ensure AsyncExitStack chaining matches actual nested `with` statements
|
||||||
# regarding explicit __context__ = None.
|
# regarding explicit __context__ = None.
|
||||||
|
@ -755,7 +711,6 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
self.fail("Expected IndexError, but no exception was raised")
|
self.fail("Expected IndexError, but no exception was raised")
|
||||||
|
|
||||||
@_async_test
|
|
||||||
async def test_instance_bypass_async(self):
|
async def test_instance_bypass_async(self):
|
||||||
class Example(object): pass
|
class Example(object): pass
|
||||||
cm = Example()
|
cm = Example()
|
||||||
|
@ -768,8 +723,7 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
self.assertIs(stack._exit_callbacks[-1][1], cm)
|
self.assertIs(stack._exit_callbacks[-1][1], cm)
|
||||||
|
|
||||||
|
|
||||||
class TestAsyncNullcontext(unittest.TestCase):
|
class TestAsyncNullcontext(unittest.IsolatedAsyncioTestCase):
|
||||||
@_async_test
|
|
||||||
async def test_async_nullcontext(self):
|
async def test_async_nullcontext(self):
|
||||||
class C:
|
class C:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue