📄 py.java
字号:
public static PyException makeException(PyObject type, PyObject value, PyObject traceback) { if (type instanceof PyInstance) { if (value != Py.None) { throw TypeError("instance exceptions may not have " + "a separate value"); } else { type = type.__class__; //return new PyException(type.__class__, type); } } if (traceback == None) return new PyException(type, value); if (!(traceback instanceof PyTraceback)) throw TypeError("raise 3rd arg must be traceback or None"); return new PyException(type, value, (PyTraceback)traceback); } public static PyObject runCode(PyCode code, PyObject locals, PyObject globals) { //System.out.println("run code"); PyFrame f; /*if (globals == null && locals == null) { f = Py.getFrame(); } else {*/ if (locals == null) { if (globals != null) { locals = globals; } else { locals = Py.getFrame().getf_locals(); } } if (globals == null) globals = Py.getFrame().f_globals; PyTableCode tc=null; if (code instanceof PyTableCode) tc = (PyTableCode)code; f = new PyFrame(tc, locals, globals, Py.getThreadState().systemState.builtins); //} return code.call(f); } public static void exec(PyObject o, PyObject globals, PyObject locals) { PyCode code; if (o instanceof PyCode) code = (PyCode)o; else { String contents = null; if (o instanceof PyString) contents = o.toString(); else if (o instanceof PyFile) { PyFile fp = (PyFile)o; if (fp.closed) return; contents = fp.read().toString(); } else throw Py.TypeError( "exec: argument 1 must be string, code or file object"); code = Py.compile_flags(contents, "<string>", "exec", Py.getCompilerFlags()); } Py.runCode(code, locals, globals); } private static ThreadStateMapping threadStateMapping = null; public static final ThreadState getThreadState() { return getThreadState(null); } public static final ThreadState getThreadState(PySystemState newSystemState) { if (threadStateMapping == null) { synchronized (Py.class) { if (threadStateMapping == null) threadStateMapping = ThreadStateMapping.makeMapping(); } } return threadStateMapping.getThreadState(newSystemState); } public static final PySystemState setSystemState(PySystemState newSystemState) { ThreadState ts = getThreadState(newSystemState); PySystemState oldSystemState = ts.systemState; if (oldSystemState != newSystemState) { //System.err.println("Warning: changing systemState "+ // "for same thread!"); ts.systemState = newSystemState; } return oldSystemState; } public static final PySystemState getSystemState() { return getThreadState().systemState; //defaultSystemState; } /* Get and set the current frame */ public static PyFrame getFrame() { //System.out.println("getFrame"); ThreadState ts = getThreadState(); if (ts == null) return null; return ts.frame; } public static void setFrame(PyFrame f) { //System.out.println("setFrame"); getThreadState().frame = f; } /* These are not used anymore. Uncomment them if there is a future clamor to make this functionality more easily usable public static void pushFrame(PyFrame f) { ThreadState ts = getThreadState(); f.f_back = ts.frame; if (f.f_builtins == null) f.f_builtins = f.f_back.f_builtins; ts.frame = f; } public static PyFrame popFrame() { ThreadState ts = getThreadState(); PyFrame f = ts.frame.f_back; ts.frame = f; return f; } */ /* A collection of functions for implementing the print statement */ public static StdoutWrapper stderr; static StdoutWrapper stdout; //public static StdinWrapper stdin; public static void print(PyObject file, PyObject o) { if (file == None) print(o); else new FixedFileWrapper(file).print(o); } public static void printComma(PyObject file, PyObject o) { if (file == None) printComma(o); else new FixedFileWrapper(file).printComma(o); } public static void println(PyObject file, PyObject o) { if (file == None) println(o); else new FixedFileWrapper(file).println(o); } public static void printlnv(PyObject file) { if (file == None) println(); else new FixedFileWrapper(file).println(); } public static void print(PyObject o) { stdout.print(o); } public static void printComma(PyObject o) { stdout.printComma(o); } public static void println(PyObject o) { stdout.println(o); } public static void println() { stdout.println(); } /* A collection of convenience functions for converting PyObjects to Java primitives */ public static boolean py2boolean(PyObject o) { return o.__nonzero__(); } public static byte py2byte(PyObject o) { if (o instanceof PyInteger) return (byte)((PyInteger)o).getValue(); Object i = o.__tojava__(Byte.TYPE); if (i == null || i == Py.NoConversion) throw Py.TypeError("integer required"); return ((Byte) i).byteValue(); } public static short py2short(PyObject o) { if (o instanceof PyInteger) return (short)((PyInteger)o).getValue(); Object i = o.__tojava__(Short.TYPE); if (i == null || i == Py.NoConversion) throw Py.TypeError("integer required"); return ((Short) i).shortValue(); } public static int py2int(PyObject o) { return py2int(o, "integer required"); } public static int py2int(PyObject o, String msg) { if (o instanceof PyInteger) return (int)((PyInteger)o).getValue(); Object obj = o.__tojava__(Integer.TYPE); if (obj == Py.NoConversion) throw Py.TypeError(msg); return ((Integer)obj).intValue(); } public static long py2long(PyObject o) { if (o instanceof PyInteger) return (long)((PyInteger)o).getValue(); Object i = o.__tojava__(Long.TYPE); if (i == null || i == Py.NoConversion) throw Py.TypeError("integer required"); return ((Long) i).longValue(); } public static float py2float(PyObject o) { if (o instanceof PyFloat) return (float)((PyFloat)o).getValue(); if (o instanceof PyInteger) return (float)((PyInteger)o).getValue(); Object i = o.__tojava__(Float.TYPE); if (i == null || i == Py.NoConversion) throw Py.TypeError("float required"); return ((Float) i).floatValue(); } public static double py2double(PyObject o) { if (o instanceof PyFloat) return (double)((PyFloat)o).getValue(); if (o instanceof PyInteger) return (double)((PyInteger)o).getValue(); Object i = o.__tojava__(Double.TYPE); if (i == null || i == Py.NoConversion) throw Py.TypeError("float required"); return ((Double) i).doubleValue(); } public static char py2char(PyObject o) { return py2char(o, "char required"); } public static char py2char(PyObject o, String msg) { if (o instanceof PyString) { PyString s = (PyString)o; if (s.__len__() != 1) throw Py.TypeError(msg); return s.toString().charAt(0); } if (o instanceof PyInteger) { return (char)((PyInteger)o).getValue(); } Object i = o.__tojava__(Character.TYPE); if (i == null || i == Py.NoConversion) throw Py.TypeError(msg); return ((Character) i).charValue(); } public static void py2void(PyObject o) { if (o != Py.None) { throw Py.TypeError("None required for void return"); } } private static PyString[] letters=null; static final PyString makeCharacter(Character o) { return makeCharacter(o.charValue()); } static final PyString makeCharacter(char c) { if (c > 255) { return new PyString(new Character(c).toString()); } if (letters == null) { letters = new PyString[256]; for(char j=0; j<256; j++) { letters[j] = new PyString(new Character(j).toString()); } } return letters[c]; } // Needs rewriting for efficiency and extensibility public static PyObject java2py(Object o) { if (o instanceof PyObject) return (PyObject)o; if (o instanceof PyProxy) return ((PyProxy)o)._getPyInstance(); if (o instanceof Number) { if (o instanceof Double || o instanceof Float) { return new PyFloat(((Number)o).doubleValue()); } else if (o instanceof Long) { return new PyLong(((Number)o).longValue()); } else if (o instanceof Integer || o instanceof Byte || o instanceof Short) { return new PyInteger(((Number)o).intValue()); } } if (o instanceof Boolean) { return ((Boolean)o).booleanValue() ? Py.One : Py.Zero; } if (o == null) return Py.None; if (o instanceof String) return new PyString((String)o); if (o instanceof Character) return makeCharacter((Character)o); if (o instanceof Class) return PyJavaClass.lookup((Class)o); Class c = o.getClass(); if (c.isArray()) { return new PyArray(c.getComponentType(), o); } return new PyJavaInstance(o); } public static PyObject makeClass(String name, PyObject[] bases, PyCode code, PyObject doc) { return makeClass(name, bases, code, doc, null, null); } public static PyObject makeClass(String name, PyObject[] bases, PyCode code, PyObject doc, PyObject[] closure_cells) { return makeClass(name, bases, code, doc, null, closure_cells); } public static PyObject makeClass(String name, PyObject[] bases, PyCode code, PyObject doc, Class proxyClass) { return makeClass(name, bases, code, doc, proxyClass, null); } private static Class[] pyClassCtrSignature = { String.class, PyTuple.class, PyObject.class, Class.class }; public static PyObject makeClass(String name, PyObject[] bases, PyCode code, PyObject doc, Class proxyClass, PyObject[] closure_cells) { PyFrame frame = getFrame(); PyObject globals = frame.f_globals; PyObject dict = code.call(Py.EmptyObjects, Py.NoKeywords, globals, Py.EmptyObjects, new PyTuple(closure_cells)); if (doc != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -