From 3cfca7e7627426986266ec3b1987649fda707a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Thu, 15 Aug 2024 18:01:58 +0200 Subject: [PATCH] fix more encoding warnings --- src/poetry/utils/env/env_manager.py | 30 +++++++++++++++---- tests/console/commands/test_run.py | 4 ++- .../repositories/test_installed_repository.py | 3 +- tests/utils/env/test_env.py | 3 +- tests/utils/env/test_env_manager.py | 6 +--- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/poetry/utils/env/env_manager.py b/src/poetry/utils/env/env_manager.py index a0fd02463df..97d645c6950 100644 --- a/src/poetry/utils/env/env_manager.py +++ b/src/poetry/utils/env/env_manager.py @@ -105,8 +105,11 @@ def _full_python_path(python: str) -> Path | None: return None try: + encoding = "locale" if sys.version_info >= (3, 10) else None executable = subprocess.check_output( - [path_python, "-c", "import sys; print(sys.executable)"], text=True + [path_python, "-c", "import sys; print(sys.executable)"], + text=True, + encoding=encoding, ).strip() return Path(executable) @@ -147,8 +150,11 @@ def get_python_version( executable = EnvManager._detect_active_python(io) if executable: + encoding = "locale" if sys.version_info >= (3, 10) else None python_patch = subprocess.check_output( - [executable, "-c", GET_PYTHON_VERSION_ONELINER], text=True + [executable, "-c", GET_PYTHON_VERSION_ONELINER], + text=True, + encoding=encoding, ).strip() version = ".".join(str(v) for v in python_patch.split(".")[:precision]) @@ -188,8 +194,11 @@ def activate(self, python: str) -> Env: raise PythonVersionNotFound(python) try: + encoding = "locale" if sys.version_info >= (3, 10) else None python_version_string = subprocess.check_output( - [python_path, "-c", GET_PYTHON_VERSION_ONELINER], text=True + [python_path, "-c", GET_PYTHON_VERSION_ONELINER], + text=True, + encoding=encoding, ) except CalledProcessError as e: raise EnvCommandError(e) @@ -354,8 +363,9 @@ def remove(self, python: str) -> Env: if python_path.is_file(): # Validate env name if provided env is a full path to python try: + encoding = "locale" if sys.version_info >= (3, 10) else None env_dir = subprocess.check_output( - [python, "-c", GET_ENV_PATH_ONELINER], text=True + [python, "-c", GET_ENV_PATH_ONELINER], text=True, encoding=encoding ).strip("\n") env_name = Path(env_dir).name if not self.check_env_is_for_current_project( @@ -398,8 +408,11 @@ def remove(self, python: str) -> Env: pass try: + encoding = "locale" if sys.version_info >= (3, 10) else None python_version_string = subprocess.check_output( - [python, "-c", GET_PYTHON_VERSION_ONELINER], text=True + [python, "-c", GET_PYTHON_VERSION_ONELINER], + text=True, + encoding=encoding, ) except CalledProcessError as e: raise EnvCommandError(e) @@ -481,8 +494,11 @@ def create_venv( python_patch = ".".join([str(v) for v in sys.version_info[:3]]) python_minor = ".".join([str(v) for v in sys.version_info[:2]]) if executable: + encoding = "locale" if sys.version_info >= (3, 10) else None python_patch = subprocess.check_output( - [executable, "-c", GET_PYTHON_VERSION_ONELINER], text=True + [executable, "-c", GET_PYTHON_VERSION_ONELINER], + text=True, + encoding=encoding, ).strip() python_minor = ".".join(python_patch.split(".")[:2]) @@ -527,10 +543,12 @@ def create_venv( continue try: + encoding = "locale" if sys.version_info >= (3, 10) else None python_patch = subprocess.check_output( [python, "-c", GET_PYTHON_VERSION_ONELINER], stderr=subprocess.STDOUT, text=True, + encoding=encoding, ).strip() except CalledProcessError: continue diff --git a/tests/console/commands/test_run.py b/tests/console/commands/test_run.py index ba2716e056d..4f07205db6e 100644 --- a/tests/console/commands/test_run.py +++ b/tests/console/commands/test_run.py @@ -1,6 +1,7 @@ from __future__ import annotations import subprocess +import sys from typing import TYPE_CHECKING @@ -116,7 +117,8 @@ def test_run_console_scripts_of_editable_dependencies_on_windows( cmd_script_file = tmp_venv._bin_dir / "quix.cmd" # `/b` ensures we only exit the script instead of any cmd.exe proc that called it - cmd_script_file.write_text("exit /b 123") + encoding = "locale" if sys.version_info >= (3, 10) else None + cmd_script_file.write_text("exit /b 123", encoding=encoding) # We prove that the CMD script executed successfully by verifying the exit code # matches what we wrote in the script assert tester.execute("quix") == 123 diff --git a/tests/repositories/test_installed_repository.py b/tests/repositories/test_installed_repository.py index b3ba33b1785..0645f0256bf 100644 --- a/tests/repositories/test_installed_repository.py +++ b/tests/repositories/test_installed_repository.py @@ -12,6 +12,7 @@ import pytest from poetry.repositories.installed_repository import InstalledRepository +from poetry.utils._compat import getencoding from poetry.utils._compat import metadata from poetry.utils.env import EnvManager from poetry.utils.env import MockEnv @@ -168,7 +169,7 @@ def fix_editable_path_for_windows( # to give inconsistent results at different phases of the test suite execution; additionally # this represents a more realistic scenario editable_pth_file = site_purelib / "editable.pth" - editable_pth_file.write_text(editable_source_directory_path) + editable_pth_file.write_text(editable_source_directory_path, encoding=getencoding()) def test_load_successful( diff --git a/tests/utils/env/test_env.py b/tests/utils/env/test_env.py index e2b97b1fb13..43d30c81d89 100644 --- a/tests/utils/env/test_env.py +++ b/tests/utils/env/test_env.py @@ -527,7 +527,8 @@ def test_detect_active_python_with_bat(poetry: Poetry, tmp_path: Path) -> None: """On Windows pyenv uses batch files for python management.""" python_wrapper = tmp_path / "python.bat" wrapped_python = Path(r"C:\SpecialPython\python.exe") - with python_wrapper.open("w") as f: + encoding = "locale" if sys.version_info >= (3, 10) else None + with python_wrapper.open("w", encoding=encoding) as f: f.write(f"@echo {wrapped_python}") os.environ["PATH"] = str(python_wrapper.parent) + os.pathsep + os.environ["PATH"] diff --git a/tests/utils/env/test_env_manager.py b/tests/utils/env/test_env_manager.py index c44aa52727e..a7b02c54513 100644 --- a/tests/utils/env/test_env_manager.py +++ b/tests/utils/env/test_env_manager.py @@ -924,17 +924,13 @@ def test_create_venv_finds_no_python_executable( manager: EnvManager, poetry: Poetry, config: Config, - mocker: MockerFixture, config_virtualenvs_path: Path, venv_name: str, ) -> None: if "VIRTUAL_ENV" in os.environ: del os.environ["VIRTUAL_ENV"] - poetry.package.python_versions = "^3.6" - - mocker.patch("sys.version_info", (3, 4, 5)) - mocker.patch("shutil.which", return_value=None) + poetry.package.python_versions = "^999" with pytest.raises(NoCompatiblePythonVersionFound) as e: manager.create_venv()