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

Evade ValueError exception on ShorthandParserError #1543

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
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