public PyStringMap(PyObject elements[]) { this(elements.length); for (int i = 0; i < elements.length; i += 2) { __setitem__(elements[i], elements[i + 1]); } }
public PyObject addClass(String name, Class<?> c) { PyObject ret = Py.java2py(c); __dict__.__setitem__(name.intern(), ret); return ret; }
/** * 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); } }
public void register_error(String name, PyObject error) { if (!error.isCallable()) { throw Py.TypeError("argument must be callable"); } errorHandlers.__setitem__(name.intern(), error); }
@ExposedMethod(doc = BuiltinDocs.dict___setitem___doc) final void stringmap___setitem__(PyObject key, PyObject value) { if (value == null) { table.remove(pyToKey(key)); } else if (key instanceof PyString) { __setitem__(((PyString)key).internedString(), value); } else { table.put(key, value); } }
/** * Merge another PyObject via its keys() method * * @param other a PyObject with a keys() method * @param keys the result of other's keys() method */ private void mergeFromKeys(PyObject other, PyObject keys) { for (PyObject key : keys.asIterable()) { __setitem__(key, other.__getitem__(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; }
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__)); }
locals.__setitem__("__name__", new PyString(filename)); locals.__setitem__("zipfile", Py.java2py(zip));
/** * Convenience method for constructing a type object of a Python exception, named as given, and * added to the namespace of the "_io" module. * * @param dict module dictionary * @param excname name of the exception * @param bases one or more bases (superclasses) * @return the constructed exception type */ private static PyType makeException(PyObject dict, String excname, PyObject... bases) { PyStringMap classDict = new PyStringMap(); classDict.__setitem__("__module__", Py.newString("_io")); PyType type = (PyType)Py.makeClass(excname, bases, classDict); dict.__setitem__(excname, type); return type; }
/** * Insert all the key:value pairs from <code>dict</code> into this mapping. */ @ExposedMethod(doc = BuiltinDocs.dict_update_doc) final void stringmap_update(PyObject[] args, String[] keywords) { int nargs = args.length - keywords.length; if (nargs > 1) { throw PyBuiltinCallable.DefaultInfo.unexpectedCall(nargs, false, "update", 0, 1); } if (nargs == 1) { PyObject arg = args[0]; if (arg.__findattr__("keys") != null) { merge(arg); } else { mergeFromSeq(arg); } } for (int i = 0; i < keywords.length; i++) { __setitem__(keywords[i], args[nargs + i]); } }
/** * Merge any iterable object producing iterable objects of length * 2 into this dict. * * @param other another PyObject */ private void mergeFromSeq(PyObject other) { PyObject pairs = other.__iter__(); PyObject pair; for (int i = 0; (pair = pairs.__iternext__()) != null; i++) { try { pair = PySequence.fastSequence(pair, ""); } catch(PyException pye) { if (pye.match(Py.TypeError)) { throw Py.TypeError(String.format("cannot convert dictionary update sequence " + "element #%d to a sequence", i)); } throw pye; } int n; if ((n = pair.__len__()) != 2) { throw Py.ValueError(String.format("dictionary update sequence element #%d " + "has length %d; 2 is required", i, n)); } __setitem__(pair.__getitem__(0), pair.__getitem__(1)); } }
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; }
public PyTuple lookup(String encoding) { PyString v = new PyString(normalizestring(encoding)); PyObject cached = searchCache.__finditem__(v); if (cached != null) { return (PyTuple)cached; } if (searchPath.__len__() == 0) { throw new PyException(Py.LookupError, "no codec search functions registered: can't find encoding '" + encoding + "'"); } for (PyObject func : searchPath.asIterable()) { PyObject created = func.__call__(v); if (created == Py.None) { continue; } if (!(created instanceof PyTuple) || created.__len__() != 4) { throw Py.TypeError("codec search functions must return 4-tuples"); } searchCache.__setitem__(v, created); return (PyTuple)created; } throw new PyException(Py.LookupError, "unknown encoding '" + encoding + "'"); }
fastGetDict().__setitem__("name", file);