Skip to content

Commit 6316b28

Browse files
SyntaxColoringseifertm
authored andcommitted
Deduplicate simplefilter snippet.
1 parent 3ffdfc5 commit 6316b28

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

pytest_asyncio/plugin.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,7 @@ def _patched_collect():
657657
def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[None]:
658658
old_loop_policy = asyncio.get_event_loop_policy()
659659
try:
660-
with warnings.catch_warnings():
661-
warnings.simplefilter("ignore", DeprecationWarning)
662-
old_loop = asyncio.get_event_loop()
660+
old_loop = _get_event_loop_no_warn()
663661
except RuntimeError:
664662
old_loop = None
665663
asyncio.set_event_loop_policy(policy)
@@ -673,9 +671,7 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No
673671
# subsequent tests from side-effects. We close this loop before restoring
674672
# the old loop to avoid ResourceWarnings.
675673
try:
676-
with warnings.catch_warnings():
677-
warnings.simplefilter("ignore", DeprecationWarning)
678-
asyncio.get_event_loop().close()
674+
_get_event_loop_no_warn().close()
679675
except RuntimeError:
680676
pass
681677
asyncio.set_event_loop(old_loop)
@@ -765,9 +761,7 @@ def pytest_fixture_setup(
765761
)
766762
policy = asyncio.get_event_loop_policy()
767763
try:
768-
with warnings.catch_warnings():
769-
warnings.simplefilter("ignore", DeprecationWarning)
770-
old_loop = policy.get_event_loop()
764+
old_loop = _get_event_loop_no_warn(policy)
771765
is_pytest_asyncio_loop = getattr(old_loop, "__pytest_asyncio", False)
772766
if old_loop is not loop and not is_pytest_asyncio_loop:
773767
old_loop.close()
@@ -829,9 +823,7 @@ def _restore_policy():
829823
# Close any event loop associated with the old loop policy
830824
# to avoid ResourceWarnings in the _provide_clean_event_loop finalizer
831825
try:
832-
with warnings.catch_warnings():
833-
warnings.simplefilter("ignore", DeprecationWarning)
834-
loop = previous_policy.get_event_loop()
826+
loop = _get_event_loop_no_warn(previous_policy)
835827
except RuntimeError:
836828
loop = None
837829
if loop:
@@ -853,6 +845,17 @@ def _provide_clean_event_loop() -> None:
853845
policy.set_event_loop(new_loop)
854846

855847

848+
def _get_event_loop_no_warn(
849+
policy: Optional[AbstractEventLoopPolicy] = None,
850+
) -> asyncio.AbstractEventLoop:
851+
with warnings.catch_warnings():
852+
warnings.simplefilter("ignore", DeprecationWarning)
853+
if policy is not None:
854+
return policy.get_event_loop()
855+
else:
856+
return asyncio.get_event_loop()
857+
858+
856859
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
857860
def pytest_pyfunc_call(pyfuncitem: Function) -> Optional[object]:
858861
"""
@@ -893,9 +896,7 @@ def wrap_in_sync(
893896
@functools.wraps(func)
894897
def inner(*args, **kwargs):
895898
coro = func(*args, **kwargs)
896-
with warnings.catch_warnings():
897-
warnings.simplefilter("ignore", DeprecationWarning)
898-
_loop = asyncio.get_event_loop()
899+
_loop = _get_event_loop_no_warn()
899900
task = asyncio.ensure_future(coro, loop=_loop)
900901
try:
901902
_loop.run_until_complete(task)

0 commit comments

Comments
 (0)