Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix confusing ReadError traceback #262

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/test_syntax_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ def test_sets(self):
' a:\n'
' set\n'
'...\n', None)

def test_invalid_char_last(self):
self.check('---\nkey: value\t\n',
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
None, problem=(2, 11))

def test_invalid_char_first(self):
self.check('---\n\tkey: value\n',
None, problem=(2, 1))

def test_invalid_char_on_second_line(self):
self.check('---\nfoo: bar\n\toutput: foo\n',
None, problem=(3, 1))

def test_invalid_chars(self):
# We expect to identify only the first syntax error as this kind of
# error is fatal (stops parsing)
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
self.check('---\nkey: \tfoo\tbar\n',
None,
problem=(2, 6))

def test_invalid_char_with_other_rule(self):
self.check('key: value\t\n',
None,
problem1=(1, 1, 'document-start'),
problem2=(1, 11))
9 changes: 9 additions & 0 deletions yamllint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ def get_syntax_error(buffer):
'syntax error: ' + e.problem + ' (syntax)')
problem.level = 'error'
return problem
except yaml.reader.ReaderError as e:
# we need to convert position into line+column
line = buffer.count('\n', 0, e.position)
col = e.position - (buffer.rindex('\n', 0, e.position) if line else 1)
problem = LintProblem(line + 1,
col,
'syntax error: ' + e.reason + ' (syntax)')
problem.level = 'error'
return problem


def _run(buffer, conf, filepath):
Expand Down
5 changes: 3 additions & 2 deletions yamllint/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def comments_between_tokens(token1, token2):


def token_or_comment_generator(buffer):
yaml_loader = yaml.BaseLoader(buffer)

try:
yaml_loader = yaml.BaseLoader(buffer)
prev = None
curr = yaml_loader.get_token()
while curr is not None:
Expand All @@ -139,7 +139,8 @@ def token_or_comment_generator(buffer):
prev = curr
curr = next

except yaml.scanner.ScannerError:
# errors like this are already analysed by get_syntax_error
except (yaml.scanner.ScannerError, yaml.reader.ReaderError):
pass


Expand Down