|
|
|
|
@ -294,11 +294,21 @@ class NameVisitor(gast.NodeVisitor):
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _is_ancestor_node(self, ancestor_node, node):
|
|
|
|
|
parent_node = self._get_parent_node(node)
|
|
|
|
|
|
|
|
|
|
while parent_node is not None:
|
|
|
|
|
if parent_node == ancestor_node:
|
|
|
|
|
return True
|
|
|
|
|
parent_node = self._get_parent_node(parent_node)
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _get_parent_node(self, node):
|
|
|
|
|
wrapper_node = self.node_to_wrapper_map.get(node)
|
|
|
|
|
if wrapper_node:
|
|
|
|
|
parent_node = wrapper_node.parent.node
|
|
|
|
|
return parent_node
|
|
|
|
|
if wrapper_node.parent:
|
|
|
|
|
parent_node = wrapper_node.parent.node
|
|
|
|
|
return parent_node
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def _remove_unnecessary_vars(self, loop_vars, loop_node):
|
|
|
|
|
@ -355,9 +365,22 @@ class NameVisitor(gast.NodeVisitor):
|
|
|
|
|
if child_node.id in target_var_names:
|
|
|
|
|
vars_of_list_generator.add(child_node)
|
|
|
|
|
|
|
|
|
|
# 2. Get target vars or vars from target vars used in for-loop.
|
|
|
|
|
elif isinstance(parent_node,
|
|
|
|
|
gast.For) and parent_node is not loop_node:
|
|
|
|
|
# 2. Get target vars or vars from target vars used in for-loop but the for-loop is
|
|
|
|
|
# 1) not the "loop_node" itself
|
|
|
|
|
# 2) not the ancestor of the "loop_node"
|
|
|
|
|
#
|
|
|
|
|
# For examples:
|
|
|
|
|
# for k in range(x): # if it's this "loop_node", i or j both should be target vars.
|
|
|
|
|
# # do something
|
|
|
|
|
#
|
|
|
|
|
# for i in range(a): # if it's this "loop_node", k or j should be in target vars but i should not.
|
|
|
|
|
# for j in range(a): # if it's this "loop_node", k should be in target_vars but i or j should not.
|
|
|
|
|
# x = i+j
|
|
|
|
|
elif isinstance(parent_node, gast.For):
|
|
|
|
|
if parent_node is loop_node:
|
|
|
|
|
continue
|
|
|
|
|
if self._is_ancestor_node(parent_node, loop_node):
|
|
|
|
|
continue
|
|
|
|
|
# 2.1 target vars in gast.For node.
|
|
|
|
|
target_node = parent_node.target
|
|
|
|
|
if isinstance(target_node, gast.Tuple):
|
|
|
|
|
|