Explicitly read setup.cfg as utf-8 on Python 3

Per the referenced bug, relying on the terminal encoding to read
setup.cfg is not safe.  Unfortunately, Python 2 doesn't accept an
encoding when reading config files so we need a fallback path for
that version.

Change-Id: If49344db2f9139c0557f6acd17671163e02468a5
Closes-Bug: 1745396
This commit is contained in:
Ben Nemec 2018-03-22 16:23:15 +00:00 committed by Stephen Finucane
parent 1f2c731485
commit 10fce39a7b
2 changed files with 10 additions and 1 deletions

View File

@ -119,6 +119,11 @@ such as the ``extract_mesages`` section provided by Babel__.
# A comment on a dedicated line
value3
.. note::
On Python 3 ``setup.cfg`` is explicitly read as UTF-8. On Python 2 the
encoding is dependent on the terminal encoding.
__ http://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files
__ http://babel.pocoo.org/en/latest/setup.html

View File

@ -214,7 +214,11 @@ def cfg_to_args(path='setup.cfg', script_args=()):
if not os.path.exists(path):
raise errors.DistutilsFileError("file '%s' does not exist" %
os.path.abspath(path))
parser.read(path)
try:
parser.read(path, encoding='utf-8')
except TypeError:
# Python 2 doesn't accept the encoding kwarg
parser.read(path)
config = {}
for section in parser.sections():
config[section] = dict()