From 10fce39a7b0c100d982d1d987eed57e31bfb9779 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Thu, 22 Mar 2018 16:23:15 +0000 Subject: [PATCH] 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 --- doc/source/user/using.rst | 5 +++++ pbr/util.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/user/using.rst b/doc/source/user/using.rst index d0ff3bd5..f8da6def 100644 --- a/doc/source/user/using.rst +++ b/doc/source/user/using.rst @@ -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 diff --git a/pbr/util.py b/pbr/util.py index 163feb83..31a2a262 100644 --- a/pbr/util.py +++ b/pbr/util.py @@ -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()