📄 py.java
字号:
dict.__setitem__("__doc__", doc); for (int i=0; i<bases.length; i++) { if (!(bases[i] instanceof PyClass)) { PyObject c = bases[i].__class__; // Only try the meta-class trick on __class__'s that are // PyInstance's. This will improve error messages for // casual mistakes while not really reducing the power of // this approach (I think) if (c instanceof PyJavaClass) { throw Py.TypeError("base is not a class object: "+ bases[i].safeRepr()); } return c.__call__(new PyString(name), new PyTuple(bases), dict); } else if (bases[i] instanceof PyMetaClass) { // experimental PyMetaClass hook try { java.lang.reflect.Constructor ctor = bases[i].getClass(). getConstructor(pyClassCtrSignature); return (PyObject) ctor.newInstance(new Object[] { name, new PyTuple(bases), dict, proxyClass }); } catch(Exception e) { throw Py.TypeError("meta-class fails to supply proper " + "ctr: " + bases[i].safeRepr()); } } } return new PyClass(name, new PyTuple(bases), dict, proxyClass); } private static int nameindex=0; public static synchronized String getName() { String name = "org.python.pycode._pyx"+nameindex; nameindex += 1; return name; } public static CompilerFlags getCompilerFlags() { return getCompilerFlags(0,false); } public static CompilerFlags getCompilerFlags(int flags,boolean dont_inherit) { CompilerFlags cflags = null; if (dont_inherit) { cflags = new CompilerFlags(flags); } else { PyFrame frame = Py.getFrame(); if (frame!=null && frame.f_code != null) { cflags = new CompilerFlags(frame.f_code.co_flags|flags); } } return cflags; } // w/o compiler-flags public static PyCode compile(modType node, String filename) { return compile(node, getName(), filename); } public static PyCode compile(modType node, String name, String filename) { return compile(node, name, filename, true, false); } public static PyCode compile(modType node, String name, String filename, boolean linenumbers, boolean printResults) { return compile_flags(node, name, filename, linenumbers, printResults, null); } public static PyCode compile(InputStream istream, String filename, String type) { return compile_flags(istream,filename,type,null); } // with compiler-flags public static PyCode compile_flags(modType node, String name, String filename, boolean linenumbers, boolean printResults,CompilerFlags cflags) { try { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); org.python.compiler.Module.compile(node, ostream, name, filename, linenumbers, printResults, false,cflags); saveClassFile(name, ostream); return BytecodeLoader.makeCode(name, ostream.toByteArray()); } catch (Throwable t) { throw parser.fixParseError(null, t, filename); } } public static PyCode compile_flags(InputStream istream, String filename, String type,CompilerFlags cflags) { modType node = parser.parse(istream, type, filename, cflags); boolean printResults = false; if (type.equals("single")) printResults = true; return Py.compile_flags(node, getName(), filename, true, printResults, cflags); } public static PyCode compile_flags(String data, String filename, String type,CompilerFlags cflags) { return Py.compile_flags( new java.io.StringBufferInputStream(data+"\n\n"), filename, type,cflags); } public static PyObject compile_command_flags(String string, String filename, String kind, CompilerFlags cflags,boolean stdprompt) { org.python.parser.ast.modType node = parser.partialParse(string+"\n", kind, filename, cflags, stdprompt); if (node == null) return Py.None; return Py.compile_flags(node, Py.getName(), filename, true, true, cflags); } public static PyObject[] unpackSequence(PyObject o, int length) { if (o instanceof PyTuple) { PyTuple tup = (PyTuple)o; //System.err.println("unpack tuple"); if (tup.__len__() == length) return tup.list; throw Py.ValueError("unpack tuple of wrong size"); } PyObject[] ret = new PyObject[length]; PyObject iter = o.__iter__(); try { for (int i = 0; i < length; i++) { PyObject tmp = iter.__iternext__(); if (tmp == null) { throw Py.ValueError("unpack sequence too short"); } ret[i] = tmp; } } catch (PyException exc) { if (Py.matchException(exc, Py.AttributeError)) { throw Py.TypeError("unpack non-sequence"); } else { throw exc; } } if (iter.__iternext__() != null) { throw Py.ValueError("unpack sequence too long"); } return ret; } public static PyObject iter(PyObject seq, String message) { try { return seq.__iter__(); } catch (PyException exc) { if (Py.matchException(exc, Py.TypeError)) throw Py.TypeError(message); throw exc; } } private static IdImpl idimpl = IdImpl.getInstance(); public static long id(PyObject o) { return idimpl.id(o); } public static String idstr(PyObject o) { return idimpl.idstr(o); } public static long java_obj_id(Object o) { return idimpl.java_obj_id(o); } public static String safeRepr(PyObject o) { return o.safeRepr(); } public static void printResult(PyObject ret) { Py.getThreadState().systemState.invoke("displayhook", ret); } public static final int ERROR=-1; public static final int WARNING=0; public static final int MESSAGE=1; public static final int COMMENT=2; public static final int DEBUG=3; public static void maybeWrite(String type, String msg, int level) { if (level <= Options.verbose) { System.err.println(type+": "+msg); } } public static void writeError(String type, String msg) { maybeWrite(type, msg, ERROR); } public static void writeWarning(String type, String msg) { maybeWrite(type, msg, WARNING); } public static void writeMessage(String type, String msg) { maybeWrite(type, msg, MESSAGE); } public static void writeComment(String type, String msg) { maybeWrite(type, msg, COMMENT); } public static void writeDebug(String type, String msg) { maybeWrite(type, msg, DEBUG); } public static void saveClassFile(String name, ByteArrayOutputStream bytestream) { String dirname = Options.proxyDebugDirectory; if (dirname == null) return; byte[] bytes = bytestream.toByteArray(); File dir = new File(dirname); File file = makeFilename(name, dir); new File(file.getParent()).mkdirs(); try { FileOutputStream o = new FileOutputStream(file); o.write(bytes); o.close(); } catch (Throwable t) { t.printStackTrace(); } } private static File makeFilename(String name, File dir) { int index = name.indexOf("."); if (index == -1) return new File(dir, name+".class"); return makeFilename(name.substring(index+1, name.length()), new File(dir, name.substring(0, index))); } }/** @deprecated **/class FixedFileWrapper extends StdoutWrapper { private PyObject file; public FixedFileWrapper(PyObject file) { name = "fixed file"; this.file = file; if (file instanceof PyJavaInstance) { Object tmp = file.__tojava__(OutputStream.class); if ((tmp != Py.NoConversion) && (tmp != null)) { OutputStream os = (OutputStream)tmp; this.file = new PyFile(os, "<java OutputStream>"); } else { tmp = file.__tojava__(Writer.class); if ((tmp != Py.NoConversion) && (tmp != null)) { Writer w = (Writer)tmp; this.file = new PyFile(w, "<java Writer>"); } } } } protected PyObject myFile() { return file; }}/** * A code object wrapper for a python function. */class JavaCode extends PyCode { private PyObject func; public JavaCode(PyObject func) { this.func = func; if (func instanceof PyReflectedFunction) this.co_name = ((PyReflectedFunction) func).__name__; } public PyObject call(PyFrame frame, PyObject closure) { System.out.println("call #1"); return Py.None; } public PyObject call(PyObject args[], String keywords[], PyObject globals, PyObject[] defaults, PyObject closure) { return func.__call__(args, keywords); } public PyObject call(PyObject self, PyObject args[], String keywords[], PyObject globals, PyObject[] defaults, PyObject closure) { return func.__call__(self, args, keywords); } public PyObject call(PyObject globals, PyObject[] defaults, PyObject closure) { return func.__call__(); } public PyObject call(PyObject arg1, PyObject globals, PyObject[] defaults, PyObject closure) { return func.__call__(arg1); } public PyObject call(PyObject arg1, PyObject arg2, PyObject globals, PyObject[] defaults, PyObject closure) { return func.__call__(arg1, arg2); } public PyObject call(PyObject arg1, PyObject arg2, PyObject arg3, PyObject globals, PyObject[] defaults, PyObject closure) { return func.__call__(arg1, arg2, arg3); }}/** * A function object wrapper for a java method which comply with the * PyArgsKeywordsCall standard. */class JavaFunc extends PyObject { java.lang.reflect.Method method; public JavaFunc(java.lang.reflect.Method method) { this.method = method; } public PyObject __call__(PyObject[] args, String[] kws) { Object[] margs = new Object[] { args, kws }; try { return Py.java2py(method.invoke(null, margs)); } catch (Throwable t) { throw Py.JavaError(t); } } public PyObject _doget(PyObject container) { return _doget(container, null); } public PyObject _doget(PyObject container, PyObject wherefound) { if (container == null) return this; return new PyMethod(container, this, wherefound); } public boolean _doset(PyObject container) { throw Py.TypeError("java function not settable: "+method.getName()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -