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