Merge pull request #8272 from bluetech/setupstate-refactor-2
runner: avoid using node's store in SetupState
This commit is contained in:
commit
fff9f28fdd
|
@ -36,7 +36,6 @@ from _pytest.outcomes import Exit
|
||||||
from _pytest.outcomes import OutcomeException
|
from _pytest.outcomes import OutcomeException
|
||||||
from _pytest.outcomes import Skipped
|
from _pytest.outcomes import Skipped
|
||||||
from _pytest.outcomes import TEST_OUTCOME
|
from _pytest.outcomes import TEST_OUTCOME
|
||||||
from _pytest.store import StoreKey
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
@ -467,29 +466,33 @@ class SetupState:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
# Maps node -> the node's finalizers.
|
|
||||||
# The stack is in the dict insertion order.
|
# The stack is in the dict insertion order.
|
||||||
self.stack: Dict[Node, List[Callable[[], object]]] = {}
|
self.stack: Dict[
|
||||||
|
Node,
|
||||||
_prepare_exc_key = StoreKey[Union[OutcomeException, Exception]]()
|
Tuple[
|
||||||
|
# Node's finalizers.
|
||||||
|
List[Callable[[], object]],
|
||||||
|
# Node's exception, if its setup raised.
|
||||||
|
Optional[Union[OutcomeException, Exception]],
|
||||||
|
],
|
||||||
|
] = {}
|
||||||
|
|
||||||
def prepare(self, item: Item) -> None:
|
def prepare(self, item: Item) -> None:
|
||||||
"""Setup objects along the collector chain to the item."""
|
"""Setup objects along the collector chain to the item."""
|
||||||
# If a collector fails its setup, fail its entire subtree of items.
|
# If a collector fails its setup, fail its entire subtree of items.
|
||||||
# The setup is not retried for each item - the same exception is used.
|
# The setup is not retried for each item - the same exception is used.
|
||||||
for col in self.stack:
|
for col, (finalizers, prepare_exc) in self.stack.items():
|
||||||
prepare_exc = col._store.get(self._prepare_exc_key, None)
|
|
||||||
if prepare_exc:
|
if prepare_exc:
|
||||||
raise prepare_exc
|
raise prepare_exc
|
||||||
|
|
||||||
needed_collectors = item.listchain()
|
needed_collectors = item.listchain()
|
||||||
for col in needed_collectors[len(self.stack) :]:
|
for col in needed_collectors[len(self.stack) :]:
|
||||||
assert col not in self.stack
|
assert col not in self.stack
|
||||||
self.stack[col] = [col.teardown]
|
self.stack[col] = ([col.teardown], None)
|
||||||
try:
|
try:
|
||||||
col.setup()
|
col.setup()
|
||||||
except TEST_OUTCOME as e:
|
except TEST_OUTCOME as e:
|
||||||
col._store[self._prepare_exc_key] = e
|
self.stack[col] = (self.stack[col][0], e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
def addfinalizer(self, finalizer: Callable[[], object], node: Node) -> None:
|
def addfinalizer(self, finalizer: Callable[[], object], node: Node) -> None:
|
||||||
|
@ -500,7 +503,7 @@ class SetupState:
|
||||||
assert node and not isinstance(node, tuple)
|
assert node and not isinstance(node, tuple)
|
||||||
assert callable(finalizer)
|
assert callable(finalizer)
|
||||||
assert node in self.stack, (node, self.stack)
|
assert node in self.stack, (node, self.stack)
|
||||||
self.stack[node].append(finalizer)
|
self.stack[node][0].append(finalizer)
|
||||||
|
|
||||||
def teardown_exact(self, nextitem: Optional[Item]) -> None:
|
def teardown_exact(self, nextitem: Optional[Item]) -> None:
|
||||||
"""Teardown the current stack up until reaching nodes that nextitem
|
"""Teardown the current stack up until reaching nodes that nextitem
|
||||||
|
@ -514,7 +517,7 @@ class SetupState:
|
||||||
while self.stack:
|
while self.stack:
|
||||||
if list(self.stack.keys()) == needed_collectors[: len(self.stack)]:
|
if list(self.stack.keys()) == needed_collectors[: len(self.stack)]:
|
||||||
break
|
break
|
||||||
node, finalizers = self.stack.popitem()
|
node, (finalizers, prepare_exc) = self.stack.popitem()
|
||||||
while finalizers:
|
while finalizers:
|
||||||
fin = finalizers.pop()
|
fin = finalizers.pop()
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue