Teardown the current stack up until reaching nodes that nextitem also descends from. When nextitem is None (meaning we're at the last item), the entire stack is torn down.
(self, nextitem: Item | None)
| 549 | self.stack[node][0].append(finalizer) |
| 550 | |
| 551 | def teardown_exact(self, nextitem: Item | None) -> None: |
| 552 | class="st">"""Teardown the current stack up until reaching nodes that nextitem |
| 553 | also descends from. |
| 554 | |
| 555 | When nextitem is None (meaning we&class="cm">#x27;re at the last item), the entire |
| 556 | stack is torn down. |
| 557 | class="st">""" |
| 558 | needed_collectors = (nextitem and nextitem.listchain()) or [] |
| 559 | exceptions: list[BaseException] = [] |
| 560 | while self.stack: |
| 561 | if list(self.stack.keys()) == needed_collectors[: len(self.stack)]: |
| 562 | break |
| 563 | node, (finalizers, _) = self.stack.popitem() |
| 564 | these_exceptions = [] |
| 565 | while finalizers: |
| 566 | fin = finalizers.pop() |
| 567 | try: |
| 568 | fin() |
| 569 | except TEST_OUTCOME as e: |
| 570 | these_exceptions.append(e) |
| 571 | |
| 572 | if len(these_exceptions) == 1: |
| 573 | exceptions.extend(these_exceptions) |
| 574 | elif these_exceptions: |
| 575 | msg = fclass="st">"errors while tearing down {node!r}" |
| 576 | exceptions.append(BaseExceptionGroup(msg, these_exceptions[::-1])) |
| 577 | |
| 578 | if len(exceptions) == 1: |
| 579 | raise exceptions[0] |
| 580 | elif exceptions: |
| 581 | raise BaseExceptionGroup(class="st">"errors during test teardown", exceptions[::-1]) |
| 582 | if nextitem is None: |
| 583 | assert not self.stack |
| 584 | |
| 585 | |
| 586 | def collect_one_node(collector: Collector) -> CollectReport: |