diff --git a/loopy/target/python.py b/loopy/target/python.py index 99ec42f44b49f546cda324dfdb3c6a5b001d2222..11951abcf17e94c0fdba51042e3060735215b423 100644 --- a/loopy/target/python.py +++ b/loopy/target/python.py @@ -133,15 +133,17 @@ class ExpressionToPythonMapper(StringifyMapper): def map_if(self, expr, enclosing_prec): # Synthesize PREC_IFTHENELSE, make sure it is in the right place in the # operator precedence hierarchy (right above "or"). - from pymbolic.mapper.stringifier import PREC_LOGICAL_OR, PREC_NONE + from pymbolic.mapper.stringifier import PREC_LOGICAL_OR PREC_IFTHENELSE = PREC_LOGICAL_OR - 1 # noqa return self.parenthesize_if_needed( "{then} if {cond} else {else_}".format( - then=self.rec(expr.then, PREC_IFTHENELSE), - cond=self.rec(expr.condition, PREC_IFTHENELSE), - else_=self.rec(expr.else_, PREC_IFTHENELSE)), - enclosing_prec, PREC_NONE) + # "1 if 0 if 1 else 2 else 3" is not valid Python. + # So force parens by using an artificially higher precedence. + then=self.rec(expr.then, PREC_LOGICAL_OR), + cond=self.rec(expr.condition, PREC_LOGICAL_OR), + else_=self.rec(expr.else_, PREC_LOGICAL_OR)), + enclosing_prec, PREC_IFTHENELSE) # }}} @@ -257,7 +259,7 @@ class PythonASTBuilderBase(ASTBuilderBase): lbound, ubound, inner): ecm = codegen_state.expression_to_code_mapper - from pymbolic.mapper.stringifier import PREC_NONE + from pymbolic.mapper.stringifier import PREC_NONE, PREC_SUM from genpy import For return For( @@ -265,7 +267,7 @@ class PythonASTBuilderBase(ASTBuilderBase): "range(%s, %s + 1)" % ( ecm(lbound, PREC_NONE, "i"), - ecm(ubound, PREC_NONE, "i"), + ecm(ubound, PREC_SUM, "i"), ), inner) diff --git a/loopy/version.py b/loopy/version.py index 8516ce006bde8b8616172a72a766ec86dfcd44f1..02244f55d0dbf207a4641c3ebf6cc33b536f0421 100644 --- a/loopy/version.py +++ b/loopy/version.py @@ -32,4 +32,4 @@ except ImportError: else: _islpy_version = islpy.version.VERSION_TEXT -DATA_MODEL_VERSION = "v63-islpy%s" % _islpy_version +DATA_MODEL_VERSION = "v64-islpy%s" % _islpy_version