private static synchronized PythonInterpreter initPythonInterpreter(String[] args, String pythonPath, String scriptName) { if (!jythonInitialized) { // the java stack traces within the jython runtime aren't useful for users System.getProperties().put("python.options.includeJavaStackInExceptions", "false"); PySystemState.initialize(System.getProperties(), new Properties(), args); pythonInterpreter = new PythonInterpreter(); pythonInterpreter.getSystemState().path.add(0, pythonPath); pythonInterpreter.setErr(System.err); pythonInterpreter.setOut(System.out); pythonInterpreter.exec("import " + scriptName); jythonInitialized = true; } return pythonInterpreter; } }
@Override protected void setUp() throws Exception { interp = new PythonInterpreter(new PyStringMap(), new PySystemState()); }
Py.setAdapter(adapter); boolean standalone = false; String jarFileName = getJarFileName(); if (jarFileName != null) { standalone = isStandalone(jarFileName); initRegistry(preProperties, postProperties, standalone, jarFileName); initBuiltins(registry); defaultPath = initPath(registry, standalone, jarFileName); defaultArgv = initArgv(argv); defaultExecutable = initExecutable(registry); initPackages(registry); initConsole(registry); Py.defaultSystemState = new PySystemState(); Py.setSystemState(Py.defaultSystemState); if (classLoader != null) { Py.defaultSystemState.setClassLoader(classLoader); Py.initClassExceptions(getDefaultBuiltins()); Py.defaultSystemState.__setattr__("_jy_console", Py.java2py(Py.getConsole()));
/** * PyServlet's initialization can be performed as a ServletContextListener or as a regular * servlet, and this is the shared init code. If both initializations are used in a single * context, the system state initialization code only runs once. */ protected static void init(Properties props, ServletContext context) { String rootPath = getRootPath(context); context.setAttribute(INIT_ATTR, true); Properties baseProps = PySystemState.getBaseProperties(); // Context parameters Enumeration<?> e = context.getInitParameterNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); props.put(name, context.getInitParameter(name)); } if (props.getProperty("python.home") == null && baseProps.getProperty("python.home") == null) { props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib"); } PySystemState.initialize(baseProps, props, new String[0]); PySystemState.add_package("javax.servlet"); PySystemState.add_package("javax.servlet.http"); PySystemState.add_package("javax.servlet.jsp"); PySystemState.add_package("javax.servlet.jsp.tagext"); PySystemState.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true); }
PySystemState state = new PySystemState(); state.setClassLoader(classLoader); PythonInterpreter interp = new PythonInterpreter(table, state);
public void init(Page owner, String zslang) { super.init(owner, zslang); if (System.getProperty("python.home") == null) System.setProperty("python.home", System.getProperty("java.io.tmpdir")); PySystemState.initialize(); PySystemState.add_extdir(owner.getDesktop().getWebApp().getRealPath("/WEB-INF/lib"), true); PySystemState.add_classdir(owner.getDesktop().getWebApp().getRealPath("/WEB-INF/classes")); _ip = new PythonInterpreter(new Variables()); }
Properties preProperties = PySystemState.getBaseProperties(); PySystemState.initialize(preProperties, opts.properties, opts.argv); PySystemState systemState = Py.getSystemState(); addWarnings(warnOptionsFromEnv(), warnoptions); systemState.setWarnoptions(warnoptions); interp.cleanup(); Py.setSystemState(new PySystemState());
public Void run() { ClassLoader loader = this.getClass().getClassLoader(); PySystemState.initialize(System.getProperties(), null, arguments, loader); if (loader instanceof JythonClassLoader) { logger.debug("jythonClassLoader"); JythonClassLoader jythonLoader = (JythonClassLoader) loader; JarFile xbayaJarFile = jythonLoader.getXBayaJarFile(); if (xbayaJarFile != null) { String jarPath = xbayaJarFile.getName(); logger.debug("jarPath: " + jarPath); // String jarDir = jarPath.substring(0, // jarPath.lastIndexOf()); File jarFile = new File(jarPath); String jarDir = jarFile.getParent(); logger.debug("jarDir: " + jarDir); // This is for the Jython interpreter to // solve import statements. PySystemState.add_extdir(jarDir); } } PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec(script); return null; } });
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { PySystemState sys = new PySystemState(); sys.setCurrentWorkingDir(build.getWorkspace().getRemote()); PythonInterpreter interp = new PythonInterpreter(null, sys); interp.setOut(listener.getLogger()); interp.setErr(listener.getLogger()); interp.exec(this.getCommand()); interp.cleanup(); build.setResult(Result.SUCCESS); return true; } }
public static void run(String[] args) { final String runner = "__run__"; String[] argv = new String[args.length + 1]; argv[0] = runner; System.arraycopy(args, 0, argv, 1, args.length); PySystemState.initialize(PySystemState.getBaseProperties(), null, argv); imp.load(runner); }
public PySystemState() { initialize(); closer = new PySystemStateCloser(this); modules = new PyStringMap(); path.append(Py.newString(ClasspathPyImporter.PYCLASSPATH_PREFIX)); executable = defaultExecutable; builtins = getDefaultBuiltins(); platform = defaultPlatform; stdout = __stdout__ = new PyFile(System.out, "<stdout>", "w" + mode, buffering, false); stderr = __stderr__ = new PyFile(System.err, "<stderr>", "w" + mode, 0, false); initEncoding(); builtins = getDefaultBuiltins(); modules.__setitem__("__builtin__", new PyModule("__builtin__", getDefaultBuiltins())); __dict__ = new PyStringMap(); __dict__.invoke("update", getType().fastGetDict()); __dict__.__setitem__("displayhook", __displayhook__); __dict__.__setitem__("excepthook", __excepthook__);
preProperties = getBaseProperties(); ClassLoader context = Thread.currentThread().getContextClassLoader(); if (context != null) { if (initialize(preProperties, postProperties, argv, classLoader, adapter, context)) { return; if (initialize(preProperties, postProperties, argv, classLoader, adapter, sysStateLoader)) { return; e.printStackTrace(System.err); doInitialize(preProperties, postProperties, argv, classLoader, adapter);
/** * Initializes a default PythonInterpreter and runs the code loaded from the * {@link CodeBootstrap} as __main__ Called by the code generated in * {@link org.python.compiler.Module#addMain()} */ public static void runMain(CodeBootstrap main, String[] args) throws Exception { PySystemState.initialize(null, null, args, main.getClass().getClassLoader()); try { imp.createFromCode("__main__", CodeLoader.loadCode(main)); } catch (PyException e) { Py.getSystemState().callExitFunc(); if (e.match(Py.SystemExit)) { return; } throw e; } Py.getSystemState().callExitFunc(); } //XXX: this needs review to make sure we are cutting out all of the Java
@Override protected PicoContainer createContainerFromScript(final PicoContainer parentContainer, final 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 interpreter.get("pico", PicoContainer.class); } catch (IOException e) { throw new ScriptedPicoContainerMarkupException(e); } finally { Thread.currentThread().setContextClassLoader(oldClassLoader); Py.getSystemState().setClassLoader(pyClassLoader); } } }
/** * Shut down the engine. * * <p> * We don't use m_interpreter.cleanup(), which delegates to * PySystemState.callExitFunc, as callExitFunc logs problems to stderr. * Instead we duplicate the callExitFunc behaviour raise our own exceptions. * </p> * * @throws EngineException * If the engine could not be shut down. */ @Override public void shutdown() throws EngineException { final PyObject exitfunc = m_systemState.__findattr__("exitfunc"); if (exitfunc != null) { try { exitfunc.__call__(); } catch (final PyException e) { throw new JythonScriptExecutionException( "calling script exit function", e); } } }
public static PyJavaPackage add_package(String n) { return add_package(n, null); }
protected PythonInterpreter initInterpreter(Set<String> dependencies) { logger.info("Creating python interpreter with [" + dependencies.size() + "] dependencies [" + dependencies + "]"); if(!dependencies.isEmpty()) { PySystemState systemState = new PySystemState(); for (String dependency: dependencies) { systemState.path.append(new PyString(dependency)); } return new ThreadSafePythonInterpreter(systemState); } return GLOBAL_INTERPRETER; }
final PySystemState state = new PySystemState(); state.setClassLoader(new MyClassLoader()); Py.setSystemState(state);
public void init(Page owner, String zslang) { super.init(owner, zslang); if (System.getProperty("python.home") == null) System.setProperty("python.home", System.getProperty("java.io.tmpdir")); PySystemState.initialize(); PySystemState.add_extdir(owner.getDesktop().getWebApp().getRealPath("/WEB-INF/lib"), true); PySystemState.add_classdir(owner.getDesktop().getWebApp().getRealPath("/WEB-INF/classes")); _ip = new PythonInterpreter(new Variables()); }
public void callExitFunc() throws PyIgnoreMethodTag { PyObject exitfunc = __findattr__("exitfunc"); if (exitfunc != null) { try { exitfunc.__call__(); } catch (PyException exc) { if (!exc.match(Py.SystemExit)) { Py.println(stderr, Py.newString("Error in sys.exitfunc:")); } Py.printException(exc); } } Py.flushLine(); }