Commit 9150b6b7 authored by Jason R. Coombs's avatar Jason R. Coombs

Prefer native strings on Python 2 when reading config files. Fixes #1653.

parent f7447817
......@@ -603,7 +603,7 @@ class Distribution(_Distribution):
for opt in options:
if opt != '__name__' and opt not in ignore_options:
val = parser.get(section, opt)
val = self._try_str(parser.get(section, opt))
opt = opt.replace('-', '_')
opt_dict[opt] = (filename, val)
......@@ -627,6 +627,26 @@ class Distribution(_Distribution):
except ValueError as msg:
raise DistutilsOptionError(msg)
@staticmethod
def _try_str(val):
"""
On Python 2, much of distutils relies on string values being of
type 'str' (bytes) and not unicode text. If the value can be safely
encoded to bytes using the default encoding, prefer that.
Why the default encoding? Because that value can be implicitly
decoded back to text if needed.
Ref #1653
"""
if six.PY3:
return val
try:
return val.encode()
except UnicodeEncodeError:
pass
return val
def _set_command_options(self, command_obj, option_dict=None):
"""
Set the options for 'command_obj' from 'option_dict'. Basically
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment