Skip to content

Commit 533e610

Browse files
authored
Merge pull request #5098 from blueyed/fix-syspath_prepend
monkeypatch.syspath_prepend: invalidate import cache
2 parents c3b7efc + 8fd5a65 commit 533e610

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

changelog/5098.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalidate import caches with ``monkeypatch.syspath_prepend``, which is required with namespace packages being used.

src/_pytest/monkeypatch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ def syspath_prepend(self, path):
271271
# https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
272272
fixup_namespace_packages(str(path))
273273

274+
# A call to syspathinsert() usually means that the caller wants to
275+
# import some dynamically created files, thus with python3 we
276+
# invalidate its import caches.
277+
# This is especially important when any namespace package is in used,
278+
# since then the mtime based FileFinder cache (that gets created in
279+
# this case already) gets not invalidated when writing the new files
280+
# quickly afterwards.
281+
if sys.version_info >= (3, 3):
282+
from importlib import invalidate_caches
283+
284+
invalidate_caches()
285+
274286
def chdir(self, path):
275287
""" Change the current working directory to the specified path.
276288
Path can be a string or a py.path.local object.

src/_pytest/pytester.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -616,27 +616,10 @@ def syspathinsert(self, path=None):
616616
This is undone automatically when this object dies at the end of each
617617
test.
618618
"""
619-
from pkg_resources import fixup_namespace_packages
620-
621619
if path is None:
622620
path = self.tmpdir
623621

624-
dirname = str(path)
625-
sys.path.insert(0, dirname)
626-
fixup_namespace_packages(dirname)
627-
628-
# a call to syspathinsert() usually means that the caller wants to
629-
# import some dynamically created files, thus with python3 we
630-
# invalidate its import caches
631-
self._possibly_invalidate_import_caches()
632-
633-
def _possibly_invalidate_import_caches(self):
634-
# invalidate caches if we can (py33 and above)
635-
try:
636-
from importlib import invalidate_caches
637-
except ImportError:
638-
return
639-
invalidate_caches()
622+
self.monkeypatch.syspath_prepend(str(path))
640623

641624
def mkdir(self, name):
642625
"""Create a new (sub)directory."""

testing/test_monkeypatch.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,10 @@ def test_syspath_prepend_with_namespace_packages(testdir, monkeypatch):
462462
import ns_pkg.world
463463

464464
assert ns_pkg.world.check() == "world"
465+
466+
# Should invalidate caches via importlib.invalidate_caches.
467+
tmpdir = testdir.tmpdir
468+
modules_tmpdir = tmpdir.mkdir("modules_tmpdir")
469+
monkeypatch.syspath_prepend(str(modules_tmpdir))
470+
modules_tmpdir.join("main_app.py").write("app = True")
471+
from main_app import app # noqa: F401

0 commit comments

Comments
 (0)