From 272ca2a49a7c7ed41836e6faa5da11baa63c4eec Mon Sep 17 00:00:00 2001 From: Ali Afshar Date: Mon, 5 Jan 2009 12:14:14 +0100 Subject: [PATCH] This commit makes the parser and lexer use regular string comparison to support jython --HG-- branch : trunk --- jinja2/defaults.py | 2 +- jinja2/lexer.py | 6 +-- jinja2/parser.py | 104 ++++++++++++++++++++++----------------------- 3 files changed, 56 insertions(+), 56 deletions(-) diff --git a/jinja2/defaults.py b/jinja2/defaults.py index aacd32b..b1a2f6a 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -36,4 +36,4 @@ DEFAULT_NAMESPACE = { # export all constants -__all__ = tuple(x for x in locals() if x.isupper()) +__all__ = tuple(x for x in locals().keys() if x.isupper()) diff --git a/jinja2/lexer.py b/jinja2/lexer.py index 19509ac..6124396 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -101,7 +101,7 @@ class Token(tuple): def __str__(self): if self.type in reverse_operators: return reverse_operators[self.type] - elif self.type is 'name': + elif self.type == 'name': return self.value return self.type @@ -210,7 +210,7 @@ class TokenStream(object): rv = self.current if self._pushed: self.current = self._pushed.popleft() - elif self.current.type is not 'eof': + elif self.current.type != 'eof': try: self.current = self._next() except StopIteration: @@ -230,7 +230,7 @@ class TokenStream(object): if not self.current.test(expr): if ':' in expr: expr = expr.split(':')[1] - if self.current.type is 'eof': + if self.current.type == 'eof': raise TemplateSyntaxError('unexpected end of template, ' 'expected %r.' % expr, self.current.lineno, diff --git a/jinja2/parser.py b/jinja2/parser.py index 5aa720e..c607540 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -63,7 +63,7 @@ class Parser(object): def parse_statement(self): """Parse a single statement.""" token = self.stream.current - if token.type is not 'name': + if token.type != 'name': self.fail('tag name expected', token.lineno) if token.value in _statement_keywords: return getattr(self, 'parse_' + self.stream.current.value)() @@ -202,7 +202,7 @@ class Parser(object): while 1: if node.names: self.stream.expect('comma') - if self.stream.current.type is 'name': + if self.stream.current.type == 'name': if parse_context(): break target = self.parse_assign_target(name_only=True) @@ -215,7 +215,7 @@ class Parser(object): node.names.append((target.name, alias.name)) else: node.names.append(target.name) - if parse_context() or self.stream.current.type is not 'comma': + if parse_context() or self.stream.current.type != 'comma': break else: break @@ -228,7 +228,7 @@ class Parser(object): node.args = args = [] node.defaults = defaults = [] self.stream.expect('lparen') - while self.stream.current.type is not 'rparen': + while self.stream.current.type != 'rparen': if args: self.stream.expect('comma') arg = self.parse_assign_target(name_only=True) @@ -240,7 +240,7 @@ class Parser(object): def parse_call_block(self): node = nodes.CallBlock(lineno=self.stream.next().lineno) - if self.stream.current.type is 'lparen': + if self.stream.current.type == 'lparen': self.parse_signature(node) else: node.args = [] @@ -270,7 +270,7 @@ class Parser(object): def parse_print(self): node = nodes.Output(lineno=self.stream.next().lineno) node.nodes = [] - while self.stream.current.type is not 'block_end': + while self.stream.current.type != 'block_end': if node.nodes: self.stream.expect('comma') node.nodes.append(self.parse_expression()) @@ -365,7 +365,7 @@ class Parser(object): def parse_add(self): lineno = self.stream.current.lineno left = self.parse_sub() - while self.stream.current.type is 'add': + while self.stream.current.type == 'add': self.stream.next() right = self.parse_sub() left = nodes.Add(left, right, lineno=lineno) @@ -375,7 +375,7 @@ class Parser(object): def parse_sub(self): lineno = self.stream.current.lineno left = self.parse_concat() - while self.stream.current.type is 'sub': + while self.stream.current.type == 'sub': self.stream.next() right = self.parse_concat() left = nodes.Sub(left, right, lineno=lineno) @@ -385,7 +385,7 @@ class Parser(object): def parse_concat(self): lineno = self.stream.current.lineno args = [self.parse_mul()] - while self.stream.current.type is 'tilde': + while self.stream.current.type == 'tilde': self.stream.next() args.append(self.parse_mul()) if len(args) == 1: @@ -395,7 +395,7 @@ class Parser(object): def parse_mul(self): lineno = self.stream.current.lineno left = self.parse_div() - while self.stream.current.type is 'mul': + while self.stream.current.type == 'mul': self.stream.next() right = self.parse_div() left = nodes.Mul(left, right, lineno=lineno) @@ -405,7 +405,7 @@ class Parser(object): def parse_div(self): lineno = self.stream.current.lineno left = self.parse_floordiv() - while self.stream.current.type is 'div': + while self.stream.current.type == 'div': self.stream.next() right = self.parse_floordiv() left = nodes.Div(left, right, lineno=lineno) @@ -415,7 +415,7 @@ class Parser(object): def parse_floordiv(self): lineno = self.stream.current.lineno left = self.parse_mod() - while self.stream.current.type is 'floordiv': + while self.stream.current.type == 'floordiv': self.stream.next() right = self.parse_mod() left = nodes.FloorDiv(left, right, lineno=lineno) @@ -425,7 +425,7 @@ class Parser(object): def parse_mod(self): lineno = self.stream.current.lineno left = self.parse_pow() - while self.stream.current.type is 'mod': + while self.stream.current.type == 'mod': self.stream.next() right = self.parse_pow() left = nodes.Mod(left, right, lineno=lineno) @@ -435,7 +435,7 @@ class Parser(object): def parse_pow(self): lineno = self.stream.current.lineno left = self.parse_unary() - while self.stream.current.type is 'pow': + while self.stream.current.type == 'pow': self.stream.next() right = self.parse_unary() left = nodes.Pow(left, right, lineno=lineno) @@ -445,15 +445,15 @@ class Parser(object): def parse_unary(self): token_type = self.stream.current.type lineno = self.stream.current.lineno - if token_type is 'name' and self.stream.current.value == 'not': + if token_type == 'name' and self.stream.current.value == 'not': self.stream.next() node = self.parse_unary() return nodes.Not(node, lineno=lineno) - if token_type is 'sub': + if token_type == 'sub': self.stream.next() node = self.parse_unary() return nodes.Neg(node, lineno=lineno) - if token_type is 'add': + if token_type == 'add': self.stream.next() node = self.parse_unary() return nodes.Pos(node, lineno=lineno) @@ -461,7 +461,7 @@ class Parser(object): def parse_primary(self, with_postfix=True): token = self.stream.current - if token.type is 'name': + if token.type == 'name': if token.value in ('true', 'false', 'True', 'False'): node = nodes.Const(token.value in ('true', 'True'), lineno=token.lineno) @@ -470,24 +470,24 @@ class Parser(object): else: node = nodes.Name(token.value, 'load', lineno=token.lineno) self.stream.next() - elif token.type is 'string': + elif token.type == 'string': self.stream.next() buf = [token.value] lineno = token.lineno - while self.stream.current.type is 'string': + while self.stream.current.type == 'string': buf.append(self.stream.current.value) self.stream.next() node = nodes.Const(''.join(buf), lineno=lineno) elif token.type in ('integer', 'float'): self.stream.next() node = nodes.Const(token.value, lineno=token.lineno) - elif token.type is 'lparen': + elif token.type == 'lparen': self.stream.next() node = self.parse_tuple() self.stream.expect('rparen') - elif token.type is 'lbracket': + elif token.type == 'lbracket': node = self.parse_list() - elif token.type is 'lbrace': + elif token.type == 'lbrace': node = self.parse_dict() else: self.fail("unexpected token '%s'" % (token,), token.lineno) @@ -526,7 +526,7 @@ class Parser(object): if self.is_tuple_end(extra_end_rules): break args.append(parse()) - if self.stream.current.type is 'comma': + if self.stream.current.type == 'comma': is_tuple = True else: break @@ -538,7 +538,7 @@ class Parser(object): def parse_list(self): token = self.stream.expect('lbracket') items = [] - while self.stream.current.type is not 'rbracket': + while self.stream.current.type != 'rbracket': if items: self.stream.expect('comma') if self.stream.current.type == 'rbracket': @@ -550,7 +550,7 @@ class Parser(object): def parse_dict(self): token = self.stream.expect('lbrace') items = [] - while self.stream.current.type is not 'rbrace': + while self.stream.current.type != 'rbrace': if items: self.stream.expect('comma') if self.stream.current.type == 'rbrace': @@ -565,13 +565,13 @@ class Parser(object): def parse_postfix(self, node): while 1: token_type = self.stream.current.type - if token_type is 'dot' or token_type is 'lbracket': + if token_type == 'dot' or token_type == 'lbracket': node = self.parse_subscript(node) - elif token_type is 'lparen': + elif token_type == 'lparen': node = self.parse_call(node) - elif token_type is 'pipe': + elif token_type == 'pipe': node = self.parse_filter(node) - elif token_type is 'name' and self.stream.current.value == 'is': + elif token_type == 'name' and self.stream.current.value == 'is': node = self.parse_test(node) else: break @@ -579,20 +579,20 @@ class Parser(object): def parse_subscript(self, node): token = self.stream.next() - if token.type is 'dot': + if token.type == 'dot': attr_token = self.stream.current self.stream.next() - if attr_token.type is 'name': + if attr_token.type == 'name': return nodes.Getattr(node, attr_token.value, 'load', lineno=token.lineno) - elif attr_token.type is not 'integer': + elif attr_token.type != 'integer': self.fail('expected name or number', attr_token.lineno) arg = nodes.Const(attr_token.value, lineno=attr_token.lineno) return nodes.Getitem(node, arg, 'load', lineno=token.lineno) - if token.type is 'lbracket': + if token.type == 'lbracket': priority_on_attribute = False args = [] - while self.stream.current.type is not 'rbracket': + while self.stream.current.type != 'rbracket': if args: self.stream.expect('comma') args.append(self.parse_subscribed()) @@ -607,24 +607,24 @@ class Parser(object): def parse_subscribed(self): lineno = self.stream.current.lineno - if self.stream.current.type is 'colon': + if self.stream.current.type == 'colon': self.stream.next() args = [None] else: node = self.parse_expression() - if self.stream.current.type is not 'colon': + if self.stream.current.type != 'colon': return node self.stream.next() args = [node] - if self.stream.current.type is 'colon': + if self.stream.current.type == 'colon': args.append(None) elif self.stream.current.type not in ('rbracket', 'comma'): args.append(self.parse_expression()) else: args.append(None) - if self.stream.current.type is 'colon': + if self.stream.current.type == 'colon': self.stream.next() if self.stream.current.type not in ('rbracket', 'comma'): args.append(self.parse_expression()) @@ -647,24 +647,24 @@ class Parser(object): self.fail('invalid syntax for function call expression', token.lineno) - while self.stream.current.type is not 'rparen': + while self.stream.current.type != 'rparen': if require_comma: self.stream.expect('comma') # support for trailing comma - if self.stream.current.type is 'rparen': + if self.stream.current.type == 'rparen': break - if self.stream.current.type is 'mul': + if self.stream.current.type == 'mul': ensure(dyn_args is None and dyn_kwargs is None) self.stream.next() dyn_args = self.parse_expression() - elif self.stream.current.type is 'pow': + elif self.stream.current.type == 'pow': ensure(dyn_kwargs is None) self.stream.next() dyn_kwargs = self.parse_expression() else: ensure(dyn_args is None and dyn_kwargs is None) - if self.stream.current.type is 'name' and \ - self.stream.look().type is 'assign': + if self.stream.current.type == 'name' and \ + self.stream.look().type == 'assign': key = self.stream.current.value self.stream.skip(2) value = self.parse_expression() @@ -688,10 +688,10 @@ class Parser(object): self.stream.next() token = self.stream.expect('name') name = token.value - while self.stream.current.type is 'dot': + while self.stream.current.type == 'dot': self.stream.next() name += '.' + self.stream.expect('name').value - if self.stream.current.type is 'lparen': + if self.stream.current.type == 'lparen': args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None) else: args = [] @@ -710,12 +710,12 @@ class Parser(object): else: negated = False name = self.stream.expect('name').value - while self.stream.current.type is 'dot': + while self.stream.current.type == 'dot': self.stream.next() name += '.' + self.stream.expect('name').value dyn_args = dyn_kwargs = None kwargs = [] - if self.stream.current.type is 'lparen': + if self.stream.current.type == 'lparen': args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None) elif self.stream.current.type in ('name', 'string', 'integer', 'float', 'lparen', 'lbracket', @@ -746,16 +746,16 @@ class Parser(object): while self.stream: token = self.stream.current - if token.type is 'data': + if token.type == 'data': if token.value: add_data(nodes.TemplateData(token.value, lineno=token.lineno)) self.stream.next() - elif token.type is 'variable_begin': + elif token.type == 'variable_begin': self.stream.next() add_data(self.parse_tuple(with_condexpr=True)) self.stream.expect('variable_end') - elif token.type is 'block_begin': + elif token.type == 'block_begin': flush_data() self.stream.next() if end_tokens is not None and \ -- 2.26.2