PyFunction handler = new PyFunction(new PyStringMap(), null, codeobj);
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(); } });
public PyObject lookup_error(String handlerName) { if (handlerName == null) { handlerName = "strict"; } PyObject handler = errorHandlers.__finditem__(handlerName.intern()); if (handler == null) { throw new PyException(Py.LookupError, "unknown error handler name '" + handlerName + "'"); } return handler; } }
public PyJavaPackage(String name,PackageManager mgr,String jarfile) { __file__ = jarfile; __name__ = name; if( mgr == null ) __mgr__ = PySystemState.packageManager; // default else __mgr__ = mgr; clsSet= new PyStringMap(); __dict__ = new PyStringMap(); __dict__.__setitem__("__name__", new PyString(__name__)); }
@ExposedNew final static PyObject stringmap_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { PyStringMap map = new PyStringMap(); map.stringmap_update(args, keywords); return map; }
/** * Add statically known classes. * * @param classes * their names as comma-separated string */ public void addPlaceholders(String classes) { StringTokenizer tok = new StringTokenizer(classes, ",@"); while (tok.hasMoreTokens()) { String p = tok.nextToken(); String name = p.trim().intern(); if (clsSet.__finditem__(name) == null) clsSet.__setitem__(name, Py.One); } }
/** * Basic helper implementation of {@link #doDir}. It merges information * from jpkg {@link PyJavaPackage#clsSet} and {@link PyJavaPackage#__dict__}. */ protected PyList basicDoDir(PyJavaPackage jpkg, boolean instantiate, boolean exclpkgs) { PyStringMap dict = jpkg.__dict__; PyStringMap cls = jpkg.clsSet; if (!instantiate) { PyList ret = cls.keys(); PyList dictKeys = dict.keys(); for (PyObject name : dictKeys.asIterable()) { if (!cls.has_key(name)) { if (exclpkgs && dict.get(name) instanceof PyJavaPackage) continue; ret.append(name); } } return ret; } for (PyObject pyname : cls.keys().asIterable()) { if (!dict.has_key(pyname)) { String name = pyname.toString(); jpkg.addClass(name, Py.findClass(jpkg.__name__ + "." + name)); } } return dict.keys(); }
return -2; int an = __len__(); int bn = other.__len__(); if (an < bn) { return 1; PyList akeys = keys(); PyList bkeys = null; if (other instanceof PyStringMap) { bkeys = ((PyStringMap)other).keys(); } else { bkeys = ((PyDictionary)other).keys(); return c; PyObject avalue = __finditem__(akey); PyObject bvalue = other.__finditem__(bkey); c = avalue._cmp(bvalue);
/** * Determines if there is a function with the given name and the correct number of arguments * in the loaded script. */ public boolean hasFunction(String name, int argsCount) { PyStringMap locals = (PyStringMap)pinterp.getLocals(); if (locals.has_key(name)) { // great, there is some variable with this name PyObject obj = locals.get(new PyString(name)); if (obj.getClass() == PyFunction.class) { // great, it's a function PyFunction fnc = (PyFunction)obj; int aCount = ((PyTableCode)fnc.func_code).co_argcount; if (aCount == argsCount) { // great, it accepts correct number of arguments return true; } boolean varargs = ((PyTableCode)fnc.func_code).varargs; if (aCount < argsCount && varargs) { // great, it is variable arguments function return true; } } } return false; }
@ExposedMethod(doc = BuiltinDocs.dict___getitem___doc) final PyObject stringmap___getitem__(PyObject key) { if (key instanceof PyString) { return __getitem__(((PyString)key).internedString()); } else { PyObject o = __finditem__(key); if (null == o) { throw Py.KeyError(key); } else { return o; } } }
@Override public Map<String, Object> getBindings() { Map<String, Object> bindings = new HashMap<String, Object>(); PyStringMap keyValueMap = (PyStringMap)interp.getLocals(); PyIterator keyValueSet = (PyIterator)keyValueMap.iteritems(); for (Object temp : keyValueSet) { PyTuple tempEntry = (PyTuple)temp; Iterator<PyObject> iter = tempEntry.iterator(); bindings.put((String)iter.next().__tojava__(String.class), iter.next()); } return bindings; }
private void merge(PyObject other) { if (other instanceof PyDictionary) { jmap.putAll(((PyDictionary) other).getMap()); } else if (other instanceof PyStringMap) { mergeFromKeys(other, ((PyStringMap) other).keys()); } else { mergeFromKeys(other, other.invoke("keys")); } }
@Override public PyObject __finditem__(PyObject key) { if (key instanceof PyString) { return __finditem__(((PyString)key).internedString()); } return table.get(key); }
public PyJavaPackage addPackage(String name, String jarfile) { int dot = name.indexOf('.'); String firstName=name; String lastName=null; if (dot != -1) { firstName = name.substring(0,dot); lastName = name.substring(dot+1, name.length()); } firstName = firstName.intern(); PyJavaPackage p = (PyJavaPackage)__dict__.__finditem__(firstName); if (p == null) { String pname = __name__.length() == 0 ? firstName : __name__+'.'+firstName; p = new PyJavaPackage(pname, __mgr__, jarfile); __dict__.__setitem__(firstName, p); } else { // this code is ok here, because this is not needed for // a top level package if (jarfile == null || !jarfile.equals(p.__file__)) p.__file__ = null; } if (lastName != null) return p.addPackage(lastName, jarfile); else return p; }
@Override public Map<String, Object> getBindings() { Map<String, Object> bindings = new HashMap<String, Object>(); PyStringMap keyValueMap = (PyStringMap)interp.getLocals(); PyIterator keyValueSet = (PyIterator)keyValueMap.iteritems(); for (Object temp : keyValueSet) { PyTuple tempEntry = (PyTuple)temp; Iterator<PyObject> iter = tempEntry.iterator(); bindings.put((String)iter.next().__tojava__(String.class), iter.next()); } return bindings; }
/** * Merge another PyObject that supports keys() with this * dict. * * @param other a PyObject with a keys() method */ private void merge(PyObject other) { if (other instanceof PyDictionary) { getMap().putAll(((PyDictionary) other).getMap()); } else if (other instanceof PyStringMap) { mergeFromKeys(other, ((PyStringMap)other).keys()); } else { mergeFromKeys(other, other.invoke("keys")); } }
private void ensureDict() { if (__dict__ == null) { __dict__ = new PyStringMap(); } }
@ExposedMethod(defaults = "Py.None", doc = BuiltinDocs.dict_get_doc) final PyObject stringmap_get(PyObject key, PyObject defaultObj) { PyObject obj = __finditem__(key); return obj == null ? defaultObj : obj; }
public static PyString intern(PyObject obj) { if (!(obj instanceof PyString) || obj instanceof PyUnicode) { throw Py.TypeError("intern() argument 1 must be string, not " + obj.getType().fastGetName()); } if (obj.getType() != PyString.TYPE) { throw Py.TypeError("can't intern subclass of string"); } PyString s = (PyString)obj; String istring = s.internedString(); PyObject ret = internedStrings.__finditem__(istring); if (ret != null) { return (PyString)ret; } internedStrings.__setitem__(istring, s); return s; }