Commit de23305c by Bjorn Neergaard Committed by Arun Babu Neelicattu

fix: Use importlib to invoke packaging.tags in Env

parent b6d4b0ad
......@@ -11,7 +11,6 @@ import shutil
import subprocess
import sys
import sysconfig
import textwrap
from contextlib import contextmanager
from copy import deepcopy
......@@ -54,6 +53,31 @@ if TYPE_CHECKING:
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
......@@ -1614,32 +1638,7 @@ class VirtualEnv(Env):
]
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)]
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment