Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.7. support for code generator #917

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion tests/swig/python/codegen/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,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