From f3a1926234410ffa028118ee19f3556b6f24fc90 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Thu, 14 Jan 2021 22:33:02 +0800 Subject: [PATCH 1/2] [Misc] Add Python 3.9 support --- .github/workflows/persubmit.yml | 4 ++-- python/taichi/lang/transformer.py | 28 +++++++++++++++++++++++----- requirements.txt | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 requirements.txt diff --git a/.github/workflows/persubmit.yml b/.github/workflows/persubmit.yml index f598a7a2cf7b6..78606899cc57f 100644 --- a/.github/workflows/persubmit.yml +++ b/.github/workflows/persubmit.yml @@ -16,8 +16,8 @@ jobs: - os: macos-latest python: 3.7 with_cc: OFF - - os: ubuntu-16.04 # TODO(archibate): windows-latest - python: 3.8 + - os: ubuntu-latest + python: 3.9 with_cc: OFF - os: ubuntu-latest python: 3.8 diff --git a/python/taichi/lang/transformer.py b/python/taichi/lang/transformer.py index e7cc3ccc245b4..2297ea91e5b15 100644 --- a/python/taichi/lang/transformer.py +++ b/python/taichi/lang/transformer.py @@ -220,7 +220,8 @@ def visit_Assign(self, node): def tuple_indexed(i): indexing = self.parse_stmt('__tmp_tuple[0]') - indexing.value.slice.value = self.parse_expr("{}".format(i)) + self.set_subscript_index(indexing.value, + self.parse_expr("{}".format(i))) return indexing.value for i, target in enumerate(targets): @@ -565,15 +566,32 @@ def visit_For(self, node): else: # Struct for return self.visit_struct_for(node, is_grouped=False) + @staticmethod + def get_subscript_index(node): + assert isinstance(node, ast.Subscript), type(node) + # ast.Index has been deprecated in Python 3.9, + # use the index value directly instead :) + if isinstance(node.slice, ast.Index): + return node.slice.value + return node.slice + + @staticmethod + def set_subscript_index(node, value): + assert isinstance(node, ast.Subscript), type(node) + if isinstance(node.slice, ast.Index): + node.slice.value = value + else: + node.slice = value + def visit_Subscript(self, node): self.generic_visit(node) value = node.value - indices = node.slice - if isinstance(indices.value, ast.Tuple): - indices = indices.value.elts + indices = self.get_subscript_index(node) + if isinstance(indices, ast.Tuple): + indices = indices.elts else: - indices = [indices.value] + indices = [indices] call = ast.Call(func=self.parse_expr('ti.subscript'), args=[value] + indices, diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000..59b7fd8c6524c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,14 @@ +setuptools +astor +pybind11 +pylint +sourceinspect +pytest +pytest-rerunfailures +pytest-xdist +yapf +numpy +GitPython +coverage +colorama +autograd From 424fe775fb0180bdcfd6b60e999a41ebdd4d129c Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Thu, 14 Jan 2021 09:42:27 -0500 Subject: [PATCH 2/2] [skip ci] enforce code format --- python/taichi/lang/transformer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/lang/transformer.py b/python/taichi/lang/transformer.py index 2297ea91e5b15..8d27e5cc52963 100644 --- a/python/taichi/lang/transformer.py +++ b/python/taichi/lang/transformer.py @@ -221,7 +221,7 @@ def visit_Assign(self, node): def tuple_indexed(i): indexing = self.parse_stmt('__tmp_tuple[0]') self.set_subscript_index(indexing.value, - self.parse_expr("{}".format(i))) + self.parse_expr("{}".format(i))) return indexing.value for i, target in enumerate(targets):