private PyObject getattr(PyObject obj, PyObject name) { // XXX: We should probably have a PyObject.__getattr__(PyObject) that does // this. This is different than __builtin__.getattr (in how it handles // exceptions) String nameStr = ensureStringAttribute(name); String[] components = nameStr.split("\\."); for (String component : components) { obj = obj.__getattr__(component.intern()); } return obj; }
private static PyException wrong_exception_type(PyObject exc) { PyObject excClass = exc.__getattr__("__class__"); PyObject className = excClass.__getattr__("__name__"); return new PyException(Py.TypeError, "Don't know how to handle " + className + " in error callback"); }
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { in.defaultReadObject(); String module = in.readUTF(); String name = in.readUTF(); /* Check for types and missing members here */ //System.out.println("module: "+module+", "+name); PyObject mod = imp.importName(module.intern(), false); PyClass pyc = (PyClass) mod.__getattr__(name.intern()); instclass = pyc; }
static CType typeOf(PyObject obj) { if (obj instanceof CType) { return (CType) obj; } else if (obj == Py.None) { return CType.VOID; } PyObject jffi_type = obj.__getattr__("_jffi_type"); if (!(jffi_type instanceof CType)) { throw Py.TypeError("invalid _jffi_type"); } return (CType) jffi_type; }
/** * Shortcut for calling a method on a PyObject with no args. * * @param name the name of the method to call. This must be an * interned string! * @return the result of calling the method name with no args **/ public PyObject invoke(String name) { PyObject f = __getattr__(name); return f.__call__(); }
private static PyObject initExc(String name, PyObject exceptions, PyObject dict) { PyObject tmp = exceptions.__getattr__(name); dict.__setitem__(name, tmp); return tmp; }
private static synchronized PyObject get_opname() { if (opname == null) { opname = get_dis().__getattr__("opname"); } return opname; } private boolean debug;
public static PyObject popen(PyObject[] args, String[] kwds) { // XXX: popen lives in posix in 2.x, but moves to os in 3.x. It's easier for us to // keep it in os // import os; return os.popen(*args, **kwargs) return imp.load("os").__getattr__("popen").__call__(args, kwds); }
public static PyObject vars(PyObject o) { try { return o.__getattr__("__dict__"); } catch (PyException e) { if (e.match(Py.AttributeError)) { throw Py.TypeError("vars() argument must have __dict__ attribute"); } throw e; } }
private Object readResolve() { if (underlying_class != null) { return PyType.fromClass(underlying_class, false); } PyObject mod = imp.importName(module.intern(), false); PyObject pytyp = mod.__getattr__(name.intern()); if (!(pytyp instanceof PyType)) { throw Py.TypeError(module + "." + name + " must be a type for deserialization"); } return pytyp; } }
public static PyObject newDate(Date date) { if (date == null) { return Py.None; } PyObject datetimeModule = __builtin__.__import__("datetime"); PyObject dateClass = datetimeModule.__getattr__("date"); Calendar cal = Calendar.getInstance(); cal.setTime(date); return dateClass.__call__(newInteger(cal.get(Calendar.YEAR)), newInteger(cal.get(Calendar.MONTH) + 1), newInteger(cal.get(Calendar.DAY_OF_MONTH))); }
/** * @deprecated Use jnr.constants.platform.Errno.valueOf(code).toString() (or * os.strerror from Python) instead. */ @Deprecated public static PyObject strerror(PyObject code) { Py.warning(Py.DeprecationWarning, "The errno.strerror function is deprecated, use os.strerror."); return imp.load("os").__getattr__("strerror").__call__(code); } }
private PyObject join_list(PyList list, PyString string) { PyObject joiner = string.__getslice__(Py.Zero, Py.Zero); if (list.size() == 0) { return joiner; } return joiner.__getattr__("join").__call__(list); }
public static void unsetenv(String key) { // import os; try: del os.environ[key]; except KeyError: pass PyObject environ = imp.load("os").__getattr__("environ"); try { environ.__delitem__(key); } catch (PyException pye) { if (!pye.match(Py.KeyError)) { throw pye; } } }
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(); } }
public PyObject expand(PyObject[] args) { if(args.length == 0) { throw Py.TypeError("expand() takes exactly 1 argument (0 given)"); } PyObject mod = imp.importName("re", true); PyObject func = mod.__getattr__("_expand"); return func.__call__(new PyObject[] {pattern, this, args[0]}); }
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); } }
public static PyString _UnpickleableError__str__(PyObject self, PyObject[] args, String[] kwargs) { PyObject selfArgs = self.__getattr__("args"); PyObject a = selfArgs.__len__() > 0 ? selfArgs.__getitem__(0) : new PyString("(what)"); return new PyString("Cannot pickle %s objects").__mod__(a).__str__(); }
public static PyString _PickleError__str__(PyObject self, PyObject[] args, String[] kwargs) { PyObject selfArgs = self.__getattr__("args"); if (selfArgs.__len__() > 0 && selfArgs.__getitem__(0).__len__() > 0) { return selfArgs.__getitem__(0).__str__(); } else { return new PyString("(what)"); } }
private void load_newobj() { PyObject arg_tup = pop(); PyObject cls = pop(); PyObject[] args = new PyObject[arg_tup.__len__() + 1]; args [0] = cls; for(int i=1; i<args.length; i++) { args [i] = arg_tup.__finditem__(i-1); } push(cls.__getattr__("__new__").__call__(args)); }