@@ -657,9 +657,7 @@ def _patched_collect():
657
657
def _temporary_event_loop_policy (policy : AbstractEventLoopPolicy ) -> Iterator [None ]:
658
658
old_loop_policy = asyncio .get_event_loop_policy ()
659
659
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 ()
663
661
except RuntimeError :
664
662
old_loop = None
665
663
asyncio .set_event_loop_policy (policy )
@@ -673,9 +671,7 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No
673
671
# subsequent tests from side-effects. We close this loop before restoring
674
672
# the old loop to avoid ResourceWarnings.
675
673
try :
676
- with warnings .catch_warnings ():
677
- warnings .simplefilter ("ignore" , DeprecationWarning )
678
- asyncio .get_event_loop ().close ()
674
+ _get_event_loop_no_warn ().close ()
679
675
except RuntimeError :
680
676
pass
681
677
asyncio .set_event_loop (old_loop )
@@ -765,9 +761,7 @@ def pytest_fixture_setup(
765
761
)
766
762
policy = asyncio .get_event_loop_policy ()
767
763
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 )
771
765
is_pytest_asyncio_loop = getattr (old_loop , "__pytest_asyncio" , False )
772
766
if old_loop is not loop and not is_pytest_asyncio_loop :
773
767
old_loop .close ()
@@ -829,9 +823,7 @@ def _restore_policy():
829
823
# Close any event loop associated with the old loop policy
830
824
# to avoid ResourceWarnings in the _provide_clean_event_loop finalizer
831
825
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 )
835
827
except RuntimeError :
836
828
loop = None
837
829
if loop :
@@ -853,6 +845,17 @@ def _provide_clean_event_loop() -> None:
853
845
policy .set_event_loop (new_loop )
854
846
855
847
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
+
856
859
@pytest .hookimpl (tryfirst = True , hookwrapper = True )
857
860
def pytest_pyfunc_call (pyfuncitem : Function ) -> Optional [object ]:
858
861
"""
@@ -893,9 +896,7 @@ def wrap_in_sync(
893
896
@functools .wraps (func )
894
897
def inner (* args , ** kwargs ):
895
898
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 ()
899
900
task = asyncio .ensure_future (coro , loop = _loop )
900
901
try :
901
902
_loop .run_until_complete (task )
0 commit comments