def Call_new(t, x):
"""Translate ``Foo(...)`` to ``new Foo(...)`` if function name starts
with a capital letter.
"""
def getNameString(x):
if isinstance(x, ast.Name):
return x.id
elif isinstance(x, ast.Attribute):
return str(x.attr)
elif isinstance(x, ast.Subscript):
if isinstance(x.slice, ast.Index):
return str(x.slice.value)
NAME_STRING = getNameString(x.func)
if (NAME_STRING and re.search(r'^[A-Z]', NAME_STRING)):
# TODO: generalize args mangling and apply here
# assert not any([x.keywords, x.starargs, x.kwargs])
subj = x
elif isinstance(x.func, ast.Name) and x.func.id == 'new':
subj = x.args[0]
else:
subj = None
if subj:
return Call_default(t, subj, operator='new ')
评论列表
文章目录