def save(self, reimport=True): with open(self._filename, 'w') as outf: Unparser(self._ast, outf) if reimport: importlib.invalidate_caches() path, filename = os.path.split(self._filename) modname = filename[:-3] if modname in sys.modules: del sys.modules[modname] self._module = importlib.import_module(filename[:-3]) self._need_save = False
def compile(func, argtypes): from astunparse import Unparser from cStringIO import StringIO env = Env() source = get_func_source(func) argnames = get_args(func) print argnames print source args = { arg_name: arg_type for arg_name, arg_type in zip(argnames, argtypes) } node = ast.parse(source) env.node = node #InsertPass(env).run() #InsertReturn(env).run() ControlFlowAnalysis(env).run() ClearUnreachedNode(env).run() print env.cf.path TypeInfer(env, args, func.func_globals).run() print env.cf.blocks InsertCoerceNode(env).run() #CoerceReturn(env).run() print ast.dump(node) RewriteName(env).run() RewriteSubscript(env).run() InsertArrayInfo(env).run() InsertDefination(env).run() buf = StringIO() Unparser(node, buf) print buf.getvalue()
def save(self): with open(self._filename, 'w') as outf: Unparser(self._ast, outf)
def show_ast(node): with StringIO() as fh: Unparser(node, file=fh) return fh.getvalue()
def unparse(tree): out = StringIO() Unparser(tree, file=out) return out.getvalue()