Skip to content

Commit baf4106

Browse files
committed
The system env can have packages in more than purelib and platlib
Signed-off-by: Aurélien Bompard <[email protected]>
1 parent 610abf7 commit baf4106

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/poetry/utils/env/base_env.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@ def platlib(self) -> Path:
219219

220220
return self._platlib
221221

222+
def _get_lib_dirs(self) -> list[Path]:
223+
return [self.purelib, self.platlib]
224+
222225
def is_path_relative_to_lib(self, path: Path) -> bool:
223-
for lib_path in [self.purelib, self.platlib]:
226+
for lib_path in self._get_lib_dirs():
224227
with contextlib.suppress(ValueError):
225228
path.relative_to(lib_path)
226229
return True

src/poetry/utils/env/system_env.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import platform
5+
import site
56
import sys
67
import sysconfig
78

@@ -87,3 +88,6 @@ def get_pip_version(self) -> Version:
8788

8889
def is_venv(self) -> bool:
8990
return self._path != self._base
91+
92+
def _get_lib_dirs(self) -> list[Path]:
93+
return super()._get_lib_dirs() + [Path(d) for d in site.getsitepackages()]

tests/utils/test_env.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import os
5+
import site
46
import subprocess
57
import sys
68

@@ -1475,8 +1477,14 @@ def test_env_system_packages_are_relative_to_lib(
14751477
venv_path = tmp_path / "venv"
14761478
EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True})
14771479
env = VirtualEnv(venv_path)
1478-
pytest_dist = metadata.distribution("pytest")
1479-
assert env.is_path_relative_to_lib(pytest_dist._path) # type: ignore[attr-defined]
1480+
site_dir = Path(site.getsitepackages()[-1])
1481+
for p in metadata.distributions():
1482+
# Emulate is_relative_to, only available in 3.9+
1483+
with contextlib.suppress(ValueError):
1484+
p._path.relative_to(site_dir) # type: ignore[attr-defined]
1485+
dist = p
1486+
break
1487+
assert env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined]
14801488

14811489

14821490
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)