Skip to content

Commit

Permalink
[Misc] Add experimental Python 3.9 support (#2157)
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate authored Jan 17, 2021
1 parent c728d38 commit 6c1c3c8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/persubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 23 additions & 5 deletions python/taichi/lang/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
setuptools
astor
pybind11
pylint
sourceinspect
pytest
pytest-rerunfailures
pytest-xdist
yapf
numpy
GitPython
coverage
colorama
autograd

0 comments on commit 6c1c3c8

Please sign in to comment.