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(); } });
/** * Helper merging list2 into list1. Returns list1. */ protected PyList merge(PyList list1, PyList list2) { for (PyObject name : list2.asIterable()) { list1.append(name); } return list1; }
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())); } }
private static void pushBlock(PyFrame f, PyTryBlock block) { if (f.f_exits == null) { // allocate in the frame where they can fit! TODO consider supporting directly in the frame f.f_exits = new PyObject[1]; // f_blockstack in CPython - a simple ArrayList might be best f.f_exits[0] = new PyList(); } ((PyList)f.f_exits[0]).append(block); }
private void parse_save_field() { PyObject field; field = new PyString(this.field.toString()); if (numeric_field) { numeric_field = false; field = field.__float__(); } fields.append(field); // XXX: fastest way to clear StringBuffer? this.field = new StringBuffer(INITIAL_BUILDER_CAPACITY); }
public void initialize(Properties preProperties, Properties postProperties, String[] argv, ClassLoader classLoader, ExtensiblePyObjectAdapter adapter) { postProperties.put(PySystemState.PYTHON_CACHEDIR_SKIP, "true"); PySystemState defaultState = PySystemState.doInitialize(preProperties, postProperties, argv, classLoader, adapter); defaultState.path.append(Py.newString("/from_SyspathAppendingInitializer_with_love")); } }
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; }
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; }
private static PyList initArgv(String[] args) { PyList argv = new PyList(); if (args != null) { for (String arg : args) { argv.append(Py.newStringOrUnicode(arg)); } } return argv; }
@Override public void installScriptSourceProvider(ScriptEngine scriptEngine, ScriptSourceProvider provider) { PySystemState sys = Py.getSystemState(); if (sys != null) { sys.path_hooks.append(new PythonSourceProvider(provider)); } }
public void register(PyObject search_function) { if (!search_function.isCallable()) { throw Py.TypeError("argument must be callable"); } searchPath.append(search_function); }
void doUnpack(ByteStream buf, int count, PyList list) { while (count-- > 0) list.append(Py.java2py(unpack(buf))); }
void doUnpack(ByteStream buf, int count, PyList list) { list.append(Py.newString(buf.readString(count))); } }
@ExposedMethod(doc = BuiltinDocs.type___subclasses___doc) public synchronized final PyObject type___subclasses__() { PyList result = new PyList(); cleanup_subclasses(); for (WeakReference<PyType> ref : subclasses) { PyType subtype = ref.get(); if (subtype == null) continue; result.append(subtype); } return result; }
private static PyList initPath(Properties props, boolean standalone, String jarFileName) { PyList path = new PyList(); addPaths(path, props.getProperty("python.path", "")); if (prefix != null) { String libpath = new File(prefix.toString(), "Lib").toString(); path.append(new PyString(libpath)); } if (standalone) { // standalone jython: add the /Lib directory inside JYTHON_JAR to the path addPaths(path, jarFileName + "/Lib"); } return path; }
final private void load_persid(PyObject pid) { if (persistent_load == null) { throw new PyException(UnpicklingError, "A load persistent id instruction was encountered,\n" + "but no persistent_load function was specified."); } if (persistent_load instanceof PyList) { ((PyList)persistent_load).append(pid); } else { pid = persistent_load.__call__(pid); } push(pid); }
protected static PythonInterpreter createInterpreter(ServletContext servletContext) { String rootPath = getRootPath(servletContext); PySystemState sys = new PySystemState(); PythonInterpreter interp = new PythonInterpreter(Py.newStringMap(), sys); sys.path.append(new PyString(rootPath)); String modulesDir = rootPath + "WEB-INF" + File.separator + "jython"; sys.path.append(new PyString(modulesDir)); return interp; }
public GenerateDDSIDL() { // Import em.py PySystemState systemState = Py.getSystemState(); systemState.path.append(new PyString("__pyclasspath__/Lib")); interp.exec("from rosidl_generator_dds_idl import generate_dds_idl"); }
public boolean enterRepr(PyObject obj) { if (reprStack == null) { reprStack = new PyList(new PyObject[] {obj}); return true; } for (int i = reprStack.size() - 1; i >= 0; i--) { if (obj == reprStack.pyget(i)) { return false; } } reprStack.append(obj); return true; }
final private void load_append() { PyObject value = pop(); PyObject obj = peek(); if(obj instanceof PyList) { ((PyList)obj).append(value); } else { PyObject appender = obj.__getattr__("append"); appender.__call__(value); } }