Skip to content

Commit

Permalink
fix more encoding warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Aug 15, 2024
1 parent 9a48d65 commit 3cfca7e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
30 changes: 24 additions & 6 deletions src/poetry/utils/env/env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/console/commands/test_run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import subprocess
import sys

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/repositories/test_installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/env/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
6 changes: 1 addition & 5 deletions tests/utils/env/test_env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3cfca7e

Please sign in to comment.