Skip to content

Commit 5eaebc1

Browse files
committed
Remove one of the tracebacks from conftest import failures
This removes the KeyError from the traceback chain when an conftest fails to import: return self._conftestpath2mod[key] E KeyError: WindowsPath('D:/projects/pytest/.tmp/root/foo/conftest.py') During handling of the above exception, another exception occurred: ... raise RuntimeError("some error") E RuntimeError: some error During handling of the above exception, another exception occurred: ... E _pytest.config.ConftestImportFailure: (...) By slightly changing the code, we can remove the first chain, which is often very confusing to users and doesn't help with anything. Fix #7223
1 parent d4dfe86 commit 5eaebc1

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/_pytest/config/__init__.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" command line options, ini-file and conftest.py processing. """
22
import argparse
3+
import contextlib
34
import copy
45
import enum
56
import inspect
@@ -511,34 +512,36 @@ def _importconftest(self, conftestpath):
511512
# Using Path().resolve() is better than py.path.realpath because
512513
# it resolves to the correct path/drive in case-insensitive file systems (#5792)
513514
key = Path(str(conftestpath)).resolve()
514-
try:
515+
516+
with contextlib.suppress(KeyError):
515517
return self._conftestpath2mod[key]
516-
except KeyError:
517-
pkgpath = conftestpath.pypkgpath()
518-
if pkgpath is None:
519-
_ensure_removed_sysmodule(conftestpath.purebasename)
520-
try:
521-
mod = conftestpath.pyimport()
522-
if (
523-
hasattr(mod, "pytest_plugins")
524-
and self._configured
525-
and not self._using_pyargs
526-
):
527-
_fail_on_non_top_pytest_plugins(conftestpath, self._confcutdir)
528-
except Exception:
529-
raise ConftestImportFailure(conftestpath, sys.exc_info())
530-
531-
self._conftest_plugins.add(mod)
532-
self._conftestpath2mod[key] = mod
533-
dirpath = conftestpath.dirpath()
534-
if dirpath in self._dirpath2confmods:
535-
for path, mods in self._dirpath2confmods.items():
536-
if path and path.relto(dirpath) or path == dirpath:
537-
assert mod not in mods
538-
mods.append(mod)
539-
self.trace("loading conftestmodule {!r}".format(mod))
540-
self.consider_conftest(mod)
541-
return mod
518+
519+
pkgpath = conftestpath.pypkgpath()
520+
if pkgpath is None:
521+
_ensure_removed_sysmodule(conftestpath.purebasename)
522+
523+
try:
524+
mod = conftestpath.pyimport()
525+
if (
526+
hasattr(mod, "pytest_plugins")
527+
and self._configured
528+
and not self._using_pyargs
529+
):
530+
_fail_on_non_top_pytest_plugins(conftestpath, self._confcutdir)
531+
except Exception as e:
532+
raise ConftestImportFailure(conftestpath, sys.exc_info()) from e
533+
534+
self._conftest_plugins.add(mod)
535+
self._conftestpath2mod[key] = mod
536+
dirpath = conftestpath.dirpath()
537+
if dirpath in self._dirpath2confmods:
538+
for path, mods in self._dirpath2confmods.items():
539+
if path and path.relto(dirpath) or path == dirpath:
540+
assert mod not in mods
541+
mods.append(mod)
542+
self.trace("loading conftestmodule {!r}".format(mod))
543+
self.consider_conftest(mod)
544+
return mod
542545

543546
#
544547
# API for bootstrapping plugin loading

0 commit comments

Comments
 (0)