summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2015-02-21 07:48:55 +0100
committerAndi Albrecht <albrecht.andi@gmail.com>2015-02-21 07:48:55 +0100
commit51871a8ffa0de99e98e2b24794458c9aa6b36689 (patch)
tree8f5beb4e9c71c4e37513b800f118c8c67754804c
parent9eb0b8cbe89057601d577b744696b5c39f3a2b20 (diff)
parent703aec1fb30dcf5c0ed14552e601e4487707435e (diff)
downloadsqlparse-51871a8ffa0de99e98e2b24794458c9aa6b36689.tar.gz
Merge pull request #172 from JacekPliszka/master
Fix pathological case of empty statement
-rw-r--r--sqlparse/filters.py6
-rw-r--r--sqlparse/lexer.py8
-rw-r--r--tests/test_format.py3
3 files changed, 15 insertions, 2 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index 7b9b5e7..9c0a476 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -271,7 +271,11 @@ class StripWhitespaceFilter:
[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)
diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py
index a60c789..3e9a1a6 100644
--- a/sqlparse/lexer.py
+++ b/sqlparse/lexer.py
@@ -315,7 +315,13 @@ class Lexer(object):
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
diff --git a/tests/test_format.py b/tests/test_format.py
index b789b17..a105b1c 100644
--- a/tests/test_format.py
+++ b/tests/test_format.py
@@ -61,6 +61,9 @@ class TestFormat(TestCaseBase):
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)