📄 pysystemstate.java
字号:
//} return; } initialized = true; // initialize the JPython registry initRegistry(preProperties, postProperties); // other initializations initBuiltins(registry); initStaticFields(); // Initialize the path (and add system defaults) defaultPath = initPath(registry); defaultArgv = initArgv(argv); // Set up the known Java packages initPackages(registry); // Finish up standard Python initialization... Py.defaultSystemState = new PySystemState(); Py.setSystemState(Py.defaultSystemState); if (classLoader != null) Py.defaultSystemState.setClassLoader(classLoader); Py.initClassExceptions(__builtin__class.__getattr__("__dict__")); // Make sure that Exception classes have been loaded PySyntaxError dummy = new PySyntaxError("", 1,1,"", ""); } private static void initStaticFields() { Py.None = new PyNone(); Py.NotImplemented = new PyNotImplemented(); Py.NoKeywords = new String[0]; Py.EmptyObjects = new PyObject[0]; Py.EmptyTuple = new PyTuple(Py.EmptyObjects); Py.NoConversion = new PySingleton("Error"); Py.Ellipsis = new PyEllipsis(); Py.Zero = new PyInteger(0); Py.One = new PyInteger(1); Py.EmptyString = new PyString(""); Py.Newline = new PyString("\n"); Py.Space = new PyString(" "); __builtin__class = PyJavaClass.lookup(__builtin__.class); // Setup standard wrappers for stdout and stderr... Py.stderr = new StderrWrapper(); Py.stdout = new StdoutWrapper(); String s = null; if (PY_RELEASE_LEVEL == 0x0A) s = "alpha"; else if (PY_RELEASE_LEVEL == 0x0B) s = "beta"; else if (PY_RELEASE_LEVEL == 0x0C) s = "candidate"; else if (PY_RELEASE_LEVEL == 0x0F) s = "final"; version_info = new PyTuple(new PyObject[] { Py.newInteger(PY_MAJOR_VERSION), Py.newInteger(PY_MINOR_VERSION), Py.newInteger(PY_MICRO_VERSION), Py.newString(s), Py.newInteger(PY_RELEASE_SERIAL) }); } public static PackageManager packageManager; public static File cachedir; private static void initCacheDirectory(Properties props) { if (Py.frozen) { cachedir = null; return; } String skip = props.getProperty("python.cachedir.skip", "false"); if (skip.equalsIgnoreCase("true")) { cachedir = null; return; } cachedir = new File(props.getProperty("python.cachedir", "cachedir")); if (!cachedir.isAbsolute()) { cachedir = new File(PySystemState.prefix, cachedir.getPath()); } } private static void initPackages(Properties props) { initCacheDirectory(props); File pkgdir; if (cachedir != null) { pkgdir = new File(cachedir, "packages"); } else { pkgdir = null; } packageManager = new SysPackageManager(pkgdir, props); } private static PyList initArgv(String[] args) { PyList argv = new PyList(); if (args != null) { for (int i=0; i<args.length; i++) { argv.append(new PyString(args[i])); } } return argv; } private static Hashtable builtinNames; public static String[] builtin_module_names = null; private static void addBuiltin(String name) { String classname; String modname; int colon = name.indexOf(':'); if (colon != -1) { // name:fqclassname modname = name.substring(0, colon).trim(); classname = name.substring(colon+1, name.length()).trim(); if (classname.equals("null")) // name:null, i.e. remove it classname = null; } else { modname = name.trim(); classname = "org.python.modules." + modname; } if (classname != null) builtinNames.put(modname, classname); else builtinNames.remove(modname); } private static void initBuiltins(Properties props) { builtinNames = new Hashtable(); // add builtins specified in the Setup.java file for (int i=0; i < Setup.builtinModules.length; i++) addBuiltin(Setup.builtinModules[i]); // add builtins specified in the registry file String builtinprop = props.getProperty("python.modules.builtin", ""); StringTokenizer tok = new StringTokenizer(builtinprop, ","); while (tok.hasMoreTokens()) addBuiltin(tok.nextToken()); int n = builtinNames.size(); builtin_module_names = new String[n]; Enumeration keys = builtinNames.keys(); for (int i=0; i<n; i++) builtin_module_names[i] = (String)keys.nextElement(); } static String getBuiltin(String name) { return (String)builtinNames.get(name); } private static PyList initPath(Properties props) { PyList path = new PyList(); if (!Py.frozen) { addPaths(path, props.getProperty("python.prepath", ".")); if (prefix != null) { String libpath = new File(prefix, "Lib").toString(); path.append(new PyString(libpath)); } addPaths(path, props.getProperty("python.path", "")); } return path; } private static void addPaths(PyList path, String pypath) { StringTokenizer tok = new StringTokenizer(pypath, java.io.File.pathSeparator); while (tok.hasMoreTokens()) path.append(new PyString(tok.nextToken().trim())); } public static PyJavaPackage add_package(String n) { return add_package(n, null); } public static PyJavaPackage add_package(String n, String contents) { return packageManager.makeJavaPackage(n, contents, null); } /** * Add a classpath directory to the list of places that are searched * for java packages. * <p> * <b>Note</b>. Classes found in directory and subdirectory are not * made available to jython by this call. It only make the java * package found ion the directory available. This call is mostly * usefull if jython is embedded in an application that deals with * its own classloaders. A servlet container is a very good example. * Calling add_classdir("<context>/WEB-INF/classes") makes the java * packages in WEB-INF classes available to jython import. However the * actual classloading is completely handled by the servlet container's * context classloader. */ public static void add_classdir(String directoryPath) { packageManager.addDirectory(new File(directoryPath)); } /** * Add a .jar & .zip directory to the list of places that are searched * for java .jar and .zip files. The .jar and .zip files found will not * be cached. * <p> * <b>Note</b>. Classes in .jar and .zip files found in the directory * are not made available to jython by this call. See the note for * add_classdir(dir) for more details. * * @param directoryPath The name of a directory. * * @see #add_classdir */ public static void add_extdir(String directoryPath) { packageManager.addJarDir(directoryPath, false); } /** * Add a .jar & .zip directory to the list of places that are searched * for java .jar and .zip files. * <p> * <b>Note</b>. Classes in .jar and .zip files found in the directory * are not made available to jython by this call. See the note for * add_classdir(dir) for more details. * * @param directoryPath The name of a directory. * @param cache Controls if the packages in the zip and jar * file should be cached. * * @see #add_classdir */ public static void add_extdir(String directoryPath, boolean cache) { packageManager.addJarDir(directoryPath, cache); } public TraceFunction tracefunc = null; public TraceFunction profilefunc = null; public void settrace(PyObject tracefunc) { //InterpreterState interp = Py.getThreadState().interp; if (tracefunc == Py.None) { this.tracefunc = null; } else { this.tracefunc = new PythonTraceFunction(tracefunc); } } public void setprofile(PyObject profilefunc) { //InterpreterState interp = Py.getThreadState().interp; if (profilefunc == Py.None) { this.profilefunc = null; } else { this.profilefunc = new PythonTraceFunction(profilefunc); } } private InputStream getSystemIn() { if (Options.pollStandardIn) { return new PollingInputStream(System.in); } else { return System.in; } } public String getdefaultencoding() { return codecs.getDefaultEncoding(); } public void setdefaultencoding(String encoding) { codecs.setDefaultEncoding(encoding); } // Not public by design. We can't rebind the displayhook if // a reflected function is inserted in the class dict. static void displayhook(PyObject o) { /* Print value except if None */ /* After printing, also assign to '_' */ /* Before, set '_' to None to avoid recursion */ if (o == Py.None) return; PySystemState sys = Py.getThreadState().systemState; sys.builtins.__setitem__("_", Py.None); Py.stdout.println(o.__repr__()); sys.builtins.__setitem__("_", o); } static void excepthook(PyObject type, PyObject val, PyObject tb) { Py.displayException(type, val, tb, null); } public void callExitFunc() throws PyIgnoreMethodTag { PyObject exitfunc = __findattr__("exitfunc"); if (exitfunc != null) { try { exitfunc.__call__(); } catch (PyException exc) { if (!Py.matchException(exc, Py.SystemExit)) { Py.println(stderr, Py.newString("Error in sys.exitfunc:")); } Py.printException(exc); } } }}// This class is based on a suggestion from Yunho Jeonclass PollingInputStream extends FilterInputStream { public PollingInputStream(InputStream s) { super(s); } private void waitForBytes() throws IOException { try { while(available()==0) { //System.err.println("waiting..."); Thread.currentThread().sleep(100); } } catch (InterruptedException e) { throw new PyException(Py.KeyboardInterrupt, "interrupt waiting on <stdin>"); } } public int read() throws IOException { waitForBytes(); return super.read(); } public int read(byte b[], int off, int len) throws IOException { waitForBytes(); return super.read(b, off, len); }}class PySystemStateFunctions extends PyBuiltinFunctionSet{ PySystemStateFunctions(String name, int index, int minargs, int maxargs) { super(name, index, minargs, maxargs, false, null); } public PyObject __call__(PyObject arg) { PySystemState sys = Py.getThreadState().systemState; switch (index) { case 10: sys.displayhook(arg); return Py.None; default: throw argCountError(1); } } public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { PySystemState sys = Py.getThreadState().systemState; switch (index) { case 30: sys.excepthook(arg1, arg2, arg3); return Py.None; default: throw argCountError(3); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -