Skip to content

Commit 74a6228

Browse files
committed
Refactor some try..excepts into read_utf8_with_fallback
Extract common pattern for reading a file with UTF-8 into the unicode_utils module.
1 parent 2b82912 commit 74a6228

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

setuptools/command/develop.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from setuptools import namespaces
1111
import setuptools
1212

13-
from ..compat import py39
13+
from ..unicode_utils import read_utf8_with_fallback
1414

1515

1616
class develop(namespaces.DevelopInstaller, easy_install):
@@ -131,14 +131,10 @@ def uninstall_link(self):
131131
if os.path.exists(self.egg_link):
132132
log.info("Removing %s (link to %s)", self.egg_link, self.egg_base)
133133

134-
try:
135-
with open(self.egg_link, encoding="utf-8") as egg_link_file:
136-
contents = [line.rstrip() for line in egg_link_file]
137-
except UnicodeDecodeError: # pragma: no cover
138-
with open(
139-
self.egg_link, encoding=py39.LOCALE_ENCODING
140-
) as egg_link_file:
141-
contents = [line.rstrip() for line in egg_link_file]
134+
contents = [
135+
line.rstrip()
136+
for line in read_utf8_with_fallback(self.egg_link).splitlines()
137+
]
142138

143139
if contents not in ([self.egg_path], [self.egg_path, self.setup_path]):
144140
log.warn("Link points to %s: uninstall aborted", contents)
@@ -165,14 +161,7 @@ def install_egg_scripts(self, dist):
165161
for script_name in self.distribution.scripts or []:
166162
script_path = os.path.abspath(convert_path(script_name))
167163
script_name = os.path.basename(script_path)
168-
169-
try:
170-
with open(script_path, encoding="utf-8") as strm:
171-
script_text = strm.read()
172-
except UnicodeDecodeError: # pragma: no cover
173-
with open(script_path, encoding=py39.LOCALE_ENCODING) as strm:
174-
script_text = strm.read()
175-
164+
script_text = read_utf8_with_fallback(script_path)
176165
self.install_script(dist, script_name, script_text, script_path)
177166

178167
return None

setuptools/package_index.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from setuptools.extern.more_itertools import unique_everseen
4242

4343
from .compat import py39
44+
from .unicode_utils import read_utf8_with_fallback
4445

4546

4647
EGG_FRAGMENT = re.compile(r'^egg=([-A-Za-z0-9_.+!]+)$')
@@ -1120,12 +1121,7 @@ def local_open(url):
11201121
for f in os.listdir(filename):
11211122
filepath = os.path.join(filename, f)
11221123
if f == 'index.html':
1123-
try:
1124-
with open(filepath, 'r', encoding="utf-8") as fp:
1125-
body = fp.read()
1126-
except UnicodeDecodeError: # pragma: no cover
1127-
with open(filepath, 'r', encoding=py39.LOCALE_ENCODING) as fp:
1128-
body = fp.read()
1124+
body = read_utf8_with_fallback(filepath)
11291125
break
11301126
elif os.path.isdir(filepath):
11311127
f += '/'

setuptools/unicode_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unicodedata
22
import sys
33

4+
from .compat import py39
5+
46

57
# HFS Plus uses decomposed UTF-8
68
def decompose(path):
@@ -42,3 +44,18 @@ def try_encode(string, enc):
4244
return string.encode(enc)
4345
except UnicodeEncodeError:
4446
return None
47+
48+
49+
def read_utf8_with_fallback(file: str, fallback_encoding=py39.LOCALE_ENCODING) -> str:
50+
"""
51+
First try to read the file with UTF-8, if there is an error fallback to a
52+
different encoding ("locale" by default). Returns the content of the file.
53+
Also useful when reading files that might have been produced by an older version of
54+
setuptools.
55+
"""
56+
try:
57+
with open(file, "r", encoding="utf-8") as f:
58+
return f.read()
59+
except UnicodeDecodeError: # pragma: no cover
60+
with open(file, "r", encoding=fallback_encoding) as f:
61+
return f.read()

setuptools/wheel.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from setuptools.command.egg_info import write_requirements, _egg_basename
1919
from setuptools.archive_util import _unpack_zipfile_obj
2020

21-
from .compat import py39
21+
from .unicode_utils import read_utf8_with_fallback
2222

2323

2424
WHEEL_NAME = re.compile(
@@ -224,12 +224,7 @@ def _move_data_entries(destination_eggdir, dist_data):
224224
def _fix_namespace_packages(egg_info, destination_eggdir):
225225
namespace_packages = os.path.join(egg_info, 'namespace_packages.txt')
226226
if os.path.exists(namespace_packages):
227-
try:
228-
with open(namespace_packages, encoding="utf-8") as fp:
229-
namespace_packages = fp.read().split()
230-
except UnicodeDecodeError: # pragma: no cover
231-
with open(namespace_packages, encoding=py39.LOCALE_ENCODING) as fp:
232-
namespace_packages = fp.read().split()
227+
namespace_packages = read_utf8_with_fallback(namespace_packages).split()
233228

234229
for mod in namespace_packages:
235230
mod_dir = os.path.join(destination_eggdir, *mod.split('.'))

0 commit comments

Comments
 (0)