Skip to content

Commit bbd8f51

Browse files
living180andialbrecht
authored andcommitted
Generalize RecursionError handling.
There are many functions that are implemented using recursion besides TokenList.flatten() that could raise RecursionError. Move the try/except block handling RecursionError from TokenList.flatten() to FilterStack.run() to avoid any of them resulting in an unwanted RecursionError.
1 parent e57923b commit bbd8f51

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

sqlparse/engine/filter_stack.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from sqlparse import lexer
1111
from sqlparse.engine import grouping
1212
from sqlparse.engine.statement_splitter import StatementSplitter
13+
from sqlparse.exceptions import SQLParseError
1314
from sqlparse.filters import StripTrailingSemicolonFilter
1415

1516

@@ -26,22 +27,25 @@ def enable_grouping(self):
2627
self._grouping = True
2728

2829
def run(self, sql, encoding=None):
29-
stream = lexer.tokenize(sql, encoding)
30-
# Process token stream
31-
for filter_ in self.preprocess:
32-
stream = filter_.process(stream)
30+
try:
31+
stream = lexer.tokenize(sql, encoding)
32+
# Process token stream
33+
for filter_ in self.preprocess:
34+
stream = filter_.process(stream)
3335

34-
stream = StatementSplitter().process(stream)
36+
stream = StatementSplitter().process(stream)
3537

36-
# Output: Stream processed Statements
37-
for stmt in stream:
38-
if self._grouping:
39-
stmt = grouping.group(stmt)
38+
# Output: Stream processed Statements
39+
for stmt in stream:
40+
if self._grouping:
41+
stmt = grouping.group(stmt)
4042

41-
for filter_ in self.stmtprocess:
42-
filter_.process(stmt)
43+
for filter_ in self.stmtprocess:
44+
filter_.process(stmt)
4345

44-
for filter_ in self.postprocess:
45-
stmt = filter_.process(stmt)
46+
for filter_ in self.postprocess:
47+
stmt = filter_.process(stmt)
4648

47-
yield stmt
49+
yield stmt
50+
except RecursionError as err:
51+
raise SQLParseError('Maximum recursion depth exceeded') from err

sqlparse/sql.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import re
1111

1212
from sqlparse import tokens as T
13-
from sqlparse.exceptions import SQLParseError
1413
from sqlparse.utils import imt, remove_quotes
1514

1615

@@ -211,14 +210,11 @@ def flatten(self):
211210
212211
This method is recursively called for all child tokens.
213212
"""
214-
try:
215-
for token in self.tokens:
216-
if token.is_group:
217-
yield from token.flatten()
218-
else:
219-
yield token
220-
except RecursionError as err:
221-
raise SQLParseError('Maximum recursion depth exceeded') from err
213+
for token in self.tokens:
214+
if token.is_group:
215+
yield from token.flatten()
216+
else:
217+
yield token
222218

223219
def get_sublists(self):
224220
for token in self.tokens:

0 commit comments

Comments
 (0)