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 ImmutableMap<String, Function> call() throws Exception { // This creates a dependency between function and the client. // However, we need to know the load paths before we can do anything, so this is necessary. PythonUtils.initializePython(); Py.getSystemState().path.append(new PyString(file.getParentFile().getCanonicalPath())); PythonInterpreter interpreter = new PythonInterpreter(); try { interpreter.execfile(file.getCanonicalPath()); } catch (IOException e) { throw new LoadException(file, e); } catch (PyException e) { throw new LoadException(file, e); } PyStringMap map = (PyStringMap) interpreter.getLocals(); ImmutableMap.Builder<String, Function> builder = ImmutableMap.builder(); for (Object key : map.keys()) { Object o = map.get(Py.java2py(key)); if (o instanceof PyFunction) { String name = (String) key; Function f = new PythonFunction(name, (PyFunction) o); builder.put(name, f); } } return builder.build(); } });
/** * Returns the banner to print before the first interaction: "Jython <version> on <platform>". * * @return the banner. */ public static String getDefaultBanner() { return String .format("Jython %s on %s", PySystemState.version, Py.getSystemState().platform); }
public FunctionThread(PyObject func, PyObject[] args, long stack_size, ThreadGroup group) { super(group, null, "Thread", stack_size); this.func = func; this.args = args; this.systemState = Py.getSystemState(); this.setName("Thread-"+Integer.toString(counter.incrementAndGet())); }
/** * Acquires the interpreter's import lock for the current thread. * * This lock should be used by import hooks to ensure * thread-safety when importing modules. * */ public static void acquire_lock() { Py.getSystemState().getImportLock().lock(); }
/** * Return true if the import lock is currently held, else false. * * @return true if the import lock is currently held, else false. */ public static boolean lock_held() { return Py.getSystemState().getImportLock().isHeldByCurrentThread(); } }
private static void initReaperThread() { RefReaper reaper = new RefReaper(); PySystemState systemState = Py.getSystemState(); systemState.registerCloser(reaper); reaperThread = new Thread(reaper, "weakref reaper"); reaperThread.setDaemon(true); reaperThread.start(); }
public boolean packageExists(String pkg, String name) { if (packageExists(this.searchPath, pkg, name)) { return true; } PySystemState system = Py.getSystemState(); if (system.getClassLoader() == null && packageExists(Py.getSystemState().path, pkg, name)) { return true; } return false; }
/** * Release the interpreter's import lock. * */ public static void release_lock() { try{ Py.getSystemState().getImportLock().unlock(); }catch(IllegalMonitorStateException e){ throw Py.RuntimeError("not holding the import lock"); } }
@Override public void installScriptSourceProvider(ScriptEngine scriptEngine, ScriptSourceProvider provider) { PySystemState sys = Py.getSystemState(); if (sys != null) { sys.path_hooks.append(new PythonSourceProvider(provider)); } }
/** Check <code>sys.stdout.fileno()</code> is acceptable to <code>_io.open()</code> */ @Test public void openStdoutByFileno() throws IOException { PySystemState sys = Py.getSystemState(); openByFilenoTest(sys.stdout, "wb"); }
/** Check <code>sys.stderr.fileno()</code> is acceptable to <code>_io.open()</code> */ @Test public void openStderrByFileno() throws IOException { PySystemState sys = Py.getSystemState(); openByFilenoTest(sys.stderr, "wb"); }
/** Check <code>sys.stdin.fileno()</code> is acceptable to <code>_io.open()</code> */ @Test public void openStdinByFileno() throws IOException { PySystemState sys = Py.getSystemState(); openByFilenoTest(sys.stdin, "rb"); }
/** Check <code>PyFile(OutputStream).fileno()</code> is acceptable to <code>_io.open()</code> */ @Test public void openPyFileOStreamByFileno() throws IOException { PySystemState sys = Py.getSystemState(); OutputStream ostream = new FileOutputStream(FILE1); PyFile file = new PyFile(ostream); openByFilenoTest(file, "wb"); }
static PyObject loadFromLoader(PyObject importer, String name) { PyUnicode.checkEncoding(name); PyObject load_module = importer.__getattr__("load_module"); ReentrantLock importLock = Py.getSystemState().getImportLock(); importLock.lock(); try { return load_module.__call__(new PyObject[]{new PyString(name)}); } finally { importLock.unlock(); } }
/** Check <code>PyFile().fileno()</code> is acceptable to <code>_io.open()</code> */ @Test public void openPyFileByFileno() throws IOException { PySystemState sys = Py.getSystemState(); PyFile file = new PyFile(FILE1, "w", 1); openByFilenoTest(file, "wb"); }
public static PyObject createFromSource(String name, InputStream fp, String filename, String outFilename, long mtime) { byte[] bytes = compileSource(name, fp, filename, mtime); if (!Py.getSystemState().dont_write_bytecode) { outFilename = cacheCompiledSource(filename, outFilename, bytes); } Py.writeComment(IMPORT_LOG, "'" + name + "' as " + filename); PyCode code = BytecodeLoader.makeCode(name + "$py", bytes, filename); return createFromCode(name, code, filename); }
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; } PyObject currentBuiltins = Py.getSystemState().getBuiltins(); currentBuiltins.__setitem__("_", Py.None); Py.stdout.println(o.__repr__()); currentBuiltins.__setitem__("_", o); }
public PyList doDir(PyJavaPackage jpkg, boolean instantiate, boolean exclpkgs) { PyList basic = basicDoDir(jpkg, instantiate, exclpkgs); PyList ret = new PyList(); doDir(this.searchPath, ret, jpkg, instantiate, exclpkgs); PySystemState system = Py.getSystemState(); if (system.getClassLoader() == null) { doDir(system.path, ret, jpkg, instantiate, exclpkgs); } return merge(basic, ret); }
private void module___init__(PyObject name, PyObject doc) { ensureDict(); __dict__.__setitem__("__name__", name); __dict__.__setitem__("__doc__", doc); if (name.equals(new PyString("__main__"))) { __dict__.__setitem__("__builtins__", Py.getSystemState().modules.__finditem__("__builtin__")); __dict__.__setitem__("__package__", Py.None); } }