📄 py.java
字号:
/* Helper functions for PyProxy's */ /** @deprecated **/ public static PyObject jfindattr(PyProxy proxy, String name) { PyInstance o = proxy._getPyInstance(); if (o == null) { proxy.__initProxy__(new Object[0]); o = proxy._getPyInstance(); } PyObject ret = o.__jfindattr__(name); if (ret == null) return null; // Set the current system state to match proxy -- usually // this is a waste of time :-( Py.setSystemState(proxy._getPySystemState()); return ret; } /** @deprecated **/ public static PyObject jgetattr(PyProxy proxy, String name) { PyInstance o = proxy._getPyInstance(); PyObject ret = null; if (o != null) { ret = o.__jfindattr__(name); } if (ret == null) throw Py.AttributeError("abstract method \""+name+ "\" not implemented"); // Set the current system state to match proxy -- usually this is a // waste of time :-( Py.setSystemState(proxy._getPySystemState()); return ret; } /* Convenience methods to create new constants without using "new" */ private static PyInteger[] integerCache = null; public static final PyInteger newInteger(int i) { if (integerCache == null) { integerCache = new PyInteger[1000]; for(int j=-100; j<900; j++) { integerCache[j+100] = new PyInteger(j); } } if (i>=-100 && i < 900) { return integerCache[i+100]; } else { return new PyInteger(i); } } public static PyObject newInteger(long i) { if (i < Integer.MIN_VALUE || i > Integer.MAX_VALUE) return new PyLong(i); else return newInteger((int)i); } public static PyLong newLong(String s) { return new PyLong(s); } public static PyLong newLong(java.math.BigInteger i) { return new PyLong(i); } public static PyComplex newImaginary(double v) { return new PyComplex(0, v); } public static PyFloat newFloat(float v) { return new PyFloat((double)v); } public static PyFloat newFloat(double v) { return new PyFloat(v); } public static PyString newString(char c) { return makeCharacter(c); } public static PyString newString(String s) { return new PyString(s); } public static PyInteger newBoolean(boolean t) { return t ? Py.One : Py.Zero; } // nested scopes: // String[] cellvars,String[] freevars,int npurecell & int moreflags public static PyCode newCode(int argcount, String varnames[], String filename, String name, boolean args, boolean keywords, PyFunctionTable funcs, int func_id, String[] cellvars,String[] freevars, int npurecell, int moreflags) { return new PyTableCode(argcount, varnames, filename, name, 0, args, keywords, funcs, func_id, cellvars, freevars, npurecell, moreflags); } public static PyCode newCode(int argcount, String varnames[], String filename, String name, int firstlineno, boolean args, boolean keywords, PyFunctionTable funcs, int func_id, String[] cellvars,String[] freevars, int npurecell, int moreflags) { return new PyTableCode(argcount, varnames, filename, name, firstlineno, args, keywords, funcs, func_id, cellvars, freevars, npurecell, moreflags); } // -- public static PyCode newCode(int argcount, String varnames[], String filename, String name, boolean args, boolean keywords, PyFunctionTable funcs, int func_id) { return new PyTableCode(argcount, varnames, filename, name, 0, args, keywords, funcs, func_id); } public static PyCode newCode(int argcount, String varnames[], String filename, String name, int firstlineno, boolean args, boolean keywords, PyFunctionTable funcs, int func_id) { return new PyTableCode(argcount, varnames, filename, name, firstlineno, args, keywords, funcs, func_id); } public static PyCode newJavaCode(Class cls, String name) { return new JavaCode(newJavaFunc(cls, name)); } public static PyObject newJavaFunc(Class cls, String name) { try { java.lang.reflect.Method m = cls.getMethod(name, new Class[] { PyObject[].class, String[].class }); return new JavaFunc(m); } catch (NoSuchMethodException e) { throw Py.JavaError(e); } } private static PyObject initExc(String name, PyObject exceptions, PyObject dict) { PyObject tmp = exceptions.__getattr__(name); dict.__setitem__(name, tmp); return tmp; } static void initClassExceptions(PyObject dict) { PyObject exc = imp.load("exceptions"); Exception = initExc("Exception", exc, dict); SystemExit = initExc("SystemExit", exc, dict); StopIteration = initExc("StopIteration", exc, dict); StandardError = initExc("StandardError", exc, dict); KeyboardInterrupt = initExc("KeyboardInterrupt", exc, dict); ImportError = initExc("ImportError", exc, dict); EnvironmentError = initExc("EnvironmentError", exc, dict); IOError = initExc("IOError", exc, dict); OSError = initExc("OSError", exc, dict); EOFError = initExc("EOFError", exc, dict); RuntimeError = initExc("RuntimeError", exc, dict); NotImplementedError = initExc("NotImplementedError", exc, dict); NameError = initExc("NameError", exc, dict); UnboundLocalError = initExc("UnboundLocalError", exc, dict); AttributeError = initExc("AttributeError", exc, dict); SyntaxError = initExc("SyntaxError", exc, dict); IndentationError = initExc("IndentationError", exc, dict); TabError = initExc("TabError", exc, dict); TypeError = initExc("TypeError", exc, dict); AssertionError = initExc("AssertionError", exc, dict); LookupError = initExc("LookupError", exc, dict); IndexError = initExc("IndexError", exc, dict); KeyError = initExc("KeyError", exc, dict); ArithmeticError = initExc("ArithmeticError", exc, dict); OverflowError = initExc("OverflowError", exc, dict); ZeroDivisionError = initExc("ZeroDivisionError", exc, dict); FloatingPointError = initExc("FloatingPointError", exc, dict); ValueError = initExc("ValueError", exc, dict); UnicodeError = initExc("UnicodeError", exc, dict); ReferenceError = initExc("ReferenceError", exc, dict); SystemError = initExc("SystemError", exc, dict); MemoryError = initExc("MemoryError", exc, dict); Warning = initExc("Warning", exc, dict); UserWarning = initExc("UserWarning", exc, dict); DeprecationWarning = initExc("DeprecationWarning", exc, dict); SyntaxWarning = initExc("SyntaxWarning", exc, dict); OverflowWarning = initExc("OverflowWarning", exc, dict); RuntimeWarning = initExc("RuntimeWarning", exc, dict); } public static PySystemState defaultSystemState; // This is a hack to get initializations to work in proper order public static synchronized boolean initPython() { PySystemState.initialize(); return true; } public static Class relFindClass(Class home,String name) { try { ClassLoader loader = home.getClassLoader(); if (loader != null) return loader.loadClass(name); else return Class.forName(name); } catch (ClassNotFoundException exc) { return null; } catch (Throwable t) { throw Py.JavaError(t); } } private static boolean secEnv=false; public static Class findClass(String name) { try { ClassLoader classLoader = Py.getSystemState().getClassLoader(); if (classLoader != null) return classLoader.loadClass(name); if(!secEnv) { try { classLoader = imp.getSyspathJavaLoader(); } catch(SecurityException e) { secEnv=true; } if (classLoader != null) { return classLoader.loadClass(name); } } return Class.forName(name); } catch (ClassNotFoundException e) { // e.printStackTrace(); return null; } catch (IllegalArgumentException e) { // e.printStackTrace(); return null; } catch (NoClassDefFoundError e) { // e.printStackTrace(); return null; } } public static Class findClassEx(String name, String reason) { try { ClassLoader classLoader = Py.getSystemState().getClassLoader(); if (classLoader != null) { writeDebug("import", "trying " + name + " as " + reason + " in classLoader"); return classLoader.loadClass(name); } if(!secEnv) { try { classLoader = imp.getSyspathJavaLoader(); } catch(SecurityException e) { secEnv=true; } if (classLoader != null) { writeDebug("import", "trying " + name + " as " + reason + " in syspath loader"); return classLoader.loadClass(name); } } writeDebug("import", "trying " + name + " as " + reason + " in Class.forName"); return Class.forName(name); } catch (ClassNotFoundException e) { return null; } catch (IllegalArgumentException e) { throw JavaError(e); } catch (LinkageError e) { throw JavaError(e); } } private static void setArgv(String arg0, String[] args) { PyObject argv[] = new PyObject[args.length+1]; argv[0] = new PyString(arg0); for(int i=1; i<argv.length; i++) argv[i] = new PyString(args[i-1]); Py.getSystemState().argv = new PyList(argv); } private static boolean propertiesInitialized = false; private static synchronized void initProperties(String[] args, String[] packages, String[] props, String frozenPackage, String[] modules, ClassLoader classLoader) { if (!propertiesInitialized) { propertiesInitialized = true; if (frozenPackage != null) { Py.frozen = true; if (frozenPackage.length() > 0) Py.frozenPackage = frozenPackage; } java.util.Properties sprops; try { sprops = new java.util.Properties(System.getProperties()); } catch (Throwable t) { sprops = new java.util.Properties(); } if (props != null) { for (int i=0; i<props.length; i+=2) { sprops.put(props[i], props[i+1]); } } //System.err.println("sprops: "+sprops); if (args == null) args = new String[0]; PySystemState.initialize(sprops, null, args, classLoader); } if (modules != null) { if(frozenModules == null) frozenModules = new java.util.Hashtable(); // System.err.println("modules: "); // ?? dbg for (int i = 0; i < modules.length; i++) { String modname = modules[i]; // System.err.print(modname + " "); // ?? dbg frozenModules.put(modname,PRESENT); // py pkgs are potentially java pkgs too. if (modname.endsWith(".__init__")) { String jpkg = modname.substring(0,modname.length()-9); PySystemState.add_package(jpkg); // System.err.print(":j "); // ?? dbg } } // System.out.println(); // ?? dbg } if (packages != null) { for (int i=0; i<packages.length; i+=2) { PySystemState.add_package(packages[i], packages[i+1]); } } } public static void initProxy(PyProxy proxy, String module, String pyclass, Object[] args, String[] packages, String[] props, boolean frozen) { initProxy(proxy, module, pyclass, args, packages, props, null, null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -