Skip to content

Commit 74de438

Browse files
committed
Fix encoding warnings with PEP 597 enabled
1 parent 108d732 commit 74de438

File tree

22 files changed

+101
-58
lines changed

22 files changed

+101
-58
lines changed

.github/workflows/tests.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ defaults:
1313
run:
1414
shell: bash
1515

16+
env:
17+
PYTHONWARNDEFAULTENCODING: 'true'
18+
1619
jobs:
1720
changes:
1821
name: Detect changed files

src/poetry/masonry/builders/editable.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import csv
44
import hashlib
55
import json
6-
import locale
76
import os
87

98
from base64 import urlsafe_b64encode
@@ -16,6 +15,7 @@
1615

1716
from poetry.utils._compat import WINDOWS
1817
from poetry.utils._compat import decode
18+
from poetry.utils._compat import getencoding
1919
from poetry.utils.env import build_environment
2020
from poetry.utils.helpers import is_dir_writable
2121
from poetry.utils.pip import pip_install
@@ -125,7 +125,7 @@ def _add_pth(self) -> list[Path]:
125125

126126
try:
127127
pth_file = self._env.site_packages.write_text(
128-
pth_file, content, encoding=locale.getpreferredencoding()
128+
pth_file, content, encoding=getencoding()
129129
)
130130
self._debug(
131131
f" - Adding <c2>{pth_file.name}</c2> to <b>{pth_file.parent}</b> for"
@@ -252,7 +252,8 @@ def _add_dist_info(self, added_files: list[Path]) -> None:
252252
"dir_info": {"editable": True},
253253
"url": self._poetry.file.path.parent.absolute().as_uri(),
254254
}
255-
)
255+
),
256+
encoding="utf-8",
256257
)
257258
added_files.append(direct_url_json)
258259

src/poetry/repositories/installed_repository.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from poetry.core.utils.helpers import module_name
1414

1515
from poetry.repositories.repository import Repository
16+
from poetry.utils._compat import getencoding
1617
from poetry.utils._compat import metadata
1718

1819

@@ -58,7 +59,7 @@ def get_package_paths(cls, env: Env, name: str) -> set[Path]:
5859
if not pth_file.exists():
5960
continue
6061

61-
with pth_file.open() as f:
62+
with pth_file.open(encoding=getencoding()) as f:
6263
for line in f:
6364
line = line.strip()
6465
if line and not line.startswith(("#", "import ", "import\t")):

src/poetry/utils/_compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import locale
34
import sys
45

56
from contextlib import suppress
@@ -52,6 +53,13 @@ def encode(string: str, encodings: list[str] | None = None) -> bytes:
5253
return string.encode(encodings[0], errors="ignore")
5354

5455

56+
def getencoding() -> str:
57+
if sys.version_info < (3, 11):
58+
return locale.getpreferredencoding()
59+
else:
60+
return locale.getencoding()
61+
62+
5563
def is_relative_to(this: Path, other: Path) -> bool:
5664
"""
5765
Return whether `this` path is relative to the `other` path. This is compatibility wrapper around
@@ -72,6 +80,7 @@ def is_relative_to(this: Path, other: Path) -> bool:
7280
"WINDOWS",
7381
"decode",
7482
"encode",
83+
"getencoding",
7584
"is_relative_to",
7685
"metadata",
7786
"tomllib",

src/poetry/utils/env/base_env.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,9 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:
330330
subprocess.check_call(cmd, stderr=stderr, env=env, **kwargs)
331331
output = ""
332332
else:
333+
encoding = "locale" if sys.version_info >= (3, 10) else None
333334
output = subprocess.check_output(
334-
cmd, stderr=stderr, env=env, text=True, **kwargs
335+
cmd, stderr=stderr, env=env, text=True, encoding=encoding, **kwargs
335336
)
336337
except CalledProcessError as e:
337338
raise EnvCommandError(e)

src/poetry/utils/env/virtual_env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def includes_system_site_packages(self) -> bool:
136136
return pyvenv_cfg.exists() and (
137137
re.search(
138138
r"^\s*include-system-site-packages\s*=\s*true\s*$",
139-
pyvenv_cfg.read_text(),
139+
pyvenv_cfg.read_text(encoding="utf-8"),
140140
re.IGNORECASE | re.MULTILINE,
141141
)
142142
is not None

src/poetry/utils/setup_reader.py

Whitespace-only changes.

src/poetry/vcs/git/system.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ def run(*args: Any, **kwargs: Any) -> None:
4040
git_command = find_git_command()
4141
env = os.environ.copy()
4242
env["GIT_TERMINAL_PROMPT"] = "0"
43-
subprocess.check_call(
43+
subprocess.check_call( # type: ignore[call-arg]
4444
git_command + list(args),
4545
stderr=subprocess.DEVNULL,
4646
stdout=subprocess.DEVNULL,
4747
env=env,
4848
text=True,
49+
encoding="utf-8",
4950
)
5051

5152
@staticmethod

tests/console/commands/env/test_remove.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@ def test_remove_all(
7676
if envs_file == "empty":
7777
envs_file_path.touch()
7878
elif envs_file == "self":
79-
envs_file_path.write_text(f'[{venv_name}]\nminor = "3.9"\npatch = "3.9.1"\n')
79+
envs_file_path.write_text(
80+
f'[{venv_name}]\nminor = "3.9"\npatch = "3.9.1"\n', encoding="utf-8"
81+
)
8082
elif envs_file == "other":
81-
envs_file_path.write_text('[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n')
83+
envs_file_path.write_text(
84+
'[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n', encoding="utf-8"
85+
)
8286
elif envs_file == "self_and_other":
8387
envs_file_path.write_text(
8488
f'[{venv_name}]\nminor = "3.9"\npatch = "3.9.1"\n'
85-
'[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n'
89+
'[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n',
90+
encoding="utf-8",
8691
)
8792
else:
8893
# no envs file -> nothing to prepare
@@ -97,7 +102,7 @@ def test_remove_all(
97102

98103
if envs_file is not None:
99104
assert envs_file_path.exists()
100-
envs_file_content = envs_file_path.read_text()
105+
envs_file_content = envs_file_path.read_text(encoding="utf-8")
101106
assert venv_name not in envs_file_content
102107
if "other" in envs_file:
103108
assert "other-abcdefgh" in envs_file_content

tests/console/commands/self/test_install.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_self_install(
4545
assert isinstance(command, SelfInstallCommand)
4646
pyproject_path = command.system_pyproject
4747
if pyproject_content:
48-
pyproject_path.write_text(pyproject_content)
48+
pyproject_path.write_text(pyproject_content, encoding="utf-8")
4949
else:
5050
assert not pyproject_path.exists()
5151

0 commit comments

Comments
 (0)