Skip to content

Commit

Permalink
Merge pull request #172 from JacekPliszka/master
Browse files Browse the repository at this point in the history
Fix pathological case of empty statement
  • Loading branch information
andialbrecht committed Feb 21, 2015
2 parents 9eb0b8c + 703aec1 commit 51871a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion sqlparse/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ def process(self, stack, stmt, depth=0):
[self.process(stack, sgroup, depth + 1)
for sgroup in stmt.get_sublists()]
self._stripws(stmt)
if depth == 0 and stmt.tokens[-1].is_whitespace():
if (
depth == 0
and stmt.tokens
and stmt.tokens[-1].is_whitespace()
):
stmt.tokens.pop(-1)


Expand Down
8 changes: 7 additions & 1 deletion sqlparse/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ def get_tokens_unprocessed(self, stream, stack=('root',)):
statestack.pop()
elif state == '#push':
statestack.append(statestack[-1])
else:
elif (
# Ugly hack - multiline-comments
# are not stackable
state != 'multiline-comments'
or not statestack
or statestack[-1] != 'multiline-comments'
):
statestack.append(state)
elif isinstance(new_state, int):
# pop
Expand Down
3 changes: 3 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def test_strip_comments_multi(self):
sql = 'select (/* sql starts here */ select 2)'
res = sqlparse.format(sql, strip_comments=True)
self.ndiffAssertEqual(res, 'select (select 2)')
sql = 'select (/* sql /* starts here */ select 2)'
res = sqlparse.format(sql, strip_comments=True)
self.ndiffAssertEqual(res, 'select (select 2)')

def test_strip_ws(self):
f = lambda sql: sqlparse.format(sql, strip_whitespace=True)
Expand Down

0 comments on commit 51871a8

Please sign in to comment.