Skip to content

Commit

Permalink
Use locale.getencoding for newer Python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
danyeaw committed Feb 11, 2024
1 parent 78a486d commit 6242db4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/poetry/masonry/builders/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import csv
import hashlib
import json
import locale
import os

from base64 import urlsafe_b64encode
Expand All @@ -16,6 +15,7 @@

from poetry.utils._compat import WINDOWS
from poetry.utils._compat import decode
from poetry.utils._compat import getencoding
from poetry.utils.env import build_environment
from poetry.utils.helpers import is_dir_writable
from poetry.utils.pip import pip_install
Expand Down Expand Up @@ -127,7 +127,7 @@ def _add_pth(self) -> list[Path]:

try:
pth_file = self._env.site_packages.write_text(
pth_file, content, encoding=locale.getpreferredencoding()
pth_file, content, encoding=getencoding()
)
self._debug(
f" - Adding <c2>{pth_file.name}</c2> to <b>{pth_file.parent}</b> for"
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/repositories/installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from poetry.core.utils.helpers import module_name

from poetry.repositories.repository import Repository
from poetry.utils._compat import getencoding
from poetry.utils._compat import metadata


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

with pth_file.open(encoding="utf-8") as f:
with pth_file.open(encoding=getencoding()) as f:
for line in f:
line = line.strip()
if line and not line.startswith(("#", "import ", "import\t")):
Expand Down
9 changes: 9 additions & 0 deletions src/poetry/utils/_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import locale
import sys

from contextlib import suppress
Expand Down Expand Up @@ -50,10 +51,18 @@ def encode(string: str, encodings: list[str] | None = None) -> bytes:
return string.encode(encodings[0], errors="ignore")


def getencoding():
if sys.version_info < (3, 11):
return locale.getpreferredencoding()
else:
return locale.getencoding()


__all__ = [
"WINDOWS",
"decode",
"encode",
"getencoding",
"metadata",
"tomllib",
]
3 changes: 3 additions & 0 deletions src/poetry/vcs/git/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from dulwich.client import find_git_command

from poetry.utils._compat import getencoding


if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -46,6 +48,7 @@ def run(*args: Any, **kwargs: Any) -> None:
stdout=subprocess.DEVNULL,
env=env,
text=True,
encoding=getencoding(),
)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/masonry/builders/test_editable_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import csv
import json
import locale
import os
import shutil

Expand All @@ -20,6 +19,7 @@
from poetry.factory import Factory
from poetry.masonry.builders.editable import EditableBuilder
from poetry.repositories.installed_repository import InstalledRepository
from poetry.utils._compat import getencoding
from poetry.utils.env import EnvCommandError
from poetry.utils.env import EnvManager
from poetry.utils.env import MockEnv
Expand Down Expand Up @@ -290,7 +290,7 @@ def test_builder_installs_proper_files_when_packages_configured(
pth_file = tmp_venv.site_packages.find(pth_file)[0]

paths = set()
with pth_file.open(encoding=locale.getpreferredencoding()) as f:
with pth_file.open(encoding=getencoding()) as f:
for line in f.readlines():
line = line.strip(os.linesep)
if line:
Expand Down

0 comments on commit 6242db4

Please sign in to comment.