Inherits from
- Transformer: compiler.transformer.Transformer
Method summary
- import_from(self, nodelist)
Methods
- import_from(self, nodelist)
Translate 'from ... import *' statements into 'import ...'.
We avoid 'from ... import *' because they only seem to work on built-in dicts, whereas we run code over user dicts. Our solution is to translate them into normal import statements:
from foo import * ~> import foo as __module for name in dir(__module): exec '%s = __module.%s' % (name, name)This translation is equivalent except it leaves the name '__module' in the namespace.
This implementation assumes that the compiler equates nested 'Stmt' nodes with flat ones, e.g.:
>>> from compiler.ast import Assign, AssName, Const, Stmt >>> from enthought.blocks.compiler_.api\ ... import compile_ast >>> >>> def ass(**kw): ... [(name, n)] = kw.items() ... return Assign([AssName(name, 'OP_ASSIGN')], Const(n)) >>> >>> s1 = Stmt([ass(a=1), ass(b=2), ass(c=3)]) >>> s2 = Stmt([ass(a=1), Stmt([ass(b=2)]), ass(c=3)]) >>> >>> assert compile_ast(Module(None, s1)) == \ ... compile_ast(Module(None, s2))