Fix encoding issues with file paths. (#50830)

* Fix encoding issues with file paths.

Discovered while testing with ANSIBLE_CONFIG env var set to a path
that contained unicode characters while LC_ALL=C.

* Fix unit tests.

* Fix another path encoding issue.
This commit is contained in:
Matt Clay 2019-01-14 13:06:47 -08:00 committed by GitHub
commit 465df0ef8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 13 deletions

View file

@ -186,7 +186,7 @@ def find_ini_config_file(warnings=None):
path_from_env = os.getenv("ANSIBLE_CONFIG", SENTINEL)
if path_from_env is not SENTINEL:
path_from_env = unfrackpath(path_from_env, follow=False)
if os.path.isdir(path_from_env):
if os.path.isdir(to_bytes(path_from_env)):
path_from_env = os.path.join(path_from_env, "ansible.cfg")
potential_paths.append(path_from_env)
@ -214,7 +214,7 @@ def find_ini_config_file(warnings=None):
potential_paths.append("/etc/ansible/ansible.cfg")
for path in potential_paths:
if os.path.exists(path):
if os.path.exists(to_bytes(path)):
break
else:
path = None
@ -254,7 +254,7 @@ class ConfigManager(object):
# consume configuration
if self._config_file:
if os.path.exists(self._config_file):
if os.path.exists(to_bytes(self._config_file)):
# initialize parser and read config
self._parse_config_file()
@ -288,7 +288,7 @@ class ConfigManager(object):
if cfile is not None:
if ftype == 'ini':
self._parsers[cfile] = configparser.ConfigParser()
with open(cfile, 'rb') as f:
with open(to_bytes(cfile), 'rb') as f:
try:
cfg_text = to_text(f.read(), errors='surrogate_or_strict')
except UnicodeError as e: