From f47bc2c24468ff5156a39923d80de09f7930ad98 Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Wed, 12 May 2021 14:56:38 +0300 Subject: [PATCH] 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. --- sqlparse/sql.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index c9e477fd..a0690624 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -48,6 +48,9 @@ class TokenBase: def __init__(self): self.parent = None + def __str__(self): + return self.value + # Pending tokenlist __len__ bug fix # def __len__(self): # return len(self.value) @@ -151,9 +154,6 @@ def __init__(self, ttype, value): self.is_whitespace = ttype in T.Whitespace self.normalized = value.upper() if self.is_keyword else value - def __str__(self): - return self.value - def _get_repr_name(self): return str(self.ttype).split('.')[-1] @@ -165,12 +165,11 @@ def flatten(self): class TokenList(TokenBase): """A group of tokens. - It has two additional instance attributes: - ``value`` is the value of the token and ``tokens`` is a list of child - tokens. + It has one additional instance attribute, ``tokens``, which is a list of + child tokens. """ - __slots__ = ('tokens', 'value') + __slots__ = 'tokens' is_group = True ttype = None @@ -180,10 +179,10 @@ class TokenList(TokenBase): def __init__(self, tokens=None): super().__init__() self.tokens = tokens or [] - self.value = str(self) [setattr(token, 'parent', self) for token in self.tokens] - def __str__(self): + @property + def value(self): return ''.join(token.value for token in self.flatten()) @property @@ -348,7 +347,6 @@ def group_tokens(self, grp_cls, start, end, include_end=True, grp = start grp.tokens.extend(subtokens) del self.tokens[start_idx + 1:end_idx] - grp.value = str(start) else: subtokens = self.tokens[start_idx:end_idx] grp = grp_cls(subtokens)