2.1 --- a/simplify.py Sun Jul 09 13:45:27 2006 +0200
2.2 +++ b/simplify.py Sun Jul 09 14:15:59 2006 +0200
2.3 @@ -31,15 +31,16 @@
2.4 A simplifying visitor for AST nodes.
2.5
2.6 Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
2.7 - CallFunc, Class, Const, Continue, Dict, Discard, For, Function,
2.8 - Getattr, Global, If, Keyword, Lambda, List, Module, Name, Pass,
2.9 - Return, Stmt, TryExcept, TryFinally, Tuple, While.
2.10 + CallFunc, Class, Const, Continue, Dict, Discard, For, From,
2.11 + Function, Getattr, Global, If, Import, Keyword, Lambda, List,
2.12 + Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple,
2.13 + While.
2.14
2.15 Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare,
2.16 - Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import, Invert,
2.17 - LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or,
2.18 - Power, Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub,
2.19 - Subscript, UnaryAdd, UnarySub, Yield.
2.20 + Decorators, Div, Ellipsis, Exec, FloorDiv, Invert, LeftShift,
2.21 + ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, Print,
2.22 + Printnl, Raise, RightShift, Slice, Sliceobj, Sub, Subscript,
2.23 + UnaryAdd, UnarySub, Yield.
2.24 """
2.25
2.26 def __init__(self):
2.27 @@ -99,6 +100,25 @@
2.28 result = Global(global_, names=global_.names)
2.29 return result
2.30
2.31 + def visitImport(self, import_):
2.32 + result = Assign(import_)
2.33 + code = []
2.34 + for path, alias in import_.names:
2.35 + importer = Import(name=path)
2.36 + top = alias or path.split(".")[0]
2.37 + code.append(StoreName(expr=importer, name=top))
2.38 + result.code = code
2.39 + return result
2.40 +
2.41 + def visitFrom(self, from_):
2.42 + result = Assign(from_)
2.43 + code = []
2.44 + code.append(StoreTemp(expr=Import(name=from_.modname)))
2.45 + for name, alias in from_.names:
2.46 + code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name)))
2.47 + result.code = code
2.48 + return result
2.49 +
2.50 def visitName(self, name):
2.51 result = LoadName(name, name=name.name)
2.52 return result