Skip to content

Commit b96c050

Browse files
committed
Check for mode in some methods
1 parent bd880c6 commit b96c050

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

setuptools/command/easy_install.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
)
7676
import pkg_resources
7777
from ..compat import py311
78-
from ..compat.encoding import encoding_for_open
78+
from ..compat.encoding import encoding_for_open, encoding_for_open_for_mode
7979
from .._path import ensure_directory
8080
from ..extern.jaraco.text import yield_lines
8181

@@ -872,7 +872,7 @@ def write_script(self, script_name, contents, mode="t", blockers=()):
872872
ensure_directory(target)
873873
if os.path.exists(target):
874874
os.unlink(target)
875-
with open(target, "w" + mode, encoding=encoding_for_open) as f:
875+
with open(target, "w" + mode, encoding=encoding_for_open_for_mode(mode)) as f:
876876
f.write(contents)
877877
chmod(target, 0o777 - mask)
878878

setuptools/command/install_scripts.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55

66
from .._path import ensure_directory
7-
from ..compat.encoding import encoding_for_open
7+
from ..compat.encoding import encoding_for_open_for_mode
88

99

1010
class install_scripts(orig.install_scripts):
@@ -61,7 +61,7 @@ def write_script(self, script_name, contents, mode="t", *ignored):
6161
mask = current_umask()
6262
if not self.dry_run:
6363
ensure_directory(target)
64-
f = open(target, "w" + mode, encoding=encoding_for_open)
64+
f = open(target, "w" + mode, encoding=encoding_for_open_for_mode(mode))
6565
f.write(contents)
6666
f.close()
6767
chmod(target, 0o777 - mask)

setuptools/compat/encoding.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import locale
22
import sys
3+
from typing import Optional
34

45

56
encoding_for_open = (
@@ -10,6 +11,16 @@
1011
to reduce the amount of `EncodingWarning` in tests logs.
1112
"""
1213

14+
15+
def encoding_for_open_for_mode(mode: str) -> Optional[str]:
16+
"""
17+
This method exists to centralize calls to `getpreferredencoding`
18+
to reduce the amount of `EncodingWarning` in tests logs,
19+
whilst ensuring no encoding is passed to binary mode.
20+
"""
21+
return None if "b" in mode else encoding_for_open
22+
23+
1324
encoding_for_pth = locale.getencoding() if sys.version_info >= (3, 11) else None
1425
"""
1526
When working with ``.pth`` files, let's ignore UTF-8 mode (``PYTHONUTF8`` or ``python -X utf8``)

setuptools/sandbox.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pkg_resources
1414
from distutils.errors import DistutilsError
1515
from pkg_resources import working_set
16-
from .compat.encoding import encoding_for_open
16+
from .compat.encoding import encoding_for_open_for_mode
1717

1818
if sys.platform.startswith('java'):
1919
import org.python.modules.posix.PosixModule as _os
@@ -451,7 +451,8 @@ def _file(self, path, mode='r', *args, **kw):
451451
def _open(self, path, mode='r', *args, **kw):
452452
if mode not in ('r', 'rt', 'rb', 'rU', 'U') and not self._ok(path):
453453
self._violation("open", path, mode, *args, **kw)
454-
return _open(path, mode, *args, **kw, encoding=encoding_for_open)
454+
kw.setdefault("encoding", encoding_for_open_for_mode(mode))
455+
return _open(path, mode, *args, **kw)
455456

456457
def tmpnam(self):
457458
self._violation("tmpnam")

0 commit comments

Comments
 (0)