Improved usage of string interning. This should fix the problems with jython.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 330fbc0 · Browse files · 1 file·+97 added−49 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+97 added−49 removed
- other:+97 added−49 removed
Provenance
- Commit
b3b5802- Tree
0a3da3f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
330fbc0
1 file changed+97 added−49 removed
modified:jinja2/lexer.py+97 added-49 removed
32 unmodified lines33 name_re = re.compile(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b')34 float_re = re.compile(r'(?<!\.)\d+\.\d+')35 newline_re = re.compile(r'(\r\n|\r|\n)')36 37+# internal the tokens and keep references to them38+TOKEN_ADD = intern('add')39+TOKEN_ASSIGN = intern('assign')40+TOKEN_COLON = intern('colon')41+TOKEN_COMMA = intern('comma')42+TOKEN_DIV = intern('div')43+TOKEN_DOT = intern('dot')44+TOKEN_EQ = intern('eq')45+TOKEN_FLOORDIV = intern('floordiv')46+TOKEN_GT = intern('gt')47+TOKEN_GTEQ = intern('gteq')48+TOKEN_LBRACE = intern('lbrace')49+TOKEN_LBRACKET = intern('lbracket')50+TOKEN_LPAREN = intern('lparen')51+TOKEN_LT = intern('lt')52+TOKEN_LTEQ = intern('lteq')53+TOKEN_MOD = intern('mod')54+TOKEN_MUL = intern('mul')55+TOKEN_NE = intern('ne')56+TOKEN_PIPE = intern('pipe')57+TOKEN_POW = intern('pow')58+TOKEN_RBRACE = intern('rbrace')59+TOKEN_RBRACKET = intern('rbracket')60+TOKEN_RPAREN = intern('rparen')61+TOKEN_SEMICOLON = intern('semicolon')62+TOKEN_SUB = intern('sub')63+TOKEN_TILDE = intern('tilde')64+TOKEN_WHITESPACE = intern('whitespace')65+TOKEN_FLOAT = intern('float')66+TOKEN_INTEGER = intern('integer')67+TOKEN_NAME = intern('name')68+TOKEN_STRING = intern('string')69+TOKEN_OPERATOR = intern('operator')70+TOKEN_BLOCK_BEGIN = intern('block_begin')71+TOKEN_BLOCK_END = intern('block_end')72+TOKEN_VARIABLE_BEGIN = intern('variable_begin')73+TOKEN_VARIABLE_END = intern('variable_end')74+TOKEN_RAW_BEGIN = intern('raw_begin')75+TOKEN_RAW_END = intern('raw_end')76+TOKEN_COMMENT_BEGIN = intern('comment_begin')77+TOKEN_COMMENT_END = intern('comment_end')78+TOKEN_COMMENT = intern('comment')79+TOKEN_LINESTATEMENT_BEGIN = intern('linestatement_begin')80+TOKEN_LINESTATEMENT_END = intern('linestatement_end')81+TOKEN_DATA = intern('data')82+TOKEN_INITIAL = intern('initial')83+TOKEN_EOF = intern('eof')84+85 # bind operators to token types86 operators = {39- '+': 'add',40- '-': 'sub',41- '/': 'div',42- '//': 'floordiv',43- '*': 'mul',44- '%': 'mod',45- '**': 'pow',46- '~': 'tilde',47- '[': 'lbracket',48- ']': 'rbracket',49- '(': 'lparen',50- ')': 'rparen',51- '{': 'lbrace',52- '}': 'rbrace',53- '==': 'eq',54- '!=': 'ne',55- '>': 'gt',56- '>=': 'gteq',57- '<': 'lt',58- '<=': 'lteq',59- '=': 'assign',60- '.': 'dot',61- ':': 'colon',62- '|': 'pipe',63- ',': 'comma',64- ';': 'semicolon'87+ '+': TOKEN_ADD,88+ '-': TOKEN_SUB,89+ '/': TOKEN_DIV,90+ '//': TOKEN_FLOORDIV,91+ '*': TOKEN_MUL,92+ '%': TOKEN_MOD,93+ '**': TOKEN_POW,94+ '~': TOKEN_TILDE,95+ '[': TOKEN_LBRACKET,96+ ']': TOKEN_RBRACKET,97+ '(': TOKEN_LPAREN,98+ ')': TOKEN_RPAREN,99+ '{': TOKEN_LBRACE,100+ '}': TOKEN_RBRACE,101+ '==': TOKEN_EQ,102+ '!=': TOKEN_NE,103+ '>': TOKEN_GT,104+ '>=': TOKEN_GTEQ,105+ '<': TOKEN_LT,106+ '<=': TOKEN_LTEQ,107+ '=': TOKEN_ASSIGN,108+ '.': TOKEN_DOT,109+ ':': TOKEN_COLON,110+ '|': TOKEN_PIPE,111+ ',': TOKEN_COMMA,112+ ';': TOKEN_SEMICOLON113 }114 115 reverse_operators = dict([(v, k) for k, v in operators.iteritems()])116 assert len(operators) == len(reverse_operators), 'operators dropped'76 unmodified lines193 return self194 195 def next(self):196 token = self.stream.current149- if token.type == 'eof':197+ if token.type is TOKEN_EOF:198 self.stream.close()199 raise StopIteration()200 self.stream.next()201 return token10 unmodified lines212 self._pushed = deque()213 self.name = name214 self.filename = filename215 self.closed = False168- self.current = Token(1, 'initial', '')216+ self.current = Token(1, TOKEN_INITIAL, '')217 self.next()218 219 def __iter__(self):220 return TokenStreamIterator(self)221 222 def __nonzero__(self):223 """Are we at the end of the stream?"""176- return bool(self._pushed) or self.current.type != 'eof'224+ return bool(self._pushed) or self.current.type is not TOKEN_EOF225 226 eos = property(lambda x: not x.__nonzero__(), doc=__nonzero__.__doc__)227 228 def push(self, token):28 unmodified lines257 """Go one token ahead and return the old one"""258 rv = self.current259 if self._pushed:260 self.current = self._pushed.popleft()213- elif self.current.type != 'eof':261+ elif self.current.type is not TOKEN_EOF:262 try:263 self.current = self._next()264 except StopIteration:265 self.close()266 return rv267 268 def close(self):269 """Close the stream."""222- self.current = Token(self.current.lineno, 'eof', '')270+ self.current = Token(self.current.lineno, TOKEN_EOF, '')271 self._next = None272 self.closed = True273 274 def expect(self, expr):2 unmodified lines277 """278 if not self.current.test(expr):279 if ':' in expr:280 expr = expr.split(':')[1]233- if self.current.type == 'eof':281+ if self.current.type is TOKEN_EOF:282 raise TemplateSyntaxError('unexpected end of template, '283 'expected %r.' % expr,284 self.current.lineno,285 self.name, self.filename)39 unmodified lines325 e = re.escape326 327 # lexing rules for tags328 tag_rules = [281- (whitespace_re, 'whitespace', None),282- (float_re, 'float', None),283- (integer_re, 'integer', None),284- (name_re, 'name', None),285- (string_re, 'string', None),286- (operator_re, 'operator', None)329+ (whitespace_re, TOKEN_WHITESPACE, None),330+ (float_re, TOKEN_FLOAT, None),331+ (integer_re, TOKEN_INTEGER, None),332+ (name_re, TOKEN_NAME, None),333+ (string_re, TOKEN_STRING, None),334+ (operator_re, TOKEN_OPERATOR, None)335 ]336 337 # assamble the root lexing rule. because "|" is ungreedy338 # we have to sort by length so that the lexer continues working34 unmodified lines373 e(environment.block_end_string)374 )] + [375 '(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)376 for n, r in root_tag_rules329- ])), ('data', '#bygroup'), '#bygroup'),377+ ])), (TOKEN_DATA, '#bygroup'), '#bygroup'),378 # data379 (c('.+'), 'data', None)380 ],381 # comments334- 'comment_begin': [382+ TOKEN_COMMENT_BEGIN: [383 (c(r'(.*?)((?:\-%s\s*|%s)%s)' % (384 e(environment.comment_end_string),385 e(environment.comment_end_string),386 block_suffix_re339- )), ('comment', 'comment_end'), '#pop'),387+ )), (TOKEN_COMMENT, TOKEN_COMMENT_END), '#pop'),388 (c('(.)'), (Failure('Missing end of comment tag'),), None)389 ],390 # blocks343- 'block_begin': [391+ TOKEN_BLOCK_BEGIN: [392 (c('(?:\-%s\s*|%s)%s' % (393 e(environment.block_end_string),394 e(environment.block_end_string),395 block_suffix_re348- )), 'block_end', '#pop'),396+ )), TOKEN_BLOCK_END, '#pop'),397 ] + tag_rules,398 # variables351- 'variable_begin': [399+ TOKEN_VARIABLE_BEGIN: [400 (c('\-%s\s*|%s' % (401 e(environment.variable_end_string),402 e(environment.variable_end_string)355- )), 'variable_end', '#pop')403+ )), TOKEN_VARIABLE_END, '#pop')404 ] + tag_rules,405 # raw block358- 'raw_begin': [406+ TOKEN_RAW_BEGIN: [407 (c('(.*?)((?:\s*%s\-|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (408 e(environment.block_start_string),409 e(environment.block_start_string),410 e(environment.block_end_string),411 e(environment.block_end_string),412 block_suffix_re365- )), ('data', 'raw_end'), '#pop'),413+ )), (TOKEN_DATA, TOKEN_RAW_END), '#pop'),414 (c('(.)'), (Failure('Missing end of raw directive'),), None)415 ],416 # line statements369- 'linestatement_begin': [370- (c(r'\s*(\n|$)'), 'linestatement_end', '#pop')417+ TOKEN_LINESTATEMENT_BEGIN: [418+ (c(r'\s*(\n|$)'), TOKEN_LINESTATEMENT_END, '#pop')419 ] + tag_rules420 }421 422 def _normalize_newlines(self, value):187 unmodified lines