Skip to content

Commit c32e7c7

Browse files
committed
remove asyncio from contextlib async tests
1 parent 63140b4 commit c32e7c7

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

Lib/test/test_contextlib_async.py

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
from contextlib import (
32
asynccontextmanager, AbstractAsyncContextManager,
43
AsyncExitStack, nullcontext, aclosing, contextmanager)
@@ -8,22 +7,47 @@
87

98
from test.test_contextlib import TestBaseExitStack
109

11-
support.requires_working_socket(module=True)
1210

13-
def _async_test(func):
11+
def _run_async_fn(async_fn, /, *args, **kwargs):
12+
coro = async_fn(*args, **kwargs)
13+
gen = type(coro).__await__(coro)
14+
try:
15+
gen.send(None)
16+
except StopIteration as e:
17+
return e.value
18+
else:
19+
raise AssertionError("coroutine did not stop")
20+
finally:
21+
gen.close()
22+
23+
24+
def _async_test(async_fn):
1425
"""Decorator to turn an async function into a test case."""
15-
@functools.wraps(func)
26+
@functools.wraps(async_fn)
1627
def wrapper(*args, **kwargs):
17-
coro = func(*args, **kwargs)
18-
asyncio.run(coro)
19-
return wrapper
28+
return _run_async_fn(async_fn, *args, **kwargs)
2029

21-
def tearDownModule():
22-
asyncio.set_event_loop_policy(None)
30+
return wrapper
2331

2432

2533
class TestAbstractAsyncContextManager(unittest.TestCase):
2634

35+
def test_async_test_self_test(self):
36+
class _async_yield:
37+
def __init__(self, v):
38+
self.v = v
39+
40+
def __await__(self):
41+
return (yield self.v)
42+
43+
@_async_test
44+
async def do_not_stop_coro():
45+
while True:
46+
await _async_yield(None)
47+
48+
with self.assertRaisesRegex(AssertionError, "coroutine did not stop"):
49+
do_not_stop_coro()
50+
2751
@_async_test
2852
async def test_enter(self):
2953
class DefaultEnter(AbstractAsyncContextManager):
@@ -455,49 +479,24 @@ async def agenfunc():
455479

456480
class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
457481
class SyncAsyncExitStack(AsyncExitStack):
458-
@staticmethod
459-
def run_coroutine(coro):
460-
loop = asyncio.get_event_loop_policy().get_event_loop()
461-
t = loop.create_task(coro)
462-
t.add_done_callback(lambda f: loop.stop())
463-
loop.run_forever()
464-
465-
exc = t.exception()
466-
if not exc:
467-
return t.result()
468-
else:
469-
context = exc.__context__
470-
471-
try:
472-
raise exc
473-
except:
474-
exc.__context__ = context
475-
raise exc
476482

477483
def close(self):
478-
return self.run_coroutine(self.aclose())
484+
return _run_async_fn(self.aclose)
479485

480486
def __enter__(self):
481-
return self.run_coroutine(self.__aenter__())
487+
return _run_async_fn(self.__aenter__)
482488

483489
def __exit__(self, *exc_details):
484-
return self.run_coroutine(self.__aexit__(*exc_details))
490+
return _run_async_fn(self.__aexit__, *exc_details)
485491

486492
exit_stack = SyncAsyncExitStack
487493
callback_error_internal_frames = [
488-
('__exit__', 'return self.run_coroutine(self.__aexit__(*exc_details))'),
489-
('run_coroutine', 'raise exc'),
490-
('run_coroutine', 'raise exc'),
494+
('__exit__', 'return _run_async_fn(self.__aexit__, *exc_details)'),
495+
('_run_async_fn', 'gen.send(None)'),
491496
('__aexit__', 'raise exc_details[1]'),
492497
('__aexit__', 'cb_suppress = cb(*exc_details)'),
493498
]
494499

495-
def setUp(self):
496-
self.loop = asyncio.new_event_loop()
497-
asyncio.set_event_loop(self.loop)
498-
self.addCleanup(self.loop.close)
499-
self.addCleanup(asyncio.set_event_loop_policy, None)
500-
501500
@_async_test
502501
async def test_async_callback(self):
503502
expected = [

0 commit comments

Comments
 (0)