Skip to content

Commit

Permalink
Python 3.7. support for code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mondus committed Sep 1, 2022
1 parent a684b9b commit 341f4c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
33 changes: 24 additions & 9 deletions swig/python/codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ">"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion tests/swig/python/codegen/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 341f4c3

Please sign in to comment.