From: Georg Brandl Date: Wed, 11 Apr 2007 14:49:16 +0000 (+0200) Subject: [svn] Correctly recognize and translate the floor division operator. X-Git-Tag: 2.0rc1~366 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=324a91f5b5599513fb868c53ce4436e068b3c966;p=jinja2.git [svn] Correctly recognize and translate the floor division operator. --HG-- branch : trunk --- diff --git a/jinja/lexer.py b/jinja/lexer.py index 4d5986a..335bbdc 100644 --- a/jinja/lexer.py +++ b/jinja/lexer.py @@ -45,7 +45,7 @@ number_re = re.compile(r'\d+(\.\d+)*') operator_re = re.compile('(%s)' % '|'.join([ isinstance(x, unicode) and str(x) or re.escape(x) for x in [ # math operators - '+', '-', '*', '/', '%', + '+', '-', '*', '//', '/', '%', # braces and parenthesis '[', ']', '(', ')', '{', '}', # attribute access and comparison / logical operators diff --git a/jinja/translators/python.py b/jinja/translators/python.py index a75a6e9..1fe6335 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -191,6 +191,7 @@ class PythonTranslator(Translator): ast.Add: self.handle_add, ast.Sub: self.handle_sub, ast.Div: self.handle_div, + ast.FloorDiv: self.handle_floor_div, ast.Mul: self.handle_mul, ast.Mod: self.handle_mod, ast.UnaryAdd: self.handle_unary_add, @@ -905,6 +906,15 @@ class PythonTranslator(Translator): self.handle_node(node.right) ) + def handle_floor_div(self, node): + """ + Divide two items, return truncated result. + """ + return '(%s // %s)' % ( + self.handle_node(node.left), + self.handle_node(node.right) + ) + def handle_mul(self, node): """ Multiply two items.