Skip to content

Commit

Permalink
fix: Use importlib to invoke packaging.tags in Env
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted authored and abn committed Apr 29, 2022
1 parent b6d4b0a commit a27c5a1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 78 deletions.
102 changes: 52 additions & 50 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ crashtest = "^0.3.0"
entrypoints = "^0.3"
html5lib = "^1.0"
importlib-metadata = { version = ">=1.6.0", python = "<3.8" }
# packaging uses calver, so version is unclamped
keyring = ">=21.2.0"
packaging = "^20.4"
# packaging uses calver, so version is unclamped
packaging = ">=20.4"
pexpect = "^4.7.0"
pkginfo = "^1.5"
requests = "^2.18"
Expand Down
47 changes: 20 additions & 27 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import subprocess
import sys
import sysconfig
import textwrap

from contextlib import contextmanager
from copy import deepcopy
Expand Down Expand Up @@ -54,6 +53,25 @@
from poetry.poetry import Poetry


GET_SYS_TAGS = f"""
import importlib.util
import json
import sys
from pathlib import Path
spec = importlib.util.spec_from_file_location("packaging", Path(r"{packaging.__file__}"))
packaging = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = packaging
spec = importlib.util.spec_from_file_location("packaging.tags", Path(r"{packaging.tags.__file__}"))
packaging_tags = importlib.util.module_from_spec(spec)
spec.loader.exec_module(packaging_tags)
print(json.dumps([(t.interpreter, t.abi, t.platform) for t in packaging_tags.sys_tags()]))
"""


GET_ENVIRONMENT_INFO = """\
import json
import os
Expand Down Expand Up @@ -1614,32 +1632,7 @@ def get_pip_command(self, embedded: bool = False) -> list[str]:
]

def get_supported_tags(self) -> list[Tag]:
file_path = Path(packaging.tags.__file__)
if file_path.suffix == ".pyc":
# Python 2
file_path = file_path.with_suffix(".py")

with file_path.open(encoding="utf-8") as f:
script = decode(f.read())

script = script.replace(
"from ._typing import TYPE_CHECKING, cast",
"TYPE_CHECKING = False\ncast = lambda type_, value: value",
)
script = script.replace(
"from ._typing import MYPY_CHECK_RUNNING, cast",
"MYPY_CHECK_RUNNING = False\ncast = lambda type_, value: value",
)

script += textwrap.dedent(
"""
import json
print(json.dumps([(t.interpreter, t.abi, t.platform) for t in sys_tags()]))
"""
)

output = self.run_python_script(script)
output = self.run_python_script(GET_SYS_TAGS)

return [Tag(*t) for t in json.loads(output)]

Expand Down
10 changes: 10 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ def test_env_shell_commands_with_stdinput_in_their_arg_work_as_expected(
assert run_output_path.resolve() == venv_base_prefix_path.resolve()


def test_env_get_supported_tags_matches_inside_virtualenv(tmp_dir, manager):
venv_path = Path(tmp_dir) / "Virtual Env"
manager.build_venv(str(venv_path))
venv = VirtualEnv(venv_path)

import packaging.tags

assert venv.get_supported_tags() == list(packaging.tags.sys_tags())


@pytest.fixture
def in_project_venv_dir(poetry: Poetry) -> Iterator[Path]:
os.environ.pop("VIRTUAL_ENV", None)
Expand Down

0 comments on commit a27c5a1

Please sign in to comment.