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 callConstructor(self, access, sig, throws=[], dosuper=1): args = [] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if dosuper: supercall = jast.InvokeLocal("super", argids) else: supercall = jast.InvokeLocal("super", []) ## for saccess, ssig in self.jconstructors: ## if len(ssig) == len(sig): ## supercall = jast.InvokeLocal("super", argids) ## break ## else: ## supercall = jast.InvokeLocal("super", []) frozen = self.module.getFrozen() initargs = [objects] initproxy = jast.InvokeLocal("__initProxy__", initargs) code = jast.Block([supercall, initproxy]) self.statements.append( jast.Constructor(self.name, jast.Modifier.ModifierString(access), args, code, throws))
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
def PyObjectArray(args): aargs = makeAnys(args) return jast.FilledArray("PyObject", aargs)
def callMethod(self, name, access, ret, sig, throws=[], dosuper=1): args = [typeName(ret)] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if isinstance(access, IntType) and isAbstract(access): dosuper = 0 access = access & ~ABSTRACT if not dosuper and not self.isAdapter: getattr = jast.InvokeStatic("Py", "jgetattr", [this, jast.StringConstant(name)]) else: getattr = jast.InvokeStatic("Py", "jfindattr", [this, jast.StringConstant(name)]) inst = jast.Identifier("inst") if len(throws) == 0: jcall = "_jcall" else: jcall = "_jcallexc" jcall = makeReturn(jast.Invoke(inst, jcall, [objects]), ret) jcall = wrapThrows(jcall, throws, ret) if dosuper: supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = None if not self.issuperproxy and name not in self.getCandSupermethods( incl=0): supermethod = jast.Method("super__" + name, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.supermethods["super__" + name] = "super__" + name else: if self.isAdapter: supercall = nullReturn(ret) else: supercall = None supermethod = None if not dosuper and not self.isAdapter: test = jcall else: test = jast.If(jast.Operation("!=", inst, jast.Null), jcall, supercall) code = jast.Block([jast.Declare("PyObject", inst, getattr), test]) meth = jast.Method(name, jast.Modifier.ModifierString(access), args, code, throws) if supermethod is not None: self.statements.append(supermethod) self.statements.append(meth)
def PyObjectArray(args): aargs = makeAnys(args) return Object( jast.FilledArray("PyObject", aargs), findType(java.lang.Class.forName("[Lorg.python.core.PyObject;")))