runner: use insertion-ordered dict instead of stack, dict pair
Since dicts are now ordered, we can use the finalizers dict itself as the dict, simplifying the code.
This commit is contained in:
parent
03c3a90c68
commit
1db78bec31
|
@ -406,8 +406,7 @@ class SetupState:
|
||||||
"""Shared state for setting up/tearing down test items or collectors."""
|
"""Shared state for setting up/tearing down test items or collectors."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.stack: List[Node] = []
|
self.stack: Dict[Node, List[Callable[[], object]]] = {}
|
||||||
self._finalizers: Dict[Node, List[Callable[[], object]]] = {}
|
|
||||||
|
|
||||||
_prepare_exc_key = StoreKey[Union[OutcomeException, Exception]]()
|
_prepare_exc_key = StoreKey[Union[OutcomeException, Exception]]()
|
||||||
|
|
||||||
|
@ -423,9 +422,7 @@ class SetupState:
|
||||||
needed_collectors = colitem.listchain()
|
needed_collectors = colitem.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
|
||||||
assert col not in self._finalizers
|
self.stack[col] = []
|
||||||
self.stack.append(col)
|
|
||||||
self._finalizers[col] = []
|
|
||||||
try:
|
try:
|
||||||
col.setup()
|
col.setup()
|
||||||
except TEST_OUTCOME as e:
|
except TEST_OUTCOME as e:
|
||||||
|
@ -437,16 +434,15 @@ class SetupState:
|
||||||
assert colitem and not isinstance(colitem, tuple)
|
assert colitem and not isinstance(colitem, tuple)
|
||||||
assert callable(finalizer)
|
assert callable(finalizer)
|
||||||
assert colitem in self.stack, (colitem, self.stack)
|
assert colitem in self.stack, (colitem, self.stack)
|
||||||
self._finalizers[colitem].append(finalizer)
|
self.stack[colitem].append(finalizer)
|
||||||
|
|
||||||
def teardown_exact(self, nextitem: Optional[Item]) -> None:
|
def teardown_exact(self, nextitem: Optional[Item]) -> None:
|
||||||
needed_collectors = nextitem and nextitem.listchain() or []
|
needed_collectors = nextitem and nextitem.listchain() or []
|
||||||
exc = None
|
exc = None
|
||||||
while self.stack:
|
while self.stack:
|
||||||
if self.stack == needed_collectors[: len(self.stack)]:
|
if list(self.stack.keys()) == needed_collectors[: len(self.stack)]:
|
||||||
break
|
break
|
||||||
colitem = self.stack.pop()
|
colitem, finalizers = self.stack.popitem()
|
||||||
finalizers = self._finalizers.pop(colitem)
|
|
||||||
finalizers.insert(0, colitem.teardown)
|
finalizers.insert(0, colitem.teardown)
|
||||||
while finalizers:
|
while finalizers:
|
||||||
fin = finalizers.pop()
|
fin = finalizers.pop()
|
||||||
|
@ -457,12 +453,10 @@ class SetupState:
|
||||||
# ideally all should be reported.
|
# ideally all should be reported.
|
||||||
if exc is None:
|
if exc is None:
|
||||||
exc = e
|
exc = e
|
||||||
for colitem in self._finalizers:
|
|
||||||
assert colitem in self.stack
|
|
||||||
if exc:
|
if exc:
|
||||||
raise exc
|
raise exc
|
||||||
if nextitem is None:
|
if nextitem is None:
|
||||||
assert not self._finalizers
|
assert not self.stack
|
||||||
|
|
||||||
|
|
||||||
def collect_one_node(collector: Collector) -> CollectReport:
|
def collect_one_node(collector: Collector) -> CollectReport:
|
||||||
|
|
Loading…
Reference in New Issue