Skip to content

Commit 2a31c1a

Browse files
committed
Use importlib also for prepend and append import modes
1 parent bde7e9b commit 2a31c1a

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/_pytest/_code/code.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,10 @@ def getfslineno(obj: Any) -> Tuple[Union[str, py.path.local], int]:
11991199

12001200

12011201
def filter_traceback(entry: TracebackEntry) -> bool:
1202-
"""Return True if a TracebackEntry instance should be removed from tracebacks:
1202+
"""Return True if a TracebackEntry instance should be included in tracebacks.
1203+
1204+
We hide traceback entries of:
1205+
12031206
* dynamically generated code (no code to show up for it);
12041207
* internal traceback from pytest or its internal libraries, py and pluggy.
12051208
"""

src/_pytest/config/__init__.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def __str__(self):
9898
)
9999

100100

101+
def filter_traceback_for_conftest_import_failure(entry) -> bool:
102+
"""filters tracebacks entries which point to pytest internals or importlib.
103+
104+
Make a special case for importlib because we use it to import test modules and conftest files
105+
in _pytest.pathlib.import_path.
106+
"""
107+
return filter_traceback(entry) and "importlib" not in str(entry.path).split(os.sep)
108+
109+
101110
def main(args=None, plugins=None) -> Union[int, ExitCode]:
102111
""" return exit code, after performing an in-process test run.
103112
@@ -115,7 +124,9 @@ def main(args=None, plugins=None) -> Union[int, ExitCode]:
115124
tw.line(
116125
"ImportError while loading conftest '{e.path}'.".format(e=e), red=True
117126
)
118-
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
127+
exc_info.traceback = exc_info.traceback.filter(
128+
filter_traceback_for_conftest_import_failure
129+
)
119130
exc_repr = (
120131
exc_info.getrepr(style="short", chain=False)
121132
if exc_info.traceback

src/_pytest/pathlib.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import atexit
22
import fnmatch
3+
import importlib.util
34
import itertools
45
import os
56
import shutil
@@ -463,8 +464,6 @@ def import_path(
463464
raise ImportError(path)
464465

465466
if mode == ImportMode.importlib:
466-
import importlib.util
467-
468467
module_name = path.stem
469468

470469
for meta_importer in sys.meta_path:
@@ -504,7 +503,7 @@ def import_path(
504503
if str(pkg_root) != sys.path[0]:
505504
sys.path.insert(0, str(pkg_root))
506505

507-
__import__(module_name)
506+
importlib.import_module(module_name)
508507

509508
mod = sys.modules[module_name]
510509
if path.name == "__init__.py":

0 commit comments

Comments
 (0)