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); }
/** * Helper merging list2 into list1. Returns list1. */ protected PyList merge(PyList list1, PyList list2) { for (PyObject name : list2.asIterable()) { list1.append(name); } return list1; }
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(); } });
private void parse_reset() { fields = new PyList(); state = ParserState.START_RECORD; numeric_field = false; }
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; } }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) final synchronized PyObject list___add__(PyObject o) { PyList sum = null; if (o instanceof PySequenceList && !(o instanceof PyTuple)) { if (o instanceof PyList) { List oList = ((PyList) o).list; List newList = new ArrayList(list.size() + oList.size()); newList.addAll(list); newList.addAll(oList); sum = fromList(newList); } } else if (!(o instanceof PySequenceList)) { // also support adding java lists (but not PyTuple!) Object oList = o.__tojava__(List.class); if (oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyList(); sum.list_extend(this); for (Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } } } return sum; }
@ExposedMethod public PyObject scope_keys() { PyList members = new PyList(); List<Integer> scopes = context.getScopes(); for (int scope : scopes) { Bindings bindings = context.getBindings(scope); if (bindings == null) continue; for (String key : bindings.keySet()) members.append(new PyString(key)); } members.sort(); return members; }
@ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) final void list___init__(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); if (seq == null) { return; } if (seq instanceof PyList) { list.addAll(((PyList) seq).list); // don't convert } else if (seq instanceof PyTuple) { list.addAll(((PyTuple) seq).getList()); } else if (seq.getClass().isAssignableFrom(Collection.class)) { System.err.println("Adding from collection"); addCollection(list, (Collection)seq); } else { for (PyObject item : seq.asIterable()) { append(item); } } }
PySystemState systemState = Py.getSystemState(); PyList warnoptions = new PyList(); addWarnings(opts.warnoptions, warnoptions); if (!Options.ignore_environment) { if (warnoptions.size() > 0) { imp.load("warnings"); path = ""; Py.getSystemState().path.insert(0, Py.newStringOrUnicode(path)); if (opts.jar) { try { Py.getSystemState().path.insert(0, Py.EmptyString);
@Override protected synchronized PyObject repeat(int count) { if (count < 0) { count = 0; } int size = size(); int newSize = size * count; if (count != 0 && newSize / count != size) { throw Py.MemoryError(""); } PyObject[] elements = list.toArray(new PyObject[size]); PyObject[] newList = new PyObject[newSize]; for (int i = 0; i < count; i++) { System.arraycopy(elements, 0, newList, i * size, size); } return new PyList(newList); }
@ExposedMethod(doc = BuiltinDocs.list___len___doc) final synchronized int list___len__() { return size(); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___imul___doc) final synchronized PyObject list___imul__(PyObject o) { if (!o.isIndex()) { return null; } int count = o.asIndex(Py.OverflowError); int size = size(); if (size == 0 || count == 1) { return this; } if (count < 1) { clear(); return this; } if (size > Integer.MAX_VALUE / count) { throw Py.MemoryError(""); } int newSize = size * count; if (list instanceof ArrayList) { ((ArrayList) list).ensureCapacity(newSize); } List<PyObject> oldList = new ArrayList<PyObject>(list); for (int i = 1; i < count; i++) { list.addAll(oldList); } gListAllocatedStatus = list.size(); // now omit? return this; }
public int index(PyObject o, int start) { return list_index(o, start, size()); }