From 341f4c330f1fbe6c1636f70533939ea43ef2b392 Mon Sep 17 00:00:00 2001 From: Mondus Date: Thu, 1 Sep 2022 15:51:14 +0100 Subject: [PATCH 1/2] Python 3.7. support for code generator --- swig/python/codegen/codegen.py | 33 ++++++++++++++++------- tests/swig/python/codegen/test_codegen.py | 3 ++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/swig/python/codegen/codegen.py b/swig/python/codegen/codegen.py index 9bd59dd37..652ed4857 100644 --- a/swig/python/codegen/codegen.py +++ b/swig/python/codegen/codegen.py @@ -222,11 +222,16 @@ def dispatchMacroEnvFunction(self, tree, tree_parent): bounds = tree_parent.args[1:] # process bounds by appending to cpp function template arguments for i in bounds: - if not isinstance(i, ast.Constant): - self.RaiseError(tree, f" Macro environment function argument '{i}' should be an constant value.") - if not isinstance(i.value, int): - self.RaiseError(tree, f" Macro environment function argument '{i}' should be an integer value.") - cpp_func_name += f", {i.value}" + if isinstance(i, ast.Num): # num required for python 3.7 + if not isinstance(i.n, int): + self.RaiseError(tree, f" Macro environment function argument '{i}' should be an integer value.") + cpp_func_name += f", {i.n}" + else: # all Python > 3.7 + if not isinstance(i, ast.Constant): + self.RaiseError(tree, f" Macro environment function argument '{i}' should be an constant value (or Num in Python <3.8).") + if not isinstance(i.value, int): + self.RaiseError(tree, f" Macro environment function argument '{i}' should be an integer value.") + cpp_func_name += f", {i.value}" # remove bounds from argument list (in place) del tree_parent.args[1:] cpp_func_name += ">" @@ -566,6 +571,9 @@ def _Expr(self, tree): if isinstance(tree.value, ast.Constant): if isinstance(tree.value.value, str): return + # catch special case of Python 3.7 Where doc string is a Str and not a Constant + elif isinstance(tree.value, ast.Str): + return # otherwise treat like a normal expression self.fill() self.dispatch(tree.value) @@ -892,10 +900,11 @@ def _AsyncWith(self, t): # expr def _Bytes(self, t): - self.RaiseError(t, "Bytes function not supported") + self.RaiseError(t, "Byte strings and Bytes function not supported") def _Str(self, tree): - self.write(repr(tree.s)) + # force writing in double quotes + self.write(f'"{tree.s}"') def _JoinedStr(self, t): self.RaiseError(t, "Joined strings not supported") @@ -922,7 +931,13 @@ def _Name(self, t): self.write(t.id) def _NameConstant(self, t): - self.RaiseError(t, "NameConstant depreciated and not supported") + # Required only for Python 3.7 + if t.value == None: + self.write(0) + elif t.value: + self.write("true") + else: + self.write("false") def _Repr(self, t): self.RaiseError(t, "Repr not supported") @@ -944,7 +959,7 @@ def _Constant(self, t): elif isinstance(value, str): self.write(f'"{value}"') elif isinstance(value, (bytes, bytearray)): # reject bytes strings e.g. b'123' - self.RaiseError(t, "Byte strings not supported") + self.RaiseError(t, "Byte strings and Bytes function not supported") elif isinstance(value, bool): if value: self.write("true") diff --git a/tests/swig/python/codegen/test_codegen.py b/tests/swig/python/codegen/test_codegen.py index 275fa238a..b69a07cd2 100644 --- a/tests/swig/python/codegen/test_codegen.py +++ b/tests/swig/python/codegen/test_codegen.py @@ -534,6 +534,7 @@ def _checkException(self, source, exception_str): # code generate code = pyflamegpu.codegen.codegen(tree) if EXCEPTION_MSG_CHECKING: + print(str(e.value)) assert exception_str in str(e.value) @@ -613,7 +614,7 @@ def test_exceptions(self): self._checkException(py_try, "Exceptions not supported") def test_bytes(self): - self._checkException("b'123'", "Byte strings not supported") + self._checkException("b'123'", "Byte strings and Bytes function not supported") def test_strings(self): self._checkException('f"{value}"', "not supported") From 1213c095d79251ca1dbf6c8dbc1f880d60c502c7 Mon Sep 17 00:00:00 2001 From: Mondus Date: Thu, 1 Sep 2022 16:17:39 +0100 Subject: [PATCH 2/2] removed left over print statement --- tests/swig/python/codegen/test_codegen.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/swig/python/codegen/test_codegen.py b/tests/swig/python/codegen/test_codegen.py index b69a07cd2..fad2bd482 100644 --- a/tests/swig/python/codegen/test_codegen.py +++ b/tests/swig/python/codegen/test_codegen.py @@ -534,7 +534,6 @@ def _checkException(self, source, exception_str): # code generate code = pyflamegpu.codegen.codegen(tree) if EXCEPTION_MSG_CHECKING: - print(str(e.value)) assert exception_str in str(e.value)