def individualizeVariables(a, variablePairs, idNum, imports):
"""Replace variable names with new individualized ones (for inlining methods)"""
if not isinstance(a, ast.AST):
return
if type(a) == ast.Name:
if a.id not in variablePairs and not (builtInName(a.id) or importedName(a.id, imports)):
name = "_var_" + a.id + "_" + str(idNum[0])
variablePairs[a.id] = name
if a.id in variablePairs:
a.id = variablePairs[a.id]
# Override built-in names when they're assigned to.
elif type(a) == ast.Assign and type(a.targets[0]) == ast.Name:
if a.targets[0].id not in variablePairs:
name = "_var_" + a.targets[0].id + "_" + str(idNum[0])
variablePairs[a.targets[0].id] = name
elif type(a) == ast.arguments:
for arg in a.args:
if type(arg) == ast.arg:
name = "_arg_" + arg.arg + "_" + str(idNum[0])
variablePairs[arg.arg] = name
arg.arg = variablePairs[arg.arg]
return
elif type(a) == ast.Call:
if type(a.func) == ast.Name:
variablePairs[a.func.id] = a.func.id # save the function name!
for child in ast.iter_child_nodes(a):
individualizeVariables(child, variablePairs, idNum, imports)
评论列表
文章目录