Make _comment_directive more defensive

This commit is contained in:
Brad Warren 2016-08-16 18:30:45 -07:00
parent 671d7ee194
commit 76c2fe579a

View file

@ -531,18 +531,17 @@ COMMENT_BLOCK = [' ', '#', COMMENT]
def _comment_directive(block, location):
"""Add a comment to the end of the line at location."""
if len(block) > location + 1: # there is a block after us
next_entry = block[location + 1]
else:
# we're at the end of the block, pretend there's a newline after us;
# it will actually be added later in add_directives
next_entry = "\n"
if isinstance(next_entry, list):
if "Certbot" in next_entry[-1]:
next_entry = block[location + 1] if location + 1 < len(block) else None
if isinstance(next_entry, list) and next_entry:
if COMMENT in next_entry[-1]:
return
next_entry = next_entry.spaced[0] # pylint: disable=no-member
elif isinstance(next_entry, nginxparser.UnspacedList):
next_entry = next_entry.spaced[0]
else:
next_entry = next_entry[0]
block.insert(location + 1, COMMENT_BLOCK[:])
if "\n" not in next_entry:
if next_entry is not None and "\n" not in next_entry:
block.insert(location + 2, '\n')