diff --git a/poetry/utils/env.py b/poetry/utils/env.py index d4eb3dab5bb..a572e7e4603 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -429,7 +429,7 @@ def __init__(self, poetry: Poetry) -> None: self._poetry = poetry @lru_cache() - def _python_version(self, executable: str) -> str: + def _python_version(self, executable: str) -> Version: try: python_version = decode( subprocess.check_output( @@ -446,7 +446,7 @@ def _python_version(self, executable: str) -> str: except CalledProcessError as e: raise EnvCommandError(e) - return python_version + return Version.parse(python_version.strip()) def activate(self, python: str, io: IO) -> "Env": venv_path = self._poetry.config.get("virtualenvs.path") @@ -468,23 +468,7 @@ def activate(self, python: str, io: IO) -> "Env": # Executable in PATH or full executable path pass - try: - python_version = decode( - subprocess.check_output( - list_to_shell_command( - [ - python, - "-c", - "\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"", - ] - ), - shell=True, - ) - ) - except CalledProcessError as e: - raise EnvCommandError(e) - - python_version = Version.parse(python_version.strip()) + python_version = self._python_version(python) minor = f"{python_version.major}.{python_version.minor}" patch = python_version.text @@ -577,7 +561,6 @@ def get( if executable: python_version = self._python_version(executable) - python_version = Version.parse(python_version.strip()) python_minor = f"{python_version.major}.{python_version.minor}" else: python_minor = ".".join([str(v) for v in sys.version_info[:2]]) @@ -721,23 +704,7 @@ def remove(self, python: str) -> "Env": # Executable in PATH or full executable path pass - try: - python_version = decode( - subprocess.check_output( - list_to_shell_command( - [ - python, - "-c", - "\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"", - ] - ), - shell=True, - ) - ) - except CalledProcessError as e: - raise EnvCommandError(e) - - python_version = Version.parse(python_version.strip()) + python_version = self._python_version(python) minor = f"{python_version.major}.{python_version.minor}" name = f"{base_env_name}-py{minor}" @@ -794,14 +761,14 @@ def create_venv( if not name: name = self._poetry.package.name - python_patch = ".".join([str(v) for v in sys.version_info[:3]]) + python_patch = Version.parse(".".join([str(v) for v in sys.version_info[:3]])) python_minor = ".".join([str(v) for v in sys.version_info[:2]]) if executable: python_patch = self._python_version(executable) - python_minor = ".".join(python_patch.split(".")[:2]) + python_minor = f"{python_patch.major}.{python_patch.minor}" supported_python = self._poetry.package.python_constraint - if not supported_python.allows(Version.parse(python_patch)): + if not supported_python.allows(python_patch): # The currently activated or chosen Python version # is not compatible with the Python constraint specified # for the project. @@ -810,7 +777,7 @@ def create_venv( # Otherwise, we try to find a compatible Python version. if executable: raise NoCompatiblePythonVersionFound( - self._poetry.package.python_versions, python_patch + self._poetry.package.python_versions, str(python_patch) ) io.write_line( @@ -843,29 +810,17 @@ def create_venv( io.write_line(f"Trying {python}") try: - python_patch = decode( - subprocess.check_output( - list_to_shell_command( - [ - python, - "-c", - "\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"", - ] - ), - stderr=subprocess.STDOUT, - shell=True, - ).strip() - ) + python_patch = self._python_version(python) except CalledProcessError: continue if not python_patch: continue - if supported_python.allows(Version.parse(python_patch)): + if supported_python.allows(python_patch): io.write_line(f"Using {python} ({python_patch})") executable = python - python_minor = ".".join(python_patch.split(".")[:2]) + python_minor = f"{python_patch.major}.{python_patch.minor}" break if not executable: