config: stop using exception triplets in `ConftestImportError`
In recent python versions all of the info is on the exception object itself so no reason to deal with the annoying tuple.
This commit is contained in:
		
							parent
							
								
									6e74601466
								
							
						
					
					
						commit
						e1074f9c3d
					
				| 
						 | 
					@ -17,7 +17,6 @@ from functools import lru_cache
 | 
				
			||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
from textwrap import dedent
 | 
					from textwrap import dedent
 | 
				
			||||||
from types import FunctionType
 | 
					from types import FunctionType
 | 
				
			||||||
from types import TracebackType
 | 
					 | 
				
			||||||
from typing import Any
 | 
					from typing import Any
 | 
				
			||||||
from typing import Callable
 | 
					from typing import Callable
 | 
				
			||||||
from typing import cast
 | 
					from typing import cast
 | 
				
			||||||
| 
						 | 
					@ -112,16 +111,14 @@ class ConftestImportFailure(Exception):
 | 
				
			||||||
    def __init__(
 | 
					    def __init__(
 | 
				
			||||||
        self,
 | 
					        self,
 | 
				
			||||||
        path: Path,
 | 
					        path: Path,
 | 
				
			||||||
        excinfo: Tuple[Type[Exception], Exception, TracebackType],
 | 
					        *,
 | 
				
			||||||
 | 
					        cause: Exception,
 | 
				
			||||||
    ) -> None:
 | 
					    ) -> None:
 | 
				
			||||||
        super().__init__(path, excinfo)
 | 
					 | 
				
			||||||
        self.path = path
 | 
					        self.path = path
 | 
				
			||||||
        self.excinfo = excinfo
 | 
					        self.cause = cause
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self) -> str:
 | 
					    def __str__(self) -> str:
 | 
				
			||||||
        return "{}: {} (from {})".format(
 | 
					        return f"{type(self.cause).__name__}: {self.cause} (from {self.path})"
 | 
				
			||||||
            self.excinfo[0].__name__, self.excinfo[1], self.path
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def filter_traceback_for_conftest_import_failure(
 | 
					def filter_traceback_for_conftest_import_failure(
 | 
				
			||||||
| 
						 | 
					@ -152,7 +149,7 @@ def main(
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            config = _prepareconfig(args, plugins)
 | 
					            config = _prepareconfig(args, plugins)
 | 
				
			||||||
        except ConftestImportFailure as e:
 | 
					        except ConftestImportFailure as e:
 | 
				
			||||||
            exc_info = ExceptionInfo.from_exc_info(e.excinfo)
 | 
					            exc_info = ExceptionInfo.from_exception(e.cause)
 | 
				
			||||||
            tw = TerminalWriter(sys.stderr)
 | 
					            tw = TerminalWriter(sys.stderr)
 | 
				
			||||||
            tw.line(f"ImportError while loading conftest '{e.path}'.", red=True)
 | 
					            tw.line(f"ImportError while loading conftest '{e.path}'.", red=True)
 | 
				
			||||||
            exc_info.traceback = exc_info.traceback.filter(
 | 
					            exc_info.traceback = exc_info.traceback.filter(
 | 
				
			||||||
| 
						 | 
					@ -654,8 +651,7 @@ class PytestPluginManager(PluginManager):
 | 
				
			||||||
            mod = import_path(conftestpath, mode=importmode, root=rootpath)
 | 
					            mod = import_path(conftestpath, mode=importmode, root=rootpath)
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            assert e.__traceback__ is not None
 | 
					            assert e.__traceback__ is not None
 | 
				
			||||||
            exc_info = (type(e), e, e.__traceback__)
 | 
					            raise ConftestImportFailure(conftestpath, cause=e) from e
 | 
				
			||||||
            raise ConftestImportFailure(conftestpath, exc_info) from e
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self._check_non_top_pytest_plugins(mod, conftestpath)
 | 
					        self._check_non_top_pytest_plugins(mod, conftestpath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -377,7 +377,8 @@ def _postmortem_traceback(excinfo: ExceptionInfo[BaseException]) -> types.Traceb
 | 
				
			||||||
    elif isinstance(excinfo.value, ConftestImportFailure):
 | 
					    elif isinstance(excinfo.value, ConftestImportFailure):
 | 
				
			||||||
        # A config.ConftestImportFailure is not useful for post_mortem.
 | 
					        # A config.ConftestImportFailure is not useful for post_mortem.
 | 
				
			||||||
        # Use the underlying exception instead:
 | 
					        # Use the underlying exception instead:
 | 
				
			||||||
        return excinfo.value.excinfo[2]
 | 
					        assert excinfo.value.cause.__traceback__ is not None
 | 
				
			||||||
 | 
					        return excinfo.value.cause.__traceback__
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        assert excinfo._excinfo is not None
 | 
					        assert excinfo._excinfo is not None
 | 
				
			||||||
        return excinfo._excinfo[2]
 | 
					        return excinfo._excinfo[2]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -384,7 +384,7 @@ class Node(abc.ABC, metaclass=NodeMeta):
 | 
				
			||||||
        from _pytest.fixtures import FixtureLookupError
 | 
					        from _pytest.fixtures import FixtureLookupError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if isinstance(excinfo.value, ConftestImportFailure):
 | 
					        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 isinstance(excinfo.value, fail.Exception):
 | 
				
			||||||
            if not excinfo.value.pytrace:
 | 
					            if not excinfo.value.pytrace:
 | 
				
			||||||
                style = "value"
 | 
					                style = "value"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2108,9 +2108,7 @@ def test_conftest_import_error_repr(tmp_path: Path) -> None:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            raise RuntimeError("some error")
 | 
					            raise RuntimeError("some error")
 | 
				
			||||||
        except Exception as exc:
 | 
					        except Exception as exc:
 | 
				
			||||||
            assert exc.__traceback__ is not None
 | 
					            raise ConftestImportFailure(path, cause=exc) from exc
 | 
				
			||||||
            exc_info = (type(exc), exc, exc.__traceback__)
 | 
					 | 
				
			||||||
            raise ConftestImportFailure(path, exc_info) from exc
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_strtobool() -> None:
 | 
					def test_strtobool() -> None:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue