Fix linenos being wrong in inventory errors

Fixes #20356

(cherry picked from commit 05879d331a)
This commit is contained in:
Toshio Kuratomi 2017-01-18 21:27:13 -08:00
parent e85f3f4461
commit 26f5e4b32d

View file

@ -38,6 +38,8 @@ class InventoryParser(object):
Takes an INI-format inventory file and builds a list of groups and subgroups Takes an INI-format inventory file and builds a list of groups and subgroups
with their associated hosts and variable settings. with their associated hosts and variable settings.
""" """
_COMMENT_MARKERS = frozenset((u';', u'#'))
b_COMMENT_MARKERS = frozenset((b';', b'#'))
def __init__(self, loader, groups, filename=C.DEFAULT_HOST_LIST): def __init__(self, loader, groups, filename=C.DEFAULT_HOST_LIST):
self.filename = filename self.filename = filename
@ -60,12 +62,19 @@ class InventoryParser(object):
try: try:
# Faster to do to_text once on a long string than many # Faster to do to_text once on a long string than many
# times on smaller strings # times on smaller strings
data = to_text(b_data, errors='surrogate_or_strict') data = to_text(b_data, errors='surrogate_or_strict').splitlines()
data = [line for line in data.splitlines() if not (line.startswith(u';') or line.startswith(u'#'))]
except UnicodeError: except UnicodeError:
# Skip comment lines here to avoid potential undecodable # Handle non-utf8 in comment lines: https://github.com/ansible/ansible/issues/17593
# errors in comments: https://github.com/ansible/ansible/issues/17593 data = []
data = [to_text(line, errors='surrogate_or_strict') for line in b_data.splitlines() if not (line.startswith(b';') or line.startswith(b'#'))] for line in b_data.splitlines():
if line and line[0] in self.b_COMMENT_MARKERS:
# Replace is okay for comment lines
#data.append(to_text(line, errors='surrogate_or_replace'))
# Currently we only need these lines for accurate lineno in errors
data.append(u'')
else:
# Non-comment lines still have to be valid uf-8
data.append(to_text(line, errors='surrogate_or_strict'))
self._parse(data) self._parse(data)
@ -95,8 +104,8 @@ class InventoryParser(object):
line = line.strip() line = line.strip()
# Skip empty lines # Skip empty lines and comments
if not line: if not line or line[0] in self._COMMENT_MARKERS:
continue continue
# Is this a [section] header? That tells us what group we're parsing # Is this a [section] header? That tells us what group we're parsing