Merge pull request #6868 from bluetech/simplify-exc
Simplify some exception handling code
This commit is contained in:
commit
4f8fff9cab
|
@ -612,13 +612,9 @@ class PytestPluginManager(PluginManager):
|
||||||
try:
|
try:
|
||||||
__import__(importspec)
|
__import__(importspec)
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
new_exc_message = 'Error importing plugin "{}": {}'.format(
|
raise ImportError(
|
||||||
modname, str(e.args[0])
|
'Error importing plugin "{}": {}'.format(modname, str(e.args[0]))
|
||||||
)
|
).with_traceback(e.__traceback__)
|
||||||
new_exc = ImportError(new_exc_message)
|
|
||||||
tb = sys.exc_info()[2]
|
|
||||||
|
|
||||||
raise new_exc.with_traceback(tb)
|
|
||||||
|
|
||||||
except Skipped as e:
|
except Skipped as e:
|
||||||
from _pytest.warnings import _issue_warning_captured
|
from _pytest.warnings import _issue_warning_captured
|
||||||
|
|
|
@ -862,19 +862,19 @@ class FixtureDef:
|
||||||
self._finalizers.append(finalizer)
|
self._finalizers.append(finalizer)
|
||||||
|
|
||||||
def finish(self, request):
|
def finish(self, request):
|
||||||
exceptions = []
|
exc = None
|
||||||
try:
|
try:
|
||||||
while self._finalizers:
|
while self._finalizers:
|
||||||
try:
|
try:
|
||||||
func = self._finalizers.pop()
|
func = self._finalizers.pop()
|
||||||
func()
|
func()
|
||||||
except: # noqa
|
except BaseException as e:
|
||||||
exceptions.append(sys.exc_info())
|
# XXX Only first exception will be seen by user,
|
||||||
if exceptions:
|
# ideally all should be reported.
|
||||||
_, val, tb = exceptions[0]
|
if exc is None:
|
||||||
# Ensure to not keep frame references through traceback.
|
exc = e
|
||||||
del exceptions
|
if exc:
|
||||||
raise val.with_traceback(tb)
|
raise exc
|
||||||
finally:
|
finally:
|
||||||
hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
|
hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
|
||||||
hook.pytest_fixture_post_finalizer(fixturedef=self, request=request)
|
hook.pytest_fixture_post_finalizer(fixturedef=self, request=request)
|
||||||
|
|
|
@ -496,11 +496,11 @@ class Session(nodes.FSCollector):
|
||||||
self.trace.root.indent += 1
|
self.trace.root.indent += 1
|
||||||
try:
|
try:
|
||||||
yield from self._collect(fspath, parts)
|
yield from self._collect(fspath, parts)
|
||||||
except NoMatch:
|
except NoMatch as exc:
|
||||||
report_arg = "::".join((str(fspath), *parts))
|
report_arg = "::".join((str(fspath), *parts))
|
||||||
# we are inside a make_report hook so
|
# we are inside a make_report hook so
|
||||||
# we cannot directly pass through the exception
|
# 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.trace.root.indent -= 1
|
||||||
self._collection_node_cache1.clear()
|
self._collection_node_cache1.clear()
|
||||||
|
|
|
@ -513,8 +513,7 @@ class Module(nodes.File, PyCollector):
|
||||||
mod = self.fspath.pyimport(ensuresyspath=importmode)
|
mod = self.fspath.pyimport(ensuresyspath=importmode)
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
raise self.CollectError(ExceptionInfo.from_current().getrepr(style="short"))
|
raise self.CollectError(ExceptionInfo.from_current().getrepr(style="short"))
|
||||||
except self.fspath.ImportMismatchError:
|
except self.fspath.ImportMismatchError as e:
|
||||||
e = sys.exc_info()[1]
|
|
||||||
raise self.CollectError(
|
raise self.CollectError(
|
||||||
"import file mismatch:\n"
|
"import file mismatch:\n"
|
||||||
"imported module %r has this __file__ attribute:\n"
|
"imported module %r has this __file__ attribute:\n"
|
||||||
|
|
|
@ -133,16 +133,14 @@ def pytest_runtest_call(item):
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
item.runtest()
|
item.runtest()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
# Store trace info to allow postmortem debugging
|
# Store trace info to allow postmortem debugging
|
||||||
type, value, tb = sys.exc_info()
|
sys.last_type = type(e)
|
||||||
assert tb is not None
|
sys.last_value = e
|
||||||
tb = tb.tb_next # Skip *this* frame
|
assert e.__traceback__ is not None
|
||||||
sys.last_type = type
|
# Skip *this* frame
|
||||||
sys.last_value = value
|
sys.last_traceback = e.__traceback__.tb_next
|
||||||
sys.last_traceback = tb
|
raise e
|
||||||
del type, value, tb # Get rid of these in this frame
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def pytest_runtest_teardown(item, nextitem):
|
def pytest_runtest_teardown(item, nextitem):
|
||||||
|
@ -318,15 +316,13 @@ class SetupState:
|
||||||
fin = finalizers.pop()
|
fin = finalizers.pop()
|
||||||
try:
|
try:
|
||||||
fin()
|
fin()
|
||||||
except TEST_OUTCOME:
|
except TEST_OUTCOME as e:
|
||||||
# XXX Only first exception will be seen by user,
|
# XXX Only first exception will be seen by user,
|
||||||
# ideally all should be reported.
|
# ideally all should be reported.
|
||||||
if exc is None:
|
if exc is None:
|
||||||
exc = sys.exc_info()
|
exc = e
|
||||||
if exc:
|
if exc:
|
||||||
_, val, tb = exc
|
raise exc
|
||||||
assert val is not None
|
|
||||||
raise val.with_traceback(tb)
|
|
||||||
|
|
||||||
def _teardown_with_finalization(self, colitem):
|
def _teardown_with_finalization(self, colitem):
|
||||||
self._callfinalizers(colitem)
|
self._callfinalizers(colitem)
|
||||||
|
@ -352,15 +348,13 @@ class SetupState:
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
self._pop_and_teardown()
|
self._pop_and_teardown()
|
||||||
except TEST_OUTCOME:
|
except TEST_OUTCOME as e:
|
||||||
# XXX Only first exception will be seen by user,
|
# XXX Only first exception will be seen by user,
|
||||||
# ideally all should be reported.
|
# ideally all should be reported.
|
||||||
if exc is None:
|
if exc is None:
|
||||||
exc = sys.exc_info()
|
exc = e
|
||||||
if exc:
|
if exc:
|
||||||
_, val, tb = exc
|
raise exc
|
||||||
assert val is not None
|
|
||||||
raise val.with_traceback(tb)
|
|
||||||
|
|
||||||
def prepare(self, colitem):
|
def prepare(self, colitem):
|
||||||
""" setup objects along the collector chain to the test-method
|
""" setup objects along the collector chain to the test-method
|
||||||
|
@ -371,15 +365,15 @@ class SetupState:
|
||||||
# check if the last collection node has raised an error
|
# check if the last collection node has raised an error
|
||||||
for col in self.stack:
|
for col in self.stack:
|
||||||
if hasattr(col, "_prepare_exc"):
|
if hasattr(col, "_prepare_exc"):
|
||||||
_, val, tb = col._prepare_exc
|
exc = col._prepare_exc
|
||||||
raise val.with_traceback(tb)
|
raise exc
|
||||||
for col in needed_collectors[len(self.stack) :]:
|
for col in needed_collectors[len(self.stack) :]:
|
||||||
self.stack.append(col)
|
self.stack.append(col)
|
||||||
try:
|
try:
|
||||||
col.setup()
|
col.setup()
|
||||||
except TEST_OUTCOME:
|
except TEST_OUTCOME as e:
|
||||||
col._prepare_exc = sys.exc_info()
|
col._prepare_exc = e
|
||||||
raise
|
raise e
|
||||||
|
|
||||||
|
|
||||||
def collect_one_node(collector):
|
def collect_one_node(collector):
|
||||||
|
|
Loading…
Reference in New Issue