Skip to content

Commit

Permalink
The system env can have packages in more than purelib and platlib
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
  • Loading branch information
abompard committed Aug 24, 2023
1 parent 610abf7 commit 46b1fc8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ def platlib(self) -> Path:

return self._platlib

def _get_lib_dirs(self) -> list[Path]:
return [self.purelib, self.platlib]

def is_path_relative_to_lib(self, path: Path) -> bool:
for lib_path in [self.purelib, self.platlib]:
for lib_path in self._get_lib_dirs():
with contextlib.suppress(ValueError):
path.relative_to(lib_path)
return True
Expand Down
4 changes: 4 additions & 0 deletions src/poetry/utils/env/system_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import platform
import site
import sys
import sysconfig

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

def is_venv(self) -> bool:
return self._path != self._base

def _get_lib_dirs(self) -> list[Path]:
return super()._get_lib_dirs() + [Path(d) for d in site.getsitepackages()]
10 changes: 8 additions & 2 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import site
import subprocess
import sys

Expand Down Expand Up @@ -1475,8 +1476,13 @@ def test_env_system_packages_are_relative_to_lib(
venv_path = tmp_path / "venv"
EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True})
env = VirtualEnv(venv_path)
pytest_dist = metadata.distribution("pytest")
assert env.is_path_relative_to_lib(pytest_dist._path) # type: ignore[attr-defined]
site_dir = Path(site.getsitepackages()[-1])
dist = next(
p
for p in metadata.distributions()
if p._path.relative_to(site_dir) is not None # type: ignore[attr-defined]
)
assert env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined]


@pytest.mark.parametrize(
Expand Down

0 comments on commit 46b1fc8

Please sign in to comment.