Skip to content

Commit 1f74151

Browse files
committed
remove modules after sys.path has been changed
to make sure we do not keep them during the update, see cruft#253
1 parent c9f678a commit 1f74151

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

cruft/_commands/utils/iohelper.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ def __enter__(self):
2020

2121
def cleanup(self, cnt=0):
2222
if self._extended_path:
23-
name = str(Path(self.tmpdir.name) / self._directory)
24-
if name in sys.path:
25-
sys.path.remove(name)
23+
dir_path = Path(self.tmpdir.name) / self._directory
24+
dir_name = str(dir_path)
25+
if dir_name in sys.path:
26+
sys.path.remove(dir_name)
27+
for name, mod in list(sys.modules.items()):
28+
if getattr(mod, "__file__", None):
29+
mod_path = Path(mod.__file__)
30+
if dir_path < mod_path:
31+
del sys.modules[name]
2632
if cnt >= 5: # pragma: no cover
2733
raise RuntimeError("Could not delete TemporaryDirectory!")
2834
try:

0 commit comments

Comments
 (0)