def print_line_to(self, file, code=None): if code is None: return jast.InvokeStatic("Py", "printlnv", [self.asAny(file)]) else: return jast.InvokeStatic( "Py", "println", [self.asAny(file), self.asAny(code)])
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 makeReturn(code, ret): if ret == Void.TYPE: return code if Class.isPrimitive(ret): r = jast.InvokeStatic("Py", "py2" + ret.__name__, [code]) else: typname = typeName(ret) r = jast.InvokeStatic( "Py", "tojava", [code, jast.GetStaticAttribute(typname, 'class')]) r = jast.Cast(typname, r) return jast.Return(r)
def import_stmt(self, names): ret = [] for dotted, asname in names: modnameConst = jast.StringConstant(".".join(dotted)) if asname: code = jast.InvokeStatic("org.python.core.imp", "importOneAs", [modnameConst, self.frame.frame]) code = self.get_module(dotted, 0).makeReference(code) ret.append(self.set_name(asname, code)) else: code = jast.InvokeStatic("org.python.core.imp", "importOne", [modnameConst, self.frame.frame]) code = self.get_module(dotted, 1).makeReference(code) ret.append(self.set_name(dotted[0], code)) return ret
def asa(self, code, type, message=None): ret = self.isa(code, type) if ret is not None: return ret if primitives.has_key(type): return jast.InvokeStatic('Py', primitives[type], [code]) if type == java.lang.Boolean.TYPE: return jast.Invoke(code, '__nonzero__', []) tname = type.__name__ tojava = jast.InvokeStatic( 'Py', 'tojava', [code, jast.GetStaticAttribute(tname, 'class')]) return jast.Cast(tname, tojava)
def visitImport(self, node): ret = [] for alias in node.names: modnameConst = jast.StringConstant(alias.name) dotted = alias.name.split('.') if alias.asname: code = jast.InvokeStatic("org.python.core.imp","importOneAs", [modnameConst, self.frame.frame]) code = self.get_module(dotted, 0).makeReference(code) ret.append(self.set_name(alias.asname, code)) else: code = jast.InvokeStatic("org.python.core.imp","importOne", [modnameConst, self.frame.frame]) code = self.get_module(dotted, 1).makeReference(code) ret.append(self.set_name(dotted[0], code)) return ret
def importall_stmt(self, module): modname = jast.StringConstant(module.value.name) self.addModule(module.value.name, '*') #print 'import *', module.value.name self._loadNames(self._getnames(module), module) return jast.InvokeStatic("org.python.core.imp", "importAll", [modname, self.frame.frame])
def visitRaise(self, node): if node.type is None: return jast.Throw(jast.InvokeStatic("Py", "makeException", [])) type = self.visit(node.type) inst = self.eval(node.inst, None) tback = self.eval(node.tback, None) return type.doraise(inst, tback)
def dumpCodes(self): self.constants.append([ "PyFunctionTable", self.getFunctionTable(), jast.New(self.name, []) ]) for label, name, code, frame in self.codes: code = frame.toCellPrepend(code) funcid = self.addFunctionCode(name, code) arglist = keyworddict = jast. False if frame.args_arglist(): arglist = jast. True if frame.args_keyworddict(): keyworddict = jast. True names = jast.StringArray(frame.getnames()) cellnames = StringArrayOrNull(frame.getcellnames()) freenames = StringArrayOrNull(frame.getfreenames()) npurecell = frame.get_npurecell() cargs = [ jast.IntegerConstant(frame.args_count()), names, jast.StringConstant(self.filename), jast.StringConstant(name), arglist, keyworddict, self.getFunctionTable(), jast.IntegerConstant(funcid), cellnames, freenames, jast.IntegerConstant(npurecell), jast.IntegerConstant((frame.opt_globals and CO_OPTIMIZED) | ( frame.scope.nested_scopes and CO_NESTED)) ] newcode = jast.InvokeStatic("Py", "newCode", cargs) self.constants.append(("PyCode", jast.Identifier(label), newcode))
def dumpFuncs(self): meths = [] cases = [] args = ["PyObject", ("PyFrame", "frame")] access = "private static" callargs = [jast.Identifier("frame")] for name, funcid, code in self.funccodes: funcname = self.uniquename(name) meths.append(jast.Method(funcname, access, args, clean(code))) body = jast.Return(jast.InvokeStatic(self.name, funcname, callargs)) cases.append( [jast.IntegerConstant(funcid), jast.FreeBlock([body])]) defaultCase = jast.FreeBlock([jast.Return(jast.Null)]) switch = jast.Block( [jast.Switch(jast.Identifier('index'), cases, defaultCase)]) meths.insert( 0, jast.Method("call_function", "public", ["PyObject", ("int", "index"), ("PyFrame", "frame")], switch)) self.superclass = "PyFunctionTable" return meths
def doraise(self, code, exc_value=None, exc_traceback=None): args = [code] if exc_value is not None: args.append(exc_value.asAny()) if exc_traceback is not None: args.append(exc_traceback.asAny()) return jast.Throw(jast.InvokeStatic("Py", "makeException", args))
def mkcall(self, callee, args): if self.isConstructor: op = jast.New(self.name, args) elif self.isStatic: op = jast.InvokeStatic(callee, self.name, args) else: #print callee, self.name, args op = jast.Invoke(callee, self.name, args) return op, self.retType
def visitAssert(self, node): if self.isAlwaysFalse("__debug__"): return jast.SimpleComment("assert") args = [self.visit(node.test).asAny()] if node.msg is not None: args.append(self.visit(node.msg).asAny()) return jast.If(self.frame.getglobal("__debug__").nonzero(), jast.InvokeStatic("Py", "assert_", args))
def visitPrint(self, node): self.setline(node) dest = self.factory.makePyNone() if node.dest: dest = self.visit(node.dest) stmts = [] if node.values: for i, v in zip(range(len(node.values)), node.values): v = self.visit(v) if node.nl and i == len(node.values) - 1: stmts.append(jast.InvokeStatic("Py", "println", [ dest.asAny(), v.asAny()])) else: stmts.append(jast.InvokeStatic("Py", "printComma", [ dest.asAny(), v.asAny()])) else: stmts.append(jast.InvokeStatic("Py", "printlnv", [dest.asAny()])) return stmts;
def assert_stmt(self, test, message=None): if self.isAlwaysFalse("__debug__"): return jast.SimpleComment("assert") args = [test.asAny()] if message is not None: args.append(message.asAny()) return jast.If( self.frame.getglobal("__debug__").nonzero(), jast.InvokeStatic("Py", "assert", args))
def dumpMain(self): meths = [] if self.javamain: code = [] newargs = jast.Identifier("newargs") code.append( jast.Declare("String[]", newargs, jast.NewArray("String", ["args.length+1"]))) code.append( jast.Set(jast.Identifier("newargs[0]"), jast.StringConstant(self.name))) args = [ jast.Identifier('args'), jast.IntegerConstant(0), jast.Identifier('newargs'), jast.IntegerConstant(1), jast.Identifier('args.length') ] code.append( jast.InvokeStatic("java.lang.System", "arraycopy", args)) args = [ jast.GetStaticAttribute( self.getclassname(self.name + '.' + self.pyinner.name), "class"), jast.Identifier('newargs'), self.getPackages(qual=1), self.getMainProperties(qual=1), self.getFrozen(), jast.StringArray(self.modules.keys()) ] code.append([jast.InvokeStatic("Py", "runMain", args)]) maincode = jast.Block(code) meths.append( jast.Method("main", "public static", ["void", ("String[]", "args")], maincode, ["java.lang.Exception"])) return meths
def makeObject(code, c): if c in [Integer.TYPE, Byte.TYPE, Short.TYPE, Long.TYPE]: mname = "newInteger" elif c in [Character.TYPE]: mname = "newString" elif c in [Float.TYPE, Double.TYPE]: mname = "newFloat" elif c in [Boolean.TYPE]: mname = "newBoolean" else: return code return jast.InvokeStatic("Py", mname, [code])
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 exec_stmt(self, code, globs=None, locs=None): if globs is None: globCode = jast.Null else: globCode = globs.asAny() if locs is None: locCode = jast.Null else: locCode = locs.asAny() return jast.InvokeStatic("Py", "exec", [code.asAny(), globCode, locCode])
def addClassDictInit(self): self.interfaces.append(org.python.core.ClassDictInit) namelist = jast.InvokeStatic( "Py", "java2py", [jast.StringArray(self.supermethods.keys())]) code = jast.Invoke(jast.Identifier("dict"), "__setitem__", [jast.StringConstant("__supernames__"), namelist]) code = jast.Block([code]) self.statements.append( jast.Method("classDictInit", "static public", ["void", ("PyObject", "dict")], code))
def visitExec(self, node): code = self.visit(node.body).asAny() if node.globals is None: globCode = jast.Null else: globCode = self.visit(node.globals).asAny() if node.locals is None: locCode = jast.Null else: locCode = self.visit(node.locals).asAny() return jast.InvokeStatic("Py", "exec", [code, globCode, locCode])
def getNew(self): args = [jast.StringConstant(self.name), PyObjectArray(self.bases), self.pycode, jast.Null] if self.isSuperclassJava(): args.append(jast.Identifier("%s.class" % self.proxyname)) clos = self.def_compiler.frame.makeClosure(self.scope) if clos: args.append(PyObjectArray(clos)) return jast.InvokeStatic("Py", "makeClass", args)
def dumpMain(self): if not hasattr(self, 'mainCode'): return [] meths = [] self.interfaces.append("PyRunnable") getmain = jast.Block([ jast.If(jast.Operation("==", self.mainCode, jast.Null), jast.InvokeStatic(self.name, "initConstants", [])), jast.Return(self.mainCode) ]) meths.append(jast.Method("getMain", "public", ["PyCode"], getmain)) return meths
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 dumpInitModule(self): meths = [] dict = jast.Identifier("dict") sargs = [ jast.StringConstant("__name__"), jast.New("PyString", [jast.StringConstant(self.name)]) ] rargs = [ jast.Invoke(jast.New("_PyInner", []), "getMain", []), dict, dict ] code = jast.Block([ jast.Invoke(dict, "__setitem__", sargs), jast.InvokeStatic("Py", "runCode", rargs) ]) meths.append( jast.Method("moduleDictInit", "public static", ["void", ("PyObject", "dict")], code)) return meths
def addPyProxyInterface(self): self.statements.append( jast.Declare('private PyInstance', jast.Identifier('__proxy'))) code = jast.Set(jast.Identifier("__proxy"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPyInstance", "public", ["void", ("PyInstance", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__proxy"))]) self.statements.append( jast.Method("_getPyInstance", "public", ["PyInstance"], code)) self.statements.append( jast.Declare('private PySystemState', jast.Identifier('__sysstate'))) code = jast.Set(jast.Identifier("__sysstate"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPySystemState", "public", ["void", ("PySystemState", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__sysstate"))]) self.statements.append( jast.Method("_getPySystemState", "public", ["PySystemState"], code)) frozen = self.module.getFrozen() this = jast.Identifier("this") initargs = [ this, jast.StringConstant(self.modname), jast.StringConstant(self.name), jast.Identifier("args"), self.packages, self.properties, frozen, jast.StringArray(self.modules) ] initproxy = jast.InvokeStatic("Py", "initProxy", initargs) code = jast.Block([initproxy]) self.statements.append( jast.Method("__initProxy__", "public", ["void", ("Object[]", "args")], code)) self.interfaces.append(org.python.core.PyProxy)
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 getNew(self): return jast.InvokeStatic("imp", "load", [jast.StringConstant(self.name)])
def getStringConstant(self, value): code = jast.InvokeStatic("Py", "newString", [jast.StringConstant(value)]) return self.getConstant(value, code, "s")
def getFloatConstant(self, value): code = jast.InvokeStatic("Py", "newFloat", [jast.FloatConstant(value)]) return self.getConstant(value, code, "f")