Skip to content

Commit 3dbd8ed

Browse files
committed
Read README.rst as UTF-8 regardless of external locale settings.
The sanitized environment used by Debian package build daemons forces use of the barebones ‘POSIX’ locale. Some versions of Python 3 will refuse to read an UTF-8 file as text when this locale is in use, unless explicitly told to use UTF-8 anyway. Python 2 doesn’t support the encoding= argument to open(), so to get setup.py to do the Right Thing in a universal fashion, read the file as *binary* and then explicitly decode it.
1 parent c7f6e3e commit 3dbd8ed

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

setup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ def run(self):
1717
self.run_command("build_ext")
1818
return _build_py.run(self)
1919

20+
# Read a file that may or may not exist, and decode its contents as
21+
# UTF-8, regardless of external locale settings.
2022
def read_file(filename):
2123
filepath = os.path.join(
2224
os.path.dirname(os.path.dirname(__file__)), filename)
23-
if os.path.exists(filepath):
24-
return open(filepath).read()
25-
else:
25+
try:
26+
raw = open(filepath, 'rb').read()
27+
except OSError:
2628
return ''
29+
return raw.decode('utf-8')
2730

2831
def mecab_config(arg):
2932
return os.popen("mecab-config " + arg).readlines()[0].split()

0 commit comments

Comments
 (0)