@@ -886,7 +886,9 @@ def finish(self, request):
886
886
self ._finalizers = []
887
887
888
888
def execute (self , request ):
889
- for argname in self ._dependee_fixture_argnames (request ):
889
+ # get required arguments and register our own finish()
890
+ # with their finalization
891
+ for argname in self .argnames :
890
892
fixturedef = request ._get_active_fixturedef (argname )
891
893
if argname != "request" :
892
894
fixturedef .addfinalizer (functools .partial (self .finish , request = request ))
@@ -909,61 +911,6 @@ def execute(self, request):
909
911
hook = self ._fixturemanager .session .gethookproxy (request .node .fspath )
910
912
return hook .pytest_fixture_setup (fixturedef = self , request = request )
911
913
912
- def _dependee_fixture_argnames (self , request ):
913
- """A list of argnames for fixtures that this fixture depends on.
914
-
915
- Given a request, this looks at the currently known list of fixture argnames, and
916
- attempts to determine what slice of the list contains fixtures that it can know
917
- should execute before it. This information is necessary so that this fixture can
918
- know what fixtures to register its finalizer with to make sure that if they
919
- would be torn down, they would tear down this fixture before themselves. It's
920
- crucial for fixtures to be torn down in the inverse order that they were set up
921
- in so that they don't try to clean up something that another fixture is still
922
- depending on.
923
-
924
- When autouse fixtures are involved, it can be tricky to figure out when fixtures
925
- should be torn down. To solve this, this method leverages the ``fixturenames``
926
- list provided by the ``request`` object, as this list is at least somewhat
927
- sorted (in terms of the order fixtures are set up in) by the time this method is
928
- reached. It's sorted enough that the starting point of fixtures that depend on
929
- this one can be found using the ``self._parent_request`` stack.
930
-
931
- If a request in the ``self._parent_request`` stack has a ``:class:FixtureDef``
932
- associated with it, then that fixture is dependent on this one, so any fixture
933
- names that appear in the list of fixture argnames that come after it can also be
934
- ruled out. The argnames of all fixtures associated with a request in the
935
- ``self._parent_request`` stack are found, and the lowest index argname is
936
- considered the earliest point in the list of fixture argnames where everything
937
- from that point onward can be considered to execute after this fixture.
938
- Everything before this point can be considered fixtures that this fixture
939
- depends on, and so this fixture should register its finalizer with all of them
940
- to ensure that if any of them are to be torn down, they will tear this fixture
941
- down first.
942
-
943
- This is the first part of the list of fixture argnames that is returned. The last
944
- part of the list is everything in ``self.argnames`` as those are explicit
945
- dependees of this fixture, so this fixture should definitely register its
946
- finalizer with them.
947
- """
948
- all_fix_names = request .fixturenames
949
- try :
950
- current_fix_index = all_fix_names .index (self .argname )
951
- except ValueError :
952
- current_fix_index = len (request .fixturenames )
953
- parent_fixture_indexes = set ()
954
-
955
- parent_request = request ._parent_request
956
- while hasattr (parent_request , "_parent_request" ):
957
- if hasattr (parent_request , "_fixturedef" ):
958
- parent_fix_name = parent_request ._fixturedef .argname
959
- if parent_fix_name in all_fix_names :
960
- parent_fixture_indexes .add (all_fix_names .index (parent_fix_name ))
961
- parent_request = parent_request ._parent_request
962
-
963
- stack_slice_index = min ([current_fix_index , * parent_fixture_indexes ])
964
- active_fixture_argnames = all_fix_names [:stack_slice_index ]
965
- return {* active_fixture_argnames , * self .argnames }
966
-
967
914
def cache_key (self , request ):
968
915
return request .param_index if not hasattr (request , "param" ) else request .param
969
916
0 commit comments