@@ -872,8 +872,7 @@ def pytest_fixture_setup(
872
872
policy = asyncio .get_event_loop_policy ()
873
873
try :
874
874
old_loop = _get_event_loop_no_warn (policy )
875
- is_pytest_asyncio_loop = getattr (old_loop , "__pytest_asyncio" , False )
876
- if old_loop is not loop and not is_pytest_asyncio_loop :
875
+ if old_loop is not loop and not _is_pytest_asyncio_loop (old_loop ):
877
876
old_loop .close ()
878
877
except RuntimeError :
879
878
# Either the current event loop has been set to None
@@ -886,6 +885,10 @@ def pytest_fixture_setup(
886
885
yield
887
886
888
887
888
+ def _is_pytest_asyncio_loop (loop : AbstractEventLoop ) -> bool :
889
+ return getattr (loop , "__pytest_asyncio" , False )
890
+
891
+
889
892
def _add_finalizers (fixturedef : FixtureDef , * finalizers : Callable [[], object ]) -> None :
890
893
"""
891
894
Registers the specified fixture finalizers in the fixture.
@@ -919,7 +922,7 @@ def _close_event_loop() -> None:
919
922
loop = policy .get_event_loop ()
920
923
except RuntimeError :
921
924
loop = None
922
- if loop is not None and not getattr (loop , "__pytest_asyncio" , False ):
925
+ if loop is not None and not _is_pytest_asyncio_loop (loop ):
923
926
if not loop .is_closed ():
924
927
warnings .warn (
925
928
_UNCLOSED_EVENT_LOOP_WARNING % loop ,
@@ -936,7 +939,7 @@ def _restore_policy():
936
939
loop = _get_event_loop_no_warn (previous_policy )
937
940
except RuntimeError :
938
941
loop = None
939
- if loop and not getattr (loop , "__pytest_asyncio" , False ):
942
+ if loop and not _is_pytest_asyncio_loop (loop ):
940
943
loop .close ()
941
944
asyncio .set_event_loop_policy (previous_policy )
942
945
@@ -955,7 +958,7 @@ def _provide_clean_event_loop() -> None:
955
958
old_loop = _get_event_loop_no_warn (policy )
956
959
except RuntimeError :
957
960
old_loop = None
958
- if old_loop is not None and not getattr (old_loop , "__pytest_asyncio" , False ):
961
+ if old_loop is not None and not _is_pytest_asyncio_loop (old_loop ):
959
962
new_loop = policy .new_event_loop ()
960
963
policy .set_event_loop (new_loop )
961
964
0 commit comments