import ast import compiler import os def process(node): moduleName = node.modname moduleSourceFileName = "t/" + moduleName + ".py" modulePirFileName = "t/" + moduleName + ".pir" moduleInitSubName = "_" + moduleName + "__init__main" #compile if needed command = "./pyparrot %s" % ( moduleSourceFileName ) os.system( command ) #load generated bytecode ast.outputInBlock( "load_bytecode \"%s\"" % ( modulePirFileName ) ) #debug ast.output("") ast.output( "##### FROM " + moduleSourceFileName + " #####" ) #call the _MODULENAME__init__main function tempReg = ast.nextPRegister() ast.outputInBlock("find_global %s, \"%s\", \"%s\"" % ( tempReg, moduleName, moduleInitSubName ) ) ast.outputInBlock("%s()" % ( tempReg ) ) #debug ast.output( "##### " + moduleSourceFileName + " #####" ) #import the appropriate variables into the current module namespace or call the import * function fromTempRegister = ast.nextPRegister() for x in node.names: importName = x[0]; if importName == "*": moduleStarImportFunction = "_" + moduleName + "_star_import" ast.outputInBlock( "find_global %s, \"%s\", \"%s\"" % ( fromTempRegister, moduleName, moduleStarImportFunction ) ) ast.outputInBlock( "%s()" % ( fromTempRegister ) ) else: ast.outputInBlock( "find_lex %s, \"%s\"" % ( fromTempRegister, importName ) ) ast.outputInBlock( "store_lex -2, \"%s\", %s" % ( importName, fromTempRegister ) )