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; } }
public synchronized static void initializePython() { if (isInitialized.get()) return; // Set the Jython package cache directory. Properties jythonProperties = new Properties(); String jythonCacheDir = Platform.getUserDataDirectory() + Platform.SEP + "_jythoncache"; jythonProperties.put("python.cachedir", jythonCacheDir); // Initialize Python. PySystemState.initialize(System.getProperties(), jythonProperties, new String[]{""}); // Add the built-in Python libraries. File nodeBoxLibraries = new File(libDir, "nodeboxlibs.zip"); Py.getSystemState().path.add(new PyString(nodeBoxLibraries.getAbsolutePath())); // This folder contains unarchived NodeBox libraries. // Only used in development. File developmentLibraries = new File("src/main/python"); Py.getSystemState().path.add(new PyString(developmentLibraries.getAbsolutePath())); // Add the user's Python directory. Py.getSystemState().path.add(new PyString(Platform.getUserPythonDirectory().getAbsolutePath())); isInitialized.set(true); }
public static synchronized void initialize() { initialize(null, null); }
public static synchronized void initialize(Properties preProperties, Properties postProperties) { initialize(preProperties, postProperties, new String[] {""}); }
public static synchronized boolean initPython() { PySystemState.initialize(); return true; }
public static synchronized void initialize(Properties preProperties, Properties postProperties, String[] argv) { initialize(preProperties, postProperties, argv, null); }
/** * Initializes the Jython runtime. This should only be called once, before any other Python * objects (including PythonInterpreter) are created. * * @param preProperties A set of properties. Typically System.getProperties() is used. * preProperties override properties from the registry file. * @param postProperties Another set of properties. Values like python.home, python.path and all * other values from the registry files can be added to this property set. * postProperties override system properties and registry properties. * @param argv Command line arguments, assigned to sys.argv. */ public static void initialize(Properties preProperties, Properties postProperties, String[] argv) { PySystemState.initialize(preProperties, postProperties, argv); }
@Override protected void setUp() throws Exception { PySystemState.initialize(); _handler = new DataHandler(); }
public static synchronized void initialize(Properties preProperties, Properties postProperties, String[] argv, ClassLoader classLoader) { initialize(preProperties, postProperties, argv, classLoader, new ClassicPyObjectAdapter()); }
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 ThreadState getThreadState(PySystemState newSystemState) { Object scoped = scopedThreadState.get()[0]; if (scoped != null) { return (ThreadState)scoped; } Thread currentThread = Thread.currentThread(); ThreadState ts = cachedThreadState.get(currentThread); if (ts != null) { return ts; } if (newSystemState == null) { Py.writeDebug("threadstate", "no current system state"); if (Py.defaultSystemState == null) { PySystemState.initialize(); } newSystemState = Py.defaultSystemState; } ts = new ThreadState(newSystemState); cachedThreadState.put(currentThread, ts); return ts; }
@Before public void setUp() throws Exception { Properties props = new Properties(System.getProperties()); props.setProperty(PySystemState.PYTHON_CACHEDIR_SKIP, "true"); PySystemState.initialize(props, null); interp = new PythonInterpreter(); String input = new String(); input += "import java.io.ByteArrayInputStream\n"; input += "import java.lang.String\n"; input += "import org.python.core.Options\n"; input += "org.python.core.Options.proxyDebugDirectory = 'build/classes'\n"; input += "class ProxyTest(java.io.ByteArrayInputStream):\n"; input += " def somemethod(self): pass\n"; input += "ProxyTest(java.lang.String('teststr').getBytes())\n"; interp.exec(input); proxyClass = Class.forName("org.python.proxies.__main__$ProxyTest$0"); }
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; } });
/** * make sure post properties override pre properties * * @throws Exception */ public void testInitialize_PrePostProperties() throws Exception { Properties preProps = createPreProperties(); preProps.setProperty(FIRST_PROP, PRE); preProps.setProperty(SECOND_PROP, PRE); Properties postProps = createPostProperties(); postProps.setProperty(SECOND_PROP, POST); PySystemState.initialize(preProps, postProps); Properties registry = PySystemState.registry; String first = registry.getProperty(FIRST_PROP, ANY); String second = registry.getProperty(SECOND_PROP, ANY); assertEquals(PRE, first); assertEquals(POST, second); }
/** * make sure a pre property overrides any registry properties with the same name * * @throws Exception */ public void testInitialize_Pre_Registries() throws Exception { // prepare both registry files String contentToAppend = FIRST_PROP.concat("=").concat(INSTALLED); appendToInstalledRegistry(contentToAppend); String userContent = FIRST_PROP.concat("=").concat(USER); addUserRegistry(userContent); // set same property on pre properties Properties preProps = createPreProperties(); preProps.setProperty(FIRST_PROP, PRE); Properties postProps = createPostProperties(); PySystemState.initialize(preProps, postProps); Properties registry = PySystemState.registry; String first = registry.getProperty(FIRST_PROP, ANY); assertEquals(PRE, first); }
/** * make sure a property from the user registry overrides the same property from the installed * registry * * @throws Exception */ public void testInitialize_Registry_User() throws Exception { // prepare both registry files String installedContent = FIRST_PROP.concat("=").concat(INSTALLED); appendToInstalledRegistry(installedContent); String userContent = FIRST_PROP.concat("=").concat(USER); addUserRegistry(userContent); // test with empty pre an post properties Properties preProps = createPreProperties(); Properties postProps = createPostProperties(); PySystemState.initialize(preProps, postProps); Properties registry = PySystemState.registry; String first = registry.getProperty(FIRST_PROP, ANY); assertEquals(USER, first); }
public void testImportFromJava() { PySystemState.initialize(); PyObject submodule = imp.load("testpkg.submodule"); PyObject module = imp.load("testpkg"); module.__getattr__("test").__call__(); } }
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()); }
/** * 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
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()); }