Skip to content

Commit

Permalink
Use exceptiongroup for teardown errors
Browse files Browse the repository at this point in the history
We might get exceptions from multiple steps of teardown, and now we have a way of reporting more than one!
  • Loading branch information
Zac-HD committed Aug 19, 2022
1 parent 69f2855 commit ac11bf3
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import Union

import attr
from exceptiongroup import BaseExceptionGroup

from .reports import BaseReport
from .reports import CollectErrorRepr
Expand Down Expand Up @@ -512,22 +513,21 @@ def teardown_exact(self, nextitem: Optional[Item]) -> None:
stack is torn down.
"""
needed_collectors = nextitem and nextitem.listchain() or []
exc = None
exceptions = []
while self.stack:
if list(self.stack.keys()) == needed_collectors[: len(self.stack)]:
break
node, (finalizers, _) = self.stack.popitem()
_, (finalizers, _) = self.stack.popitem()
while finalizers:
fin = finalizers.pop()
try:
fin()
except TEST_OUTCOME as e:
# XXX Only first exception will be seen by user,
# ideally all should be reported.
if exc is None:
exc = e
if exc:
raise exc
exceptions.append(e)
if len(exceptions) == 1:
raise exceptions[0]
elif exceptions:
raise BaseExceptionGroup("", exceptions)
if nextitem is None:
assert not self.stack

Expand Down

0 comments on commit ac11bf3

Please sign in to comment.