我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用ast.expr_context()。
def visit_child_nodes(self, node, visit_func=None, seen_ids=None): if visit_func is None: visit_func = self.visit for field, value in ast.iter_fields(node): if isinstance(value, list): for i in range(len(value)): item = value[i] if isinstance(item, ast.AST): if self.make_unique: if id(item) in seen_ids: if hasattr(item, 'parent'): item.parent = None if hasattr(item, 'parent_functiondef'): item.parent_functiondef = None if hasattr(item, 'parent_functionname'): item.parent_functionname = None item = copy.deepcopy(item) value[i] = item setattr(node, field, value) seen_ids.add(id(item)) visit_func(item) # elif isinstance(value, ast.expr_context): # print('found context', value) # if self.make_unique: # print('cloning context', value) # value = copy.deepcopy(value) # setattr(node, field, value) # visit_func(value) elif isinstance(value, ast.AST): if self.make_unique: if id(value) in seen_ids: if hasattr(value, 'parent'): value.parent = None if hasattr(value, 'parent_functiondef'): value.parent_functiondef = None if hasattr(value, 'parent_functionname'): value.parent_functionname = None value = copy.deepcopy(value) seen_ids.add(id(value)) setattr(node, field, value) else: seen_ids.add(id(value)) visit_func(value)