Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use importlib to invoke packaging.tags in Env #4749

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 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
53 changes: 26 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,31 @@
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 +1638,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
12 changes: 12 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ 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: str, manager: EnvManager
):
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