Skip to content

Commit

Permalink
Evade ValueError exception on ShorthandParserError
Browse files Browse the repository at this point in the history
  • Loading branch information
uchida committed Oct 13, 2015
1 parent de2a969 commit d1d27a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions awscli/shorthand.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,32 @@ def __init__(self, value, expected, actual, index):
super(ShorthandParseError, self).__init__(msg)

def _construct_msg(self):
if '\n' in self.value:
# If there's newlines in the expression, we want
consumed, remaining, num_spaces = self.value, '', self.index
if '\n' in self.value[:self.index]:
# If there's newlines in the consumed expression, we want
# to make sure we're only counting the spaces
# from the last newline:
# foo=bar,\n
# bar==baz
# ^
last_newline = self.value[:self.index].rindex('\n')
num_spaces = self.index - last_newline - 1
else:
num_spaces = self.index
if '\n' in self.value[self.index:]:
# If there's newline in the remaining, divide value
# into consumed and remainig
# foo==bar,\n
# ^
# bar=baz
next_newline = self.index + self.value[self.index:].index('\n')
consumed = self.value[:next_newline]
remaining = self.value[next_newline:]
msg = (
"Expected: '%s', received: '%s' for input:\n"
"%s\n"
"%s\n"
) % (self.expected, self.actual, self.value,
' ' * num_spaces + '^')
"%s"
"%s"
) % (self.expected, self.actual, consumed,
' ' * num_spaces + '^', remaining)
return msg


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_shorthand.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_error_parsing():
yield (_is_error, "foo={bar}")
yield (_is_error, "foo={bar=bar")
yield (_is_error, "foo=bar,")

yield (_is_error, "foo==bar,\nbar=baz")

def _is_error(expr):
try:
Expand Down

0 comments on commit d1d27a1

Please sign in to comment.