22from contextlib import (
33 asynccontextmanager , AbstractAsyncContextManager ,
44 AsyncExitStack , nullcontext , aclosing , contextmanager )
5- import functools
65from test import support
76import unittest
87import traceback
1110
1211support .requires_working_socket (module = True )
1312
14- def _async_test (func ):
15- """Decorator to turn an async function into a test case."""
16- @functools .wraps (func )
17- def wrapper (* args , ** kwargs ):
18- coro = func (* args , ** kwargs )
19- asyncio .run (coro )
20- return wrapper
21-
2213def tearDownModule ():
2314 asyncio .set_event_loop_policy (None )
2415
2516
26- class TestAbstractAsyncContextManager (unittest .TestCase ):
17+ class TestAbstractAsyncContextManager (unittest .IsolatedAsyncioTestCase ):
2718
28- @_async_test
2919 async def test_enter (self ):
3020 class DefaultEnter (AbstractAsyncContextManager ):
3121 async def __aexit__ (self , * args ):
@@ -37,7 +27,6 @@ async def __aexit__(self, *args):
3727 async with manager as context :
3828 self .assertIs (manager , context )
3929
40- @_async_test
4130 async def test_slots (self ):
4231 class DefaultAsyncContextManager (AbstractAsyncContextManager ):
4332 __slots__ = ()
@@ -49,7 +38,6 @@ async def __aexit__(self, *args):
4938 manager = DefaultAsyncContextManager ()
5039 manager .var = 42
5140
52- @_async_test
5341 async def test_async_gen_propagates_generator_exit (self ):
5442 # A regression test for https://bugs.python.org/issue33786.
5543
@@ -104,9 +92,8 @@ class NoneAexit(ManagerFromScratch):
10492 self .assertFalse (issubclass (NoneAexit , AbstractAsyncContextManager ))
10593
10694
107- class AsyncContextManagerTestCase (unittest .TestCase ):
95+ class AsyncContextManagerTestCase (unittest .IsolatedAsyncioTestCase ):
10896
109- @_async_test
11097 async def test_contextmanager_plain (self ):
11198 state = []
11299 @asynccontextmanager
@@ -120,7 +107,6 @@ async def woohoo():
120107 state .append (x )
121108 self .assertEqual (state , [1 , 42 , 999 ])
122109
123- @_async_test
124110 async def test_contextmanager_finally (self ):
125111 state = []
126112 @asynccontextmanager
@@ -138,7 +124,6 @@ async def woohoo():
138124 raise ZeroDivisionError ()
139125 self .assertEqual (state , [1 , 42 , 999 ])
140126
141- @_async_test
142127 async def test_contextmanager_traceback (self ):
143128 @asynccontextmanager
144129 async def f ():
@@ -194,7 +179,6 @@ class StopAsyncIterationSubclass(StopAsyncIteration):
194179 self .assertEqual (frames [0 ].name , 'test_contextmanager_traceback' )
195180 self .assertEqual (frames [0 ].line , 'raise stop_exc' )
196181
197- @_async_test
198182 async def test_contextmanager_no_reraise (self ):
199183 @asynccontextmanager
200184 async def whee ():
@@ -204,7 +188,6 @@ async def whee():
204188 # Calling __aexit__ should not result in an exception
205189 self .assertFalse (await ctx .__aexit__ (TypeError , TypeError ("foo" ), None ))
206190
207- @_async_test
208191 async def test_contextmanager_trap_yield_after_throw (self ):
209192 @asynccontextmanager
210193 async def whoo ():
@@ -217,7 +200,6 @@ async def whoo():
217200 with self .assertRaises (RuntimeError ):
218201 await ctx .__aexit__ (TypeError , TypeError ('foo' ), None )
219202
220- @_async_test
221203 async def test_contextmanager_trap_no_yield (self ):
222204 @asynccontextmanager
223205 async def whoo ():
@@ -227,7 +209,6 @@ async def whoo():
227209 with self .assertRaises (RuntimeError ):
228210 await ctx .__aenter__ ()
229211
230- @_async_test
231212 async def test_contextmanager_trap_second_yield (self ):
232213 @asynccontextmanager
233214 async def whoo ():
@@ -238,7 +219,6 @@ async def whoo():
238219 with self .assertRaises (RuntimeError ):
239220 await ctx .__aexit__ (None , None , None )
240221
241- @_async_test
242222 async def test_contextmanager_non_normalised (self ):
243223 @asynccontextmanager
244224 async def whoo ():
@@ -252,7 +232,6 @@ async def whoo():
252232 with self .assertRaises (SyntaxError ):
253233 await ctx .__aexit__ (RuntimeError , None , None )
254234
255- @_async_test
256235 async def test_contextmanager_except (self ):
257236 state = []
258237 @asynccontextmanager
@@ -270,7 +249,6 @@ async def woohoo():
270249 raise ZeroDivisionError (999 )
271250 self .assertEqual (state , [1 , 42 , 999 ])
272251
273- @_async_test
274252 async def test_contextmanager_except_stopiter (self ):
275253 @asynccontextmanager
276254 async def woohoo ():
@@ -297,7 +275,6 @@ class StopAsyncIterationSubclass(StopAsyncIteration):
297275 else :
298276 self .fail (f'{ stop_exc } was suppressed' )
299277
300- @_async_test
301278 async def test_contextmanager_wrap_runtimeerror (self ):
302279 @asynccontextmanager
303280 async def woohoo ():
@@ -342,14 +319,12 @@ def test_contextmanager_doc_attrib(self):
342319 self .assertEqual (baz .__doc__ , "Whee!" )
343320
344321 @support .requires_docstrings
345- @_async_test
346322 async def test_instance_docstring_given_cm_docstring (self ):
347323 baz = self ._create_contextmanager_attribs ()(None )
348324 self .assertEqual (baz .__doc__ , "Whee!" )
349325 async with baz :
350326 pass # suppress warning
351327
352- @_async_test
353328 async def test_keywords (self ):
354329 # Ensure no keyword arguments are inhibited
355330 @asynccontextmanager
@@ -358,7 +333,6 @@ async def woohoo(self, func, args, kwds):
358333 async with woohoo (self = 11 , func = 22 , args = 33 , kwds = 44 ) as target :
359334 self .assertEqual (target , (11 , 22 , 33 , 44 ))
360335
361- @_async_test
362336 async def test_recursive (self ):
363337 depth = 0
364338 ncols = 0
@@ -385,7 +359,6 @@ async def recursive():
385359 self .assertEqual (ncols , 10 )
386360 self .assertEqual (depth , 0 )
387361
388- @_async_test
389362 async def test_decorator (self ):
390363 entered = False
391364
@@ -404,7 +377,6 @@ async def test():
404377 await test ()
405378 self .assertFalse (entered )
406379
407- @_async_test
408380 async def test_decorator_with_exception (self ):
409381 entered = False
410382
@@ -427,7 +399,6 @@ async def test():
427399 await test ()
428400 self .assertFalse (entered )
429401
430- @_async_test
431402 async def test_decorating_method (self ):
432403
433404 @asynccontextmanager
@@ -462,15 +433,14 @@ async def method(self, a, b, c=None):
462433 self .assertEqual (test .b , 2 )
463434
464435
465- class AclosingTestCase (unittest .TestCase ):
436+ class AclosingTestCase (unittest .IsolatedAsyncioTestCase ):
466437
467438 @support .requires_docstrings
468439 def test_instance_docs (self ):
469440 cm_docstring = aclosing .__doc__
470441 obj = aclosing (None )
471442 self .assertEqual (obj .__doc__ , cm_docstring )
472443
473- @_async_test
474444 async def test_aclosing (self ):
475445 state = []
476446 class C :
@@ -482,7 +452,6 @@ async def aclose(self):
482452 self .assertEqual (x , y )
483453 self .assertEqual (state , [1 ])
484454
485- @_async_test
486455 async def test_aclosing_error (self ):
487456 state = []
488457 class C :
@@ -496,7 +465,6 @@ async def aclose(self):
496465 1 / 0
497466 self .assertEqual (state , [1 ])
498467
499- @_async_test
500468 async def test_aclosing_bpo41229 (self ):
501469 state = []
502470
@@ -522,7 +490,7 @@ async def agenfunc():
522490 self .assertEqual (state , [1 ])
523491
524492
525- class TestAsyncExitStack (TestBaseExitStack , unittest .TestCase ):
493+ class TestAsyncExitStack (TestBaseExitStack , unittest .IsolatedAsyncioTestCase ):
526494 class SyncAsyncExitStack (AsyncExitStack ):
527495 @staticmethod
528496 def run_coroutine (coro ):
@@ -561,13 +529,6 @@ def __exit__(self, *exc_details):
561529 ('__aexit__' , 'cb_suppress = cb(*exc_details)' ),
562530 ]
563531
564- def setUp (self ):
565- self .loop = asyncio .new_event_loop ()
566- asyncio .set_event_loop (self .loop )
567- self .addCleanup (self .loop .close )
568- self .addCleanup (asyncio .set_event_loop_policy , None )
569-
570- @_async_test
571532 async def test_async_callback (self ):
572533 expected = [
573534 ((), {}),
@@ -610,7 +571,6 @@ async def _exit(*args, **kwds):
610571 stack .push_async_callback (callback = _exit , arg = 3 )
611572 self .assertEqual (result , [])
612573
613- @_async_test
614574 async def test_async_push (self ):
615575 exc_raised = ZeroDivisionError
616576 async def _expect_exc (exc_type , exc , exc_tb ):
@@ -646,7 +606,6 @@ async def __aexit__(self, *exc_details):
646606 self .assertIs (stack ._exit_callbacks [- 1 ][1 ], _expect_exc )
647607 1 / 0
648608
649- @_async_test
650609 async def test_enter_async_context (self ):
651610 class TestCM (object ):
652611 async def __aenter__ (self ):
@@ -668,7 +627,6 @@ async def _exit():
668627
669628 self .assertEqual (result , [1 , 2 , 3 , 4 ])
670629
671- @_async_test
672630 async def test_enter_async_context_errors (self ):
673631 class LacksEnterAndExit :
674632 pass
@@ -688,7 +646,6 @@ async def __aenter__(self):
688646 await stack .enter_async_context (LacksExit ())
689647 self .assertFalse (stack ._exit_callbacks )
690648
691- @_async_test
692649 async def test_async_exit_exception_chaining (self ):
693650 # Ensure exception chaining matches the reference behaviour
694651 async def raise_exc (exc ):
@@ -720,7 +677,6 @@ async def suppress_exc(*exc_details):
720677 self .assertIsInstance (inner_exc , ValueError )
721678 self .assertIsInstance (inner_exc .__context__ , ZeroDivisionError )
722679
723- @_async_test
724680 async def test_async_exit_exception_explicit_none_context (self ):
725681 # Ensure AsyncExitStack chaining matches actual nested `with` statements
726682 # regarding explicit __context__ = None.
@@ -755,7 +711,6 @@ async def my_cm_with_exit_stack():
755711 else :
756712 self .fail ("Expected IndexError, but no exception was raised" )
757713
758- @_async_test
759714 async def test_instance_bypass_async (self ):
760715 class Example (object ): pass
761716 cm = Example ()
@@ -768,8 +723,7 @@ class Example(object): pass
768723 self .assertIs (stack ._exit_callbacks [- 1 ][1 ], cm )
769724
770725
771- class TestAsyncNullcontext (unittest .TestCase ):
772- @_async_test
726+ class TestAsyncNullcontext (unittest .IsolatedAsyncioTestCase ):
773727 async def test_async_nullcontext (self ):
774728 class C :
775729 pass
0 commit comments