Skip to content

Commit

Permalink
Generalize RecursionError handling.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
living180 authored and andialbrecht committed Dec 10, 2024
1 parent e57923b commit bbd8f51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
32 changes: 18 additions & 14 deletions sqlparse/engine/filter_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlparse import lexer
from sqlparse.engine import grouping
from sqlparse.engine.statement_splitter import StatementSplitter
from sqlparse.exceptions import SQLParseError
from sqlparse.filters import StripTrailingSemicolonFilter


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

def run(self, sql, encoding=None):
stream = lexer.tokenize(sql, encoding)
# Process token stream
for filter_ in self.preprocess:
stream = filter_.process(stream)
try:
stream = lexer.tokenize(sql, encoding)
# Process token stream
for filter_ in self.preprocess:
stream = filter_.process(stream)

stream = StatementSplitter().process(stream)
stream = StatementSplitter().process(stream)

# Output: Stream processed Statements
for stmt in stream:
if self._grouping:
stmt = grouping.group(stmt)
# Output: Stream processed Statements
for stmt in stream:
if self._grouping:
stmt = grouping.group(stmt)

for filter_ in self.stmtprocess:
filter_.process(stmt)
for filter_ in self.stmtprocess:
filter_.process(stmt)

for filter_ in self.postprocess:
stmt = filter_.process(stmt)
for filter_ in self.postprocess:
stmt = filter_.process(stmt)

yield stmt
yield stmt
except RecursionError as err:
raise SQLParseError('Maximum recursion depth exceeded') from err
14 changes: 5 additions & 9 deletions sqlparse/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import re

from sqlparse import tokens as T
from sqlparse.exceptions import SQLParseError
from sqlparse.utils import imt, remove_quotes


Expand Down Expand Up @@ -211,14 +210,11 @@ def flatten(self):
This method is recursively called for all child tokens.
"""
try:
for token in self.tokens:
if token.is_group:
yield from token.flatten()
else:
yield token
except RecursionError as err:
raise SQLParseError('Maximum recursion depth exceeded') from err
for token in self.tokens:
if token.is_group:
yield from token.flatten()
else:
yield token

def get_sublists(self):
for token in self.tokens:
Expand Down

0 comments on commit bbd8f51

Please sign in to comment.