def visitImportFrom(self, node): modname = jast.StringConstant(node.module) module = self.get_module(node.module.split('.'), 0) if len(node.names) == 0: self.addModule(node.module, '*') self._loadNames(self._getnames(module), module) return jast.InvokeStatic("org.python.core.imp", "importAll", [modname, self.frame.frame]) topmodname = jast.StringConstant(node.module) modnames = [alias.name for alias in node.names] modnamesArray = jast.FilledArray( "String", map(lambda x: jast.StringConstant(x), modnames)) do_import = jast.InvokeStatic("org.python.core.imp", "importFrom", [topmodname, modnamesArray, self.frame.frame]) imp_accu = self.frame.gettemp('PyObject[]') stmts = [ jast.Set(imp_accu, do_import) ] for i in range(len(node.names)): modname = node.names[i].name asname = node.names[i].asname if asname is None: asname = modname code = jast.Subscript(imp_accu, i) stmts.append(self.set_name(asname, module.getattr(modname).makeReference(code))) stmts.append(jast.Set(imp_accu,jast.Null)) self.frame.freetemp(imp_accu) return stmts
def seqSet(self, elts): n = len(elts) unpacked = jast.InvokeStatic("org.python.core.Py", "unpackSequence", [self.temporary.asAny(), jast.IntegerConstant(n)]) tmp = self.frame.gettemp('PyObject[]') stmts = [ jast.Set(tmp, unpacked) ] for i in range(n): code = jast.Subscript(tmp, i) stmts.append(self.set(elts[i], self.factory.makePyObject(code))) self.frame.freetemp(tmp) return stmts
def set_list(self, seq, value): if hasattr(self, 'AUG'): raise SyntaxError, "augmented assign to tuple not possible" if len(seq) > 0 and seq[-1].id == JJTCOMMA: del seq[-1] n = len(seq) unpacked = jast.InvokeStatic( "org.python.core.Py", "unpackSequence", [value.asAny(), jast.IntegerConstant(n)]) tmp = self.frame.gettemp('PyObject[]') stmts = [jast.Set(tmp, unpacked)] for i in range(n): code = jast.Subscript(tmp, i) stmts.append(self.set(seq[i], self.factory.makePyObject(code))) self.frame.freetemp(tmp) return stmts
def importfrom_stmt(self, top, names): module = self.get_module(top, 0) if names == '*': return self.importall_stmt(module) #print 'import * from', module #names = module.dir() modnames = [] asnames = [] for modname, asname in names: if asname is None: asname = modname asnames.append(asname) modnames.append(modname) topmodname = jast.StringConstant(".".join(top)) modnamesArray = jast.FilledArray( "String", map(lambda x: jast.StringConstant(x), modnames)) do_import = jast.InvokeStatic( "org.python.core.imp", "importFrom", [topmodname, modnamesArray, self.frame.frame]) if not self.imp_accu: imp_accu = self.imp_accu = jast.Identifier("imp_accu") self.makeFreeDecl("PyObject[]", imp_accu) else: imp_accu = self.imp_accu stmts = [jast.Set(imp_accu, do_import)] for i in range(len(asnames)): asname = asnames[i] modname = modnames[i] code = jast.Subscript(imp_accu, i) stmts.append( self.set_name(asname, module.getattr(modname).makeReference(code))) stmts.append(jast.Set(imp_accu, jast.Null)) return stmts