Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,9 @@ def import_plugin(self, modname, consider_entry_points=False):
try:
__import__(importspec)
except ImportError as e:
new_exc_message = 'Error importing plugin "{}": {}'.format(
modname, str(e.args[0])
)
new_exc = ImportError(new_exc_message)
tb = sys.exc_info()[2]

raise new_exc.with_traceback(tb)
raise ImportError(
'Error importing plugin "{}": {}'.format(modname, str(e.args[0]))
).with_traceback(e.__traceback__)

except Skipped as e:
from _pytest.warnings import _issue_warning_captured
Expand Down
16 changes: 8 additions & 8 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,19 +862,19 @@ def addfinalizer(self, finalizer):
self._finalizers.append(finalizer)

def finish(self, request):
exceptions = []
exc = None
try:
while self._finalizers:
try:
func = self._finalizers.pop()
func()
except: # noqa
exceptions.append(sys.exc_info())
if exceptions:
_, val, tb = exceptions[0]
# Ensure to not keep frame references through traceback.
del exceptions
raise val.with_traceback(tb)
except BaseException 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
finally:
hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
hook.pytest_fixture_post_finalizer(fixturedef=self, request=request)
Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ def collect(self):
self.trace.root.indent += 1
try:
yield from self._collect(fspath, parts)
except NoMatch:
except NoMatch as exc:
report_arg = "::".join((str(fspath), *parts))
# we are inside a make_report hook so
# we cannot directly pass through the exception
self._notfound.append((report_arg, sys.exc_info()[1]))
self._notfound.append((report_arg, exc))

self.trace.root.indent -= 1
self._collection_node_cache1.clear()
Expand Down
3 changes: 1 addition & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ def _importtestmodule(self):
mod = self.fspath.pyimport(ensuresyspath=importmode)
except SyntaxError:
raise self.CollectError(ExceptionInfo.from_current().getrepr(style="short"))
except self.fspath.ImportMismatchError:
e = sys.exc_info()[1]
except self.fspath.ImportMismatchError as e:
raise self.CollectError(
"import file mismatch:\n"
"imported module %r has this __file__ attribute:\n"
Expand Down
42 changes: 18 additions & 24 deletions src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,14 @@ def pytest_runtest_call(item):
pass
try:
item.runtest()
except Exception:
except Exception as e:
# Store trace info to allow postmortem debugging
type, value, tb = sys.exc_info()
assert tb is not None
tb = tb.tb_next # Skip *this* frame
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
del type, value, tb # Get rid of these in this frame
raise
sys.last_type = type(e)
sys.last_value = e
assert e.__traceback__ is not None
# Skip *this* frame
sys.last_traceback = e.__traceback__.tb_next
raise e


def pytest_runtest_teardown(item, nextitem):
Expand Down Expand Up @@ -318,15 +316,13 @@ def _callfinalizers(self, colitem):
fin = finalizers.pop()
try:
fin()
except TEST_OUTCOME:
except TEST_OUTCOME as e:
# XXX Only first exception will be seen by user,
# ideally all should be reported.
if exc is None:
exc = sys.exc_info()
exc = e
if exc:
_, val, tb = exc
assert val is not None
raise val.with_traceback(tb)
raise exc

def _teardown_with_finalization(self, colitem):
self._callfinalizers(colitem)
Expand All @@ -352,15 +348,13 @@ def _teardown_towards(self, needed_collectors):
break
try:
self._pop_and_teardown()
except TEST_OUTCOME:
except TEST_OUTCOME as e:
# XXX Only first exception will be seen by user,
# ideally all should be reported.
if exc is None:
exc = sys.exc_info()
exc = e
if exc:
_, val, tb = exc
assert val is not None
raise val.with_traceback(tb)
raise exc

def prepare(self, colitem):
""" setup objects along the collector chain to the test-method
Expand All @@ -371,15 +365,15 @@ def prepare(self, colitem):
# check if the last collection node has raised an error
for col in self.stack:
if hasattr(col, "_prepare_exc"):
_, val, tb = col._prepare_exc
raise val.with_traceback(tb)
exc = col._prepare_exc
raise exc
for col in needed_collectors[len(self.stack) :]:
self.stack.append(col)
try:
col.setup()
except TEST_OUTCOME:
col._prepare_exc = sys.exc_info()
raise
except TEST_OUTCOME as e:
col._prepare_exc = e
raise e


def collect_one_node(collector):
Expand Down