Skip to content

Commit ff4f391

Browse files
committed
Make TokenList.value a property not an attribute.
The fact that a new value was being computed each time TokenList.group_tokens() was called caused supra-linear runtime when token grouping was enabled. Address by making TokenList.value a dynamically-computed property rather than a static attribute.
1 parent 7de0ab1 commit ff4f391

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

sqlparse/sql.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class TokenBase:
4848
def __init__(self):
4949
self.parent = None
5050

51+
def __str__(self):
52+
return self.value
53+
5154
# Pending tokenlist __len__ bug fix
5255
# def __len__(self):
5356
# return len(self.value)
@@ -151,9 +154,6 @@ def __init__(self, ttype, value):
151154
self.is_whitespace = ttype in T.Whitespace
152155
self.normalized = value.upper() if self.is_keyword else value
153156

154-
def __str__(self):
155-
return self.value
156-
157157
def _get_repr_name(self):
158158
return str(self.ttype).split('.')[-1]
159159

@@ -165,11 +165,11 @@ def flatten(self):
165165
class TokenList(TokenBase):
166166
"""A group of tokens.
167167
168-
It has two additional instance attributes, ``value``, which is the value of
169-
the token list, and ``tokens``, which holds a list of child-tokens.
168+
It has an additional instance attribute ``tokens`` which holds a
169+
list of child-tokens.
170170
"""
171171

172-
__slots__ = ('tokens', 'value')
172+
__slots__ = 'tokens'
173173

174174
is_group = True
175175
ttype = None
@@ -179,10 +179,10 @@ class TokenList(TokenBase):
179179
def __init__(self, tokens=None):
180180
super().__init__()
181181
self.tokens = tokens or []
182-
self.value = str(self)
183182
[setattr(token, 'parent', self) for token in self.tokens]
184183

185-
def __str__(self):
184+
@property
185+
def value(self):
186186
return ''.join(token.value for token in self.flatten())
187187

188188
@property
@@ -347,7 +347,6 @@ def group_tokens(self, grp_cls, start, end, include_end=True,
347347
grp = start
348348
grp.tokens.extend(subtokens)
349349
del self.tokens[start_idx + 1:end_idx]
350-
grp.value = str(start)
351350
else:
352351
subtokens = self.tokens[start_idx:end_idx]
353352
grp = grp_cls(subtokens)

0 commit comments

Comments
 (0)