linter: Fix UnicodeError when parsing comments

And add tests when reading non-ASCII strings and comments (both from
Python strings and from files).

Fixes: #10
This commit is contained in:
Adrien Vergé
2016-06-28 09:58:23 +02:00
parent 63dd8313f8
commit c8ba8f7e99
4 changed files with 43 additions and 3 deletions

View File

@@ -71,7 +71,10 @@ def get_costemic_problems(buffer, conf):
self.all_rules = set([r.ID for r in rules])
def process_comment(self, comment):
comment = repr(comment)
try:
comment = str(comment)
except UnicodeError:
return # this certainly wasn't a yamllint directive comment
if re.match(r'^# yamllint disable( rule:\S+)*\s*$', comment):
rules = [item[5:] for item in comment[18:].split(' ')][1:]
@@ -95,7 +98,10 @@ def get_costemic_problems(buffer, conf):
class DisableLineDirective(DisableDirective):
def process_comment(self, comment):
comment = repr(comment)
try:
comment = str(comment)
except UnicodeError:
return # this certainly wasn't a yamllint directive comment
if re.match(r'^# yamllint disable-line( rule:\S+)*\s*$', comment):
rules = [item[5:] for item in comment[23:].split(' ')][1:]

View File

@@ -49,7 +49,7 @@ class Comment(object):
self.token_after = token_after
self.comment_before = comment_before
def __repr__(self):
def __str__(self):
end = self.buffer.find('\n', self.pointer)
if end == -1:
end = self.buffer.find('\0', self.pointer)