diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2019-01-28 17:10:37 -0500 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2019-01-28 18:09:50 -0500 |
| commit | 9150b6b7272130f11a71190905e6bd3db31afd81 (patch) | |
| tree | c46f72147efd1f12790b3623c3a9d627ca79e21a /setuptools/dist.py | |
| parent | f7447817b65c12dfe508249cea019c41ce0dc23f (diff) | |
| download | python-setuptools-git-9150b6b7272130f11a71190905e6bd3db31afd81.tar.gz | |
Prefer native strings on Python 2 when reading config files. Fixes #1653.
Diffstat (limited to 'setuptools/dist.py')
| -rw-r--r-- | setuptools/dist.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/setuptools/dist.py b/setuptools/dist.py index b8551228..ddb1787a 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -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 |
