From 01ab38e8f64cb4f79ce38da531570d34b6248888 Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Thu, 26 Apr 2018 08:25:42 +0200
Subject: [PATCH] Removed python3.6 only constructs for python3.5 compatibility

---
 assignment.py                  | 40 +++++++++++++++++++++++++++++-----
 astnodes.py                    |  2 +-
 boundaries/boundaryhandling.py |  4 ++--
 cpu/vectorization.py           |  3 ++-
 display_utils.py               |  2 +-
 field.py                       |  2 +-
 gpucuda/indexing.py            |  4 ++--
 integer_set_analysis.py        |  9 +++++---
 simp/assignment_collection.py  |  6 ++---
 transformations.py             | 10 ++++-----
 10 files changed, 58 insertions(+), 24 deletions(-)

diff --git a/assignment.py b/assignment.py
index 251ff6966..5eec6c027 100644
--- a/assignment.py
+++ b/assignment.py
@@ -1,7 +1,12 @@
 # -*- coding: utf-8 -*-
-from sympy.codegen.ast import Assignment
+import sympy as sp
 from sympy.printing.latex import LatexPrinter
 
+try:
+    from sympy.codegen.ast import Assignment
+except ImportError:
+    Assignment = None
+
 __all__ = ['Assignment']
 
 
@@ -9,12 +14,37 @@ def print_assignment_latex(printer, expr):
     """sympy cannot print Assignments as Latex. Thus, this function is added to the sympy Latex printer"""
     printed_lhs = printer.doprint(expr.lhs)
     printed_rhs = printer.doprint(expr.rhs)
-    return f"{printed_lhs} \leftarrow {printed_rhs}"
+    return "{printed_lhs} \leftarrow {printed_rhs}".format(printed_lhs=printed_lhs, printed_rhs=printed_rhs)
 
 
 def assignment_str(assignment):
-    return f"{assignment.lhs} ← {assignment.rhs}"
+    return "{lhs} ← {rhs}".format(lhs=assignment.lhs, rhs=assignment.rhs)
+
+
+if Assignment:
+
+    Assignment.__str__ = assignment_str
+    LatexPrinter._print_Assignment = print_assignment_latex
+
+else:
+    # back port for older sympy versions that don't have Assignment  yet
+
+    class Assignment(sp.Rel):
+
+        rel_op = ':='
+        __slots__ = []
 
+        def __new__(cls, lhs, rhs=0, **assumptions):
+            from sympy.matrices.expressions.matexpr import (
+                MatrixElement, MatrixSymbol)
+            from sympy.tensor.indexed import Indexed
+            lhs = sp.sympify(lhs)
+            rhs = sp.sympify(rhs)
+            # Tuple of things that can be on the lhs of an assignment
+            assignable = (sp.Symbol, MatrixSymbol, MatrixElement, Indexed)
+            if not isinstance(lhs, assignable):
+                raise TypeError("Cannot assign to lhs of type %s." % type(lhs))
+            return sp.Rel.__new__(cls, lhs, rhs, **assumptions)
 
-Assignment.__str__ = assignment_str
-LatexPrinter._print_Assignment = print_assignment_latex
+        __str__ = assignment_str
+        _print_Assignment = print_assignment_latex
diff --git a/astnodes.py b/astnodes.py
index 7206624ef..7ed51daac 100644
--- a/astnodes.py
+++ b/astnodes.py
@@ -494,7 +494,7 @@ class SympyAssignment(Node):
     def _repr_html_(self):
         printed_lhs = sp.latex(self.lhs)
         printed_rhs = sp.latex(self.rhs)
-        return f"${printed_lhs} \leftarrow {printed_rhs}$"
+        return "${printed_lhs} \leftarrow {printed_rhs}$".format(printed_lhs=printed_lhs, printed_rhs=printed_rhs)
 
 
 class ResolvedFieldAccess(sp.Indexed):
diff --git a/boundaries/boundaryhandling.py b/boundaries/boundaryhandling.py
index 73330c33a..f36cad5ff 100644
--- a/boundaries/boundaryhandling.py
+++ b/boundaries/boundaryhandling.py
@@ -51,13 +51,13 @@ class FlagInterface:
                 self._used_flags.add(flag)
                 assert self._is_power_of_2(flag)
                 return flag
-        raise ValueError(f"All available {self.max_bits} flags are reserved")
+        raise ValueError("All available {} flags are reserved".format(self.max_bits))
 
     def reserve_flag(self, flag):
         assert self._is_power_of_2(flag)
         flag = self.dtype(flag)
         if flag in self._used_flags:
-            raise ValueError(f"The flag {flag} is already reserved")
+            raise ValueError("The flag {flag} is already reserved".format(flag=flag))
         self._used_flags.add(flag)
         return flag
 
diff --git a/cpu/vectorization.py b/cpu/vectorization.py
index f8870c52a..d841c50b9 100644
--- a/cpu/vectorization.py
+++ b/cpu/vectorization.py
@@ -43,7 +43,8 @@ def vectorize_inner_loops_and_adapt_load_stores(ast_node, vector_width=4):
                     successful = False
                     break
                 typed_symbol = base.label
-                assert type(typed_symbol.dtype) is PointerType, f"Type of access is {typed_symbol.dtype}, {indexed}"
+                assert type(typed_symbol.dtype) is PointerType, \
+                    "Type of access is {}, {}".format(typed_symbol.dtype, indexed)
                 substitutions[indexed] = cast_func(indexed, VectorType(typed_symbol.dtype.base_type, vector_width))
         if not successful:
             warnings.warn("Could not vectorize loop because of non-consecutive memory access")
diff --git a/display_utils.py b/display_utils.py
index d4c924bc4..612d69aa3 100644
--- a/display_utils.py
+++ b/display_utils.py
@@ -27,7 +27,7 @@ def highlight_cpp(code: str):
     from pygments.lexers import CppLexer
 
     css = HtmlFormatter().get_style_defs('.highlight')
-    css_tag = f"<style>{css}</style>"
+    css_tag = "<style>{css}</style>".format(css=css)
     display(HTML(css_tag))
     return HTML(highlight(code, CppLexer(), HtmlFormatter()))
 
diff --git a/field.py b/field.py
index 29112e605..87b4785a2 100644
--- a/field.py
+++ b/field.py
@@ -269,7 +269,7 @@ class Field:
         self._layout = normalize_layout(layout)
         self.shape = shape
         self.strides = strides
-        self.latex_name: Optional[str] = None
+        self.latex_name = None  # type: Optional[str]
 
     def new_field_with_different_name(self, new_name):
         return Field(new_name, self.field_type, self._dtype, self._layout, self.shape, self.strides)
diff --git a/gpucuda/indexing.py b/gpucuda/indexing.py
index 1d2a655bf..862143abb 100644
--- a/gpucuda/indexing.py
+++ b/gpucuda/indexing.py
@@ -97,8 +97,8 @@ class BlockIndexing(AbstractIndexing):
                                                     _get_end_from_slice(self._iterationSlice, arr_shape))]
         widths = sp.Matrix(widths).subs(substitution_dict)
 
-        grid: Tuple[int, ...] = tuple(sp.ceiling(length / block_size)
-                                      for length, block_size in zip(widths, self._blockSize))
+        grid = tuple(sp.ceiling(length / block_size)
+                     for length, block_size in zip(widths, self._blockSize))  # type: : Tuple[int, ...]
         extend_bs = (1,) * (3 - len(self._blockSize))
         extend_gr = (1,) * (3 - len(grid))
 
diff --git a/integer_set_analysis.py b/integer_set_analysis.py
index 8d01c9ee7..fc4a60b13 100644
--- a/integer_set_analysis.py
+++ b/integer_set_analysis.py
@@ -36,11 +36,13 @@ def isl_iteration_set(node: ast.Node):
         loop_start_str = remove_brackets(str(loop.start))
         loop_stop_str = remove_brackets(str(loop.stop))
         ctr_name = loop.loop_counter_name
-        conditions.append(remove_brackets(f"{ctr_name} >= {loop_start_str} and {ctr_name} < {loop_stop_str}"))
+        set_string_description = "{} >= {} and {} < {}".format(ctr_name, loop_start_str, ctr_name, loop_stop_str)
+        conditions.append(remove_brackets(set_string_description))
 
     symbol_names = ','.join(degrees_of_freedom)
     condition_str = ' and '.join(conditions)
-    set_description = f"{{ [{symbol_names}] : {condition_str} }}"
+    set_description = "{{ [{symbol_names}] : {condition_str} }}".format(symbol_names=symbol_names,
+                                                                        condition_str=condition_str)
     return degrees_of_freedom, isl.BasicSet(set_description)
 
 
@@ -51,7 +53,8 @@ def simplify_loop_counter_dependent_conditional(conditional):
     if dofs_in_condition.issubset(dofs_in_loops):
         symbol_names = ','.join(dofs_in_loops)
         condition_str = remove_brackets(str(conditional.condition_expr))
-        condition_set = isl.BasicSet(f"{{ [{symbol_names}] : {condition_str} }}")
+        condition_set = isl.BasicSet("{{ [{symbol_names}] : {condition_str} }}".format(symbol_names=symbol_names,
+                                                                                       condition_str=condition_str))
 
         if condition_set.is_empty():
             conditional.replace_by_false_block()
diff --git a/simp/assignment_collection.py b/simp/assignment_collection.py
index 9b7e9948b..5fb57a1ec 100644
--- a/simp/assignment_collection.py
+++ b/simp/assignment_collection.py
@@ -326,10 +326,10 @@ class AssignmentCollection:
     def __str__(self):
         result = "Subexpressions:\n"
         for eq in self.subexpressions:
-            result += f"\t{eq}\n"
+            result += "\t{eq}\n".format(eq=eq)
         result += "Main Assignments:\n"
         for eq in self.main_assignments:
-            result += f"\t{eq}\n"
+            result += "\t{eq}\n".format(eq=eq)
         return result
 
 
@@ -343,6 +343,6 @@ class SymbolGen:
         return self
 
     def __next__(self):
-        name = f"{self._symbol}_{self._ctr}"
+        name = "{}_{}".format(self._symbol, self._ctr)
         self._ctr += 1
         return sp.Symbol(name)
diff --git a/transformations.py b/transformations.py
index 81dd22c29..62b78258d 100644
--- a/transformations.py
+++ b/transformations.py
@@ -713,12 +713,12 @@ class KernelConstraintsCheck:
             fai = self.FieldAndIndex(lhs.field, lhs.index)
             self._field_writes[fai].add(lhs.offsets)
             if len(self._field_writes[fai]) > 1:
-                raise ValueError(f"Field {lhs.field.name} is written at two different locations")
+                raise ValueError("Field {} is written at two different locations".format(lhs.field.name))
         elif isinstance(lhs, sp.Symbol):
             if lhs in self._defined_pure_symbols:
-                raise ValueError(f"Assignments not in SSA form, multiple assignments to {lhs.name}")
+                raise ValueError("Assignments not in SSA form, multiple assignments to {}".format(lhs.name))
             if lhs in self._accessed_pure_symbols:
-                raise ValueError(f"Symbol {lhs.name} is written, after it has been read")
+                raise ValueError("Symbol {} is written, after it has been read".format(lhs.name))
             self._defined_pure_symbols.add(lhs)
 
     def _update_accesses_rhs(self, rhs):
@@ -727,8 +727,8 @@ class KernelConstraintsCheck:
             for write_offset in writes:
                 assert len(writes) == 1
                 if write_offset != rhs.offsets:
-                    raise ValueError(f"Violation of loop independence condition. "
-                                     f"Field {rhs.field} is read at {rhs.offsets} and written at {write_offset}")
+                    raise ValueError("Violation of loop independence condition. Field "
+                                     "{} is read at {} and written at {}".format(rhs.field, rhs.offsets, write_offset))
             self.fields_read.add(rhs.field)
         elif isinstance(rhs, sp.Symbol):
             self._accessed_pure_symbols.add(rhs)
-- 
GitLab