public void cleanup() { systemState.callExitFunc(); try { Py.getSystemState().stdout.invoke("flush"); } catch (PyException pye) { // fall through } try { Py.getSystemState().stderr.invoke("flush"); } catch (PyException pye) { // fall through } }
public PythonInterpreter(PyObject dict, PySystemState systemState) { if (dict == null) dict = new PyStringMap(); if (systemState == null) { systemState = Py.getSystemState(); if (systemState == null) systemState = new PySystemState(); } module = new PyModule("main", dict); this.systemState = systemState; locals = module.__dict__; setState(); }
protected PicoContainer createContainerFromScript( PicoContainer parentContainer, Object assemblyScope) { ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader pyClassLoader = Py.getSystemState().getClassLoader(); try { Thread.currentThread().setContextClassLoader(getClassLoader()); Py.getSystemState().setClassLoader(getClassLoader()); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.set("parent", parentContainer); interpreter.set("assemblyScope", assemblyScope); interpreter.execfile(getScriptInputStream(), "picocontainer.py"); return (PicoContainer) interpreter.get("pico", PicoContainer.class); } catch (IOException e) { throw new ScriptedPicoContainerMarkupException(e); } finally { Thread.currentThread().setContextClassLoader(oldClassLoader); Py.getSystemState().setClassLoader(pyClassLoader); } }
public PythonInterpreter(PyObject dict, PySystemState systemState) { if (dict == null) { dict = new PyStringMap(); } if (systemState == null) { systemState = Py.getSystemState(); if (systemState == null) { systemState = new PySystemState(); } } this.systemState = systemState; setState(); module = new PyModule("__main__", dict); systemState.modules.__setitem__("__main__", module); locals = dict; }
/** * Creates a jython object instance for the script cache. * * @param key the path to the jython script * @return an instantiated jython object * @throws Exception if the jython object failed to be instantiated */ @Override public Object createEntry(Object key) throws Exception { // log.debug("createEntry({})", key); String path = key.toString(); int qmarkPos = path.lastIndexOf("?"); if (qmarkPos != -1) { path = path.substring(0, qmarkPos); } int slashPos = path.indexOf("/"); String portalId = path.substring(0, slashPos); PyObject scriptObject = null; InputStream in = velocityService.getResource(path); if (in == null) { log.debug("Failed to load script: '{}'", path); } else { // add current and default portal directories to python sys.path addSysPaths(portalId, Py.getSystemState()); // setup the python interpreter PythonInterpreter python = PythonInterpreter.threadLocalStateInterpreter(null); // expose services for backward compatibility - deprecated python.set("Services", scriptingServices); // python.setLocals(scriptObject); JythonLogger jythonLogger = new JythonLogger(path); python.setOut(jythonLogger); python.setErr(jythonLogger); python.execfile(in, path); String scriptClassName = StringUtils.capitalize(FilenameUtils.getBaseName(path)) + "Data"; PyObject scriptClass = python.get(scriptClassName); if (scriptClass != null) { scriptObject = scriptClass.__call__(); } else { log.debug("Failed to find class '{}'", scriptClassName); } python.cleanup(); } return scriptObject; }