public void append_json_repr(StringBuffer buf, PyObject py_obj) throws JSONEncodeError { if (py_obj instanceof PyString) append_json_string_repr(buf, ((PyString) py_obj).toString()); // Must test for PyBoolean before PyInteger because former is a subclass of latter. else if (py_obj instanceof PyBoolean) buf.append(((PyBoolean) py_obj).getBooleanValue() ? "true" : "false"); else if (py_obj instanceof PyInteger) buf.append(Integer.toString(((PyInteger) py_obj).getValue())); else if (py_obj instanceof PyLong) { String repr = ((PyLong) py_obj).__repr__().toString(); buf.append(repr.substring(0, repr.length() - 1)); } else if (py_obj instanceof PyFloat) buf.append(Double.toString(((PyFloat) py_obj).getValue())); else if (py_obj instanceof PyStringMap) append_json_string_map_repr(buf, (PyStringMap) py_obj); else if (py_obj instanceof PyDictionary) append_json_dictionary_repr(buf, (PyDictionary) py_obj); else if (py_obj instanceof PySequence) append_json_sequence_repr(buf, (PySequence) py_obj); else if (py_obj instanceof PyNone) buf.append("null"); else if (py_obj.__findattr__("__json__") != null && py_obj.__findattr__("__json__").isCallable()) buf.append(((PyMethod) py_obj.__findattr__("__json__")).__call__().toString()); else throw new JSONEncodeError( "Python '" + ((PyType) py_obj.fastGetClass()).fastGetName() + "' object '" + py_obj.__repr__() + "' is not encodable in JSON"); }
public static PyString SyntaxError__str__(PyObject self, PyObject[] arg, String[] kwargs) { PyObject msg = self.__getattr__("msg"); PyObject str = msg.__str__(); if (!(msg instanceof PyString)) { return Py.newString(str.toString()); } PyObject filename = self.__findattr__("filename"); PyObject lineno = self.__findattr__("lineno"); boolean haveFilename = filename instanceof PyString; boolean haveLieno = lineno instanceof PyInteger; if (!haveFilename && !haveLieno) { return (PyString) str; } String result; if (haveFilename && haveLieno) { result = String.format("%s (%s, line %d)", str, basename(filename.toString()), lineno.asInt()); } else if (haveFilename) { result = String.format("%s (%s)", str, basename(filename.toString())); } else { result = String.format("%s (line %d)", str, lineno.asInt()); } return Py.newString(result); }
/** * Initialize all __slots__ arguments in the specified dict to None. * * @param self a PyObject dict */ private static void initSlots(PyObject self) { for (PyObject name : self.__findattr__("__slots__").asIterable()) { if (!(name instanceof PyString)) { continue; } self.__setattr__((PyString) name, Py.None); } }
public static PyObject EnvironmentError__str__(PyObject self, PyObject[] args, String[] kwargs) { PyObject errno = self.__findattr__("errno"); PyObject strerror = self.__findattr__("strerror"); PyObject filename = self.__findattr__("filename"); PyString result; if (filename.__nonzero__()) { result = Py.newString("[Errno %s] %s: %s"); result = (PyString) result.__mod__(new PyTuple(errno, strerror, filename.__repr__())); } else if (errno.__nonzero__() && strerror.__nonzero__()) { result = Py.newString("[Errno %s] %s"); result = (PyString) result.__mod__(new PyTuple(errno, strerror)); } else { return PyBaseException.TYPE.invoke("__str__", self, args, kwargs); } return result; }
public static PyObject EnvironmentError__reduce__( PyObject self, PyObject[] args, String[] kwargs) { PyBaseException selfBase = (PyBaseException) self; PyObject reduceArgs = selfBase.args; PyObject filename = self.__findattr__("filename"); // self->args is only the first two real arguments if there was a file name given // to EnvironmentError if (selfBase.args.__len__() == 2 && filename != null) { reduceArgs = new PyTuple(selfBase.args.__finditem__(0), selfBase.args.__finditem__(1), filename); } if (selfBase.__dict__ != null) { return new PyTuple(selfBase.getType(), reduceArgs, selfBase.__dict__); } else { return new PyTuple(selfBase.getType(), reduceArgs); } }
public PyObject __findattr__(String name) { PyObject ret = super.__findattr__(name); if (ret != null) return ret; return im_func.__findattr__(name); }