📄 py.java
字号:
// Copyright (c) Corporation for National Research Initiativespackage org.python.core;import org.python.parser.ast.modType;import java.lang.reflect.InvocationTargetException;import java.io.*;public final class Py{ static boolean frozen; static String frozenPackage=null; private final static Object PRESENT=new Object(); static java.util.Hashtable frozenModules; static boolean initialized; /* Holds the singleton None and Ellipsis objects */ /** The singleton None Python object **/ public static PyObject None; /** The singleton Ellipsis Python object - written as ... when indexing */ public static PyObject Ellipsis; /** The singleton NotImplemented Python object. Used in rich comparison */ public static PyObject NotImplemented; /** A zero-length array of Strings to pass to functions that don't have any keyword arguments **/ public static String[] NoKeywords; /** A zero-length array of PyObject's to pass to functions that expect zero-arguments **/ public static PyObject[] EmptyObjects; /** A tuple with zero elements **/ public static PyTuple EmptyTuple; /** The Python integer 0 - also used as false **/ public static PyInteger Zero; /** The Python integer 1 - also used as true **/ public static PyInteger One; /** A zero-length Python string **/ public static PyString EmptyString; /** A Python string containing '\n' **/ public static PyString Newline; /** A Python string containing ' ' **/ public static PyString Space; /** A unique object to indicate no conversion is possible in __tojava__ methods **/ public static Object NoConversion; public static PyObject OSError; public static PyObject NotImplementedError; public static PyObject EnvironmentError; /* The standard Python exceptions */ public static PyObject OverflowError; public static PyException OverflowError(String message) { return new PyException(Py.OverflowError, message); } public static PyObject RuntimeError; public static PyException RuntimeError(String message) { return new PyException(Py.RuntimeError, message); } public static PyObject KeyboardInterrupt; /*public static PyException KeyboardInterrupt(String message) { return new PyException(Py.KeyboardInterrupt, message); }*/ public static PyObject FloatingPointError; public static PyException FloatingPointError(String message) { return new PyException(Py.FloatingPointError, message); } public static PyObject SyntaxError; public static PyException SyntaxError(String message) { return new PyException(Py.SyntaxError, message); } public static PyObject IndentationError; public static PyObject TabError; public static PyObject AttributeError; public static PyException AttributeError(String message) { return new PyException(Py.AttributeError, message); } public static PyObject IOError; public static PyException IOError(java.io.IOException ioe) { //System.err.println("ioe: "+ioe); //ioe.printStackTrace(); String message = ioe.getMessage(); if (ioe instanceof java.io.FileNotFoundException) { message = "File not found - "+message; } return new PyException(Py.IOError, message); } public static PyException IOError(String message) { //System.err.println("sioe: "+message); return new PyException(Py.IOError, message); } public static PyObject KeyError; public static PyException KeyError(String message) { return new PyException(Py.KeyError, message); } public static PyObject AssertionError; public static PyException AssertionError(String message) { return new PyException(Py.AssertionError, message); } public static PyObject TypeError; public static PyException TypeError(String message) { return new PyException(Py.TypeError, message); } public static PyObject ReferenceError; public static PyException ReferenceError(String message) { return new PyException(Py.ReferenceError, message); } public static PyObject SystemError; public static PyException SystemError(String message) { return new PyException(Py.SystemError, message); } public static PyObject IndexError; public static PyException IndexError(String message) { return new PyException(Py.IndexError, message); } public static PyObject ZeroDivisionError; public static PyException ZeroDivisionError(String message) { return new PyException(Py.ZeroDivisionError, message); } public static PyObject NameError; public static PyException NameError(String message) { return new PyException(Py.NameError, message); } public static PyObject UnboundLocalError; public static PyException UnboundLocalError(String message) { return new PyException(Py.UnboundLocalError, message); } public static PyObject SystemExit; /*public static PyException SystemExit(String message) { return new PyException(Py.SystemExit, message); }*/ static void maybeSystemExit(PyException exc) { //System.err.println("maybeSystemExit: " + exc.type.toString()); if (Py.matchException(exc, Py.SystemExit)) { PyObject value = exc.value; //System.err.println("exiting: "+value.getClass().getName()); if (value instanceof PyInstance) { PyObject tmp = value.__findattr__("code"); if (tmp != null) value = tmp; } Py.getSystemState().callExitFunc(); if (value instanceof PyInteger) { System.exit(((PyInteger)value).getValue()); } else { if (value != Py.None) { try { Py.println(value); System.exit(1); } catch (Throwable t0) { } } System.exit(0); } } } public static PyObject StopIteration; public static PyException StopIteration(String message) { return new PyException(Py.StopIteration, message); } public static PyObject ImportError; public static PyException ImportError(String message) { return new PyException(Py.ImportError, message); } public static PyObject ValueError; public static PyException ValueError(String message) { return new PyException(Py.ValueError, message); } public static PyObject UnicodeError; public static PyException UnicodeError(String message) { return new PyException(Py.UnicodeError, message); } public static PyObject EOFError; public static PyException EOFError(String message) { return new PyException(Py.EOFError, message); } public static PyObject MemoryError; public static void memory_error(OutOfMemoryError t) { if (Options.showJavaExceptions) { t.printStackTrace(); }// this logic would allow to re-enable the old behavior when it makes sense, // or better offer a hook?// try {// byte[] alloc = new byte[(512*1024)];// } catch(OutOfMemoryError oome) {// System.err.println("Out Of Memory");// System.err.println("You might want to try the -mx flag to increase heap size.");// System.exit(-1);// } } public static PyException MemoryError(String message) { return new PyException(Py.MemoryError, message); } public static PyObject ArithmeticError; public static PyObject LookupError; public static PyObject StandardError; public static PyObject Exception; public static PyObject Warning; public static void Warning(String message) { warning(Warning, message); } public static PyObject UserWarning; public static void UserWarning(String message) { warning(UserWarning, message); } public static PyObject DeprecationWarning; public static void DeprecationWarning(String message) { warning(DeprecationWarning, message); } public static PyObject SyntaxWarning; public static void SyntaxWarning(String message) { warning(SyntaxWarning, message); } public static PyObject OverflowWarning; public static void OverflowWarning(String message) { warning(OverflowWarning, message); } public static PyObject RuntimeWarning; public static void RuntimeWarning(String message) { warning(RuntimeWarning, message); } private static PyObject warnings_mod; private static PyObject importWarnings() { if (warnings_mod != null) return warnings_mod; PyObject mod; try { mod = __builtin__.__import__("warnings"); } catch(PyException e) { if (matchException(e,ImportError)) { return null; } throw e; } warnings_mod = mod; return mod; } private static String warn_hcategory(PyObject category) { PyObject name = category.__findattr__("__name__"); if (name != null) return "["+name+"]"; return "[warning]"; } public static void warning(PyObject category, String message) { PyObject func = null; PyObject mod = importWarnings(); if (mod != null) func = mod.__getattr__("warn"); if (func == null) { System.err.println(warn_hcategory(category) + ": " + message); return; } else { func.__call__(Py.newString(message), category); } } public static void warning(PyObject category, String message, String filename, int lineno, String module, PyObject registry) { PyObject func = null; PyObject mod = importWarnings(); if (mod != null) func = mod.__getattr__("warn_explicit"); if (func == null) { System.err.println(filename + ":" + lineno + ":" + warn_hcategory(category) + ": " + message); return; } else { func.__call__(new PyObject[] { Py.newString(message), category, Py.newString(filename), Py.newInteger(lineno), (module == null) ? Py.None : Py.newString(module), registry}, Py.NoKeywords); } } public static PyObject JavaError; public static PyException JavaError(Throwable t) {// System.err.println("t: "+t); if (t instanceof PyException) { return (PyException)t; } else if (t instanceof InvocationTargetException) { return JavaError( ((InvocationTargetException)t).getTargetException()); }// Remove this automatic coercion, people want to see the real// exceptions!// else if (t instanceof java.io.IOException) {// return IOError((java.io.IOException)t);// }// see corresponding logic in matchException else if (t instanceof OutOfMemoryError) { memory_error((OutOfMemoryError)t); } PyObject exc = Py.java2py(t); return new PyException(exc.__class__, exc); } // Don't allow any constructors. Class only provides static methods. private Py() { ; } /** @deprecated **/ //public static InterpreterState interp; /** Convert a given <code>PyObject</code> to an instance of a Java class. Identical to <code>o.__tojava__(c)</code> except that it will raise a <code>TypeError</code> if the conversion fails. @param o the <code>PyObject</code> to convert. @param c the class to convert it to. **/ public static Object tojava(PyObject o, Class c) { Object obj = o.__tojava__(c); if (obj == Py.NoConversion) { throw Py.TypeError("can't convert "+o.__repr__()+" to "+ c.getName()); } return obj; } // ??pending: was @deprecated but is actually used by proxie code. // Can get rid of it? public static Object tojava(PyObject o, String s) { Class c = findClass(s); if (c == null) throw Py.TypeError("can't convert to: "+s); return tojava(o, c); // prev:Class.forName }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -