Skip to content

Commit

Permalink
Merge pull request #11817 from bluetech/conftesterror-cleanup
Browse files Browse the repository at this point in the history
config: stop using exception triplets in `ConftestImportError`
  • Loading branch information
bluetech authored Jan 15, 2024
2 parents 6e74601 + e1074f9 commit 9af6d46
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
16 changes: 6 additions & 10 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from pathlib import Path
from textwrap import dedent
from types import FunctionType
from types import TracebackType
from typing import Any
from typing import Callable
from typing import cast
Expand Down Expand Up @@ -112,16 +111,14 @@ class ConftestImportFailure(Exception):
def __init__(
self,
path: Path,
excinfo: Tuple[Type[Exception], Exception, TracebackType],
*,
cause: Exception,
) -> None:
super().__init__(path, excinfo)
self.path = path
self.excinfo = excinfo
self.cause = cause

def __str__(self) -> str:
return "{}: {} (from {})".format(
self.excinfo[0].__name__, self.excinfo[1], self.path
)
return f"{type(self.cause).__name__}: {self.cause} (from {self.path})"


def filter_traceback_for_conftest_import_failure(
Expand Down Expand Up @@ -152,7 +149,7 @@ def main(
try:
config = _prepareconfig(args, plugins)
except ConftestImportFailure as e:
exc_info = ExceptionInfo.from_exc_info(e.excinfo)
exc_info = ExceptionInfo.from_exception(e.cause)
tw = TerminalWriter(sys.stderr)
tw.line(f"ImportError while loading conftest '{e.path}'.", red=True)
exc_info.traceback = exc_info.traceback.filter(
Expand Down Expand Up @@ -654,8 +651,7 @@ def _importconftest(
mod = import_path(conftestpath, mode=importmode, root=rootpath)
except Exception as e:
assert e.__traceback__ is not None
exc_info = (type(e), e, e.__traceback__)
raise ConftestImportFailure(conftestpath, exc_info) from e
raise ConftestImportFailure(conftestpath, cause=e) from e

self._check_non_top_pytest_plugins(mod, conftestpath)

Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ def _postmortem_traceback(excinfo: ExceptionInfo[BaseException]) -> types.Traceb
elif isinstance(excinfo.value, ConftestImportFailure):
# A config.ConftestImportFailure is not useful for post_mortem.
# Use the underlying exception instead:
return excinfo.value.excinfo[2]
assert excinfo.value.cause.__traceback__ is not None
return excinfo.value.cause.__traceback__
else:
assert excinfo._excinfo is not None
return excinfo._excinfo[2]
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def _repr_failure_py(
from _pytest.fixtures import FixtureLookupError

if isinstance(excinfo.value, ConftestImportFailure):
excinfo = ExceptionInfo.from_exc_info(excinfo.value.excinfo)
excinfo = ExceptionInfo.from_exception(excinfo.value.cause)
if isinstance(excinfo.value, fail.Exception):
if not excinfo.value.pytrace:
style = "value"
Expand Down
4 changes: 1 addition & 3 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,9 +2108,7 @@ def test_conftest_import_error_repr(tmp_path: Path) -> None:
try:
raise RuntimeError("some error")
except Exception as exc:
assert exc.__traceback__ is not None
exc_info = (type(exc), exc, exc.__traceback__)
raise ConftestImportFailure(path, exc_info) from exc
raise ConftestImportFailure(path, cause=exc) from exc


def test_strtobool() -> None:
Expand Down

0 comments on commit 9af6d46

Please sign in to comment.