private static boolean isUnicodeError(PyObject exc) { return Py.isInstance(exc, Py.UnicodeDecodeError) || Py.isInstance(exc, Py.UnicodeEncodeError) || Py.isInstance(exc, Py.UnicodeTranslateError); }
public static boolean isinstance(PyObject obj, PyObject cls) { return Py.isInstance(obj, cls); }
/** * Ensure a PyString value for UnicodeErrors * * @param attr a PyObject * @param name of the attribute * @return an PyString */ public static PyString getString(PyObject attr, String name) { if (!Py.isInstance(attr, PyString.TYPE)) { throw Py.TypeError(String.format("%.200s attribute must be str", name)); } return (PyString)attr; }
public static PyObject strict_errors(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("strict_errors", args, kws, "exc"); PyObject exc = ap.getPyObject(0); if (Py.isInstance(exc, Py.UnicodeDecodeError)) { throw new PyException(Py.UnicodeDecodeError, exc); } else if (Py.isInstance(exc, Py.UnicodeEncodeError)) { throw new PyException(Py.UnicodeEncodeError, exc); } else if (Py.isInstance(exc, Py.UnicodeTranslateError)) { throw new PyException(Py.UnicodeTranslateError, exc); } throw wrong_exception_type(exc); }
@Override public PyObject __call__(PyObject arg1, PyObject arg2) { PyObject formatted = arg1.__format__(arg2); if (!Py.isInstance(formatted, PyString.TYPE) && !Py.isInstance(formatted, PyUnicode.TYPE) ) { throw Py.TypeError("instance.__format__ must return string or unicode, not " + formatted.getType().fastGetName()); } return formatted; } }
public void setName(PyObject value) { if (value == null || !Py.isInstance(value, PyString.TYPE)) { throw Py.TypeError("__name__ must be a string object"); } String name = value.toString(); if (name.contains("\u0000")) { throw Py.TypeError("__name__ must not contain null bytes"); } __name__ = name; }
@Override public String toString() { PyObject mod = __dict__.__finditem__("__module__"); String modStr = (mod == null || !Py.isInstance(mod, PyString.TYPE)) ? "?" : mod.toString(); return String.format("<class %s.%s at %s>", modStr, __name__, Py.idstr(this)); }
private static char toChar(String name, PyObject src, char dflt) { if (src == null) { return dflt; } boolean isStr = Py.isInstance(src, PyString.TYPE); if (src == Py.None || isStr && src.__len__() == 0) { return '\0'; } else if (!isStr || src.__len__() != 1) { throw Py.TypeError(String.format("\"%s\" must be an 1-character string", name)); } return src.toString().charAt(0); }
/** * If a class doesn't define a __repr__ method of its own, the return * value from this method is used. */ protected PyString makeDefaultRepr() { PyObject mod = instclass.__dict__.__finditem__("__module__"); String modStr = (mod == null || !Py.isInstance(mod, PyString.TYPE)) ? "?" : mod.toString(); return new PyString(String.format("<%s.%s instance at %s>", modStr, instclass.__name__, Py.idstr(this))); }
private static PyObject checkedForType(PyObject arg, int pos, PyType type) { if (arg == null || Py.isInstance(arg, type)) return arg; throw Py.TypeError(String.format("argument %d must be %s, not %s", pos + 1, type.fastGetName(), arg.getType().fastGetName())); }
private static PyObject getNumber(PyObject obj) { if (Py.isInstance(obj, getNumberClass())) { return obj; } try { PyObject intObj = obj.__int__(); if (Py.isInstance(obj, getNumberClass())) { return intObj; } throw Py.TypeError("a number is required"); } catch (PyException exc) { if (exc.match(Py.ValueError)) { throw Py.TypeError("a number is required"); } throw exc; } }
ok = false; } else { ok = Py.isInstance(self, im_class);
public static PyObject dir(PyObject o) { PyObject ret = o.__dir__(); if (!Py.isInstance(ret, PyList.TYPE)) { throw Py.TypeError("__dir__() must return a list, not " + ret.getType().fastGetName()); } ((PyList)ret).sort(); return ret; }
public static PyObject filterBaseString(PyObject func, PyBaseString seq, PyType stringType) { if (func == Py.None && seq.getType() == stringType) { // If it's a real string we can return the original, as no character is ever // false and __getitem__ does return this character. If it's a subclass we // must go through the __getitem__ loop return seq; } StringBuilder builder = new StringBuilder(); for (PyObject item : seq.asIterable()) { if (func == Py.None) { if (!item.__nonzero__()) { continue; } } else if (!func.__call__(item).__nonzero__()) { continue; } if (!Py.isInstance(item, stringType)) { String name = stringType.fastGetName(); throw Py.TypeError(String.format("can't filter %s to %s: __getitem__ returned " + "different type", name, name)); } builder.append(item.toString()); } String result = builder.toString(); return stringType == PyString.TYPE ? new PyString(result) : new PyUnicode(result); }
public static boolean isInstance(PyObject inst, PyObject cls) { // Quick test for an exact match if (inst.getType() == cls) { return true; } if (cls instanceof PyTuple) { for (PyObject item : cls.asIterable()) { if (isInstance(inst, item)) { return true; } } return false; } PyObject checkerResult; if ((checkerResult = dispatchToChecker(inst, cls, "__instancecheck__")) != null) { return checkerResult.__nonzero__(); } return recursiveIsInstance(inst, cls); }
public static PyObject replace_errors(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("replace_errors", args, kws, "exc"); PyObject exc = ap.getPyObject(0); if (Py.isInstance(exc, Py.UnicodeEncodeError)) { int end = exceptions.getEnd(exc, true); return new PyTuple(new PyUnicode("?"), Py.newInteger(end)); } else if (Py.isInstance(exc, Py.UnicodeDecodeError)) { int end = exceptions.getEnd(exc, false); return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER), Py.newInteger(end)); } else if (Py.isInstance(exc, Py.UnicodeTranslateError)) { int end = exceptions.getEnd(exc, true); return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER), Py.newInteger(end)); } throw wrong_exception_type(exc); }
private static void ensureFromList(PyObject mod, PyObject fromlist, String name, boolean recursive) { if (mod.__findattr__("__path__") == null) { return; } //This can happen with imports like "from . import foo" if (name.length() == 0) { name = mod.__findattr__("__name__").toString(); } StringBuilder modNameBuffer = new StringBuilder(name); for (PyObject item : fromlist.asIterable()) { if (!Py.isInstance(item, PyBaseString.TYPE)) { throw Py.TypeError("Item in ``from list'' not a string"); } if (item.toString().equals("*")) { if (recursive) { // Avoid endless recursion continue; } PyObject all; if ((all = mod.__findattr__("__all__")) != null) { ensureFromList(mod, all, name, true); } } if (mod.__findattr__((PyString) item) == null) { String fullName = modNameBuffer.toString() + "." + item.toString(); import_next(mod, modNameBuffer, item.toString(), fullName, null); } } }
public static PyObject xmlcharrefreplace_errors(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("xmlcharrefreplace_errors", args, kws, "exc"); PyObject exc = ap.getPyObject(0); if (!Py.isInstance(exc, Py.UnicodeEncodeError)) { throw wrong_exception_type(exc); } int start = ((PyInteger)exc.__getattr__("start")).getValue(); int end = ((PyInteger)exc.__getattr__("end")).getValue(); String object = exc.__getattr__("object").toString(); StringBuilder replacement = new StringBuilder(); xmlcharrefreplace_internal(start, end, object, replacement); return new PyTuple(Py.java2py(replacement.toString()), exc.__getattr__("end")); }
public static PyObject backslashreplace_errors(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("backslashreplace_errors", args, kws, "exc"); PyObject exc = ap.getPyObject(0); if (!Py.isInstance(exc, Py.UnicodeEncodeError)) { throw wrong_exception_type(exc); } int start = ((PyInteger)exc.__getattr__("start")).getValue(); int end = ((PyInteger)exc.__getattr__("end")).getValue(); String object = exc.__getattr__("object").toString(); StringBuilder replacement = new StringBuilder(); backslashreplace_internal(start, end, object, replacement); return new PyTuple(Py.java2py(replacement.toString()), exc.__getattr__("end")); }
final private boolean save_pers(PyObject object, PyObject pers_func) { PyObject pid = pers_func.__call__(object); if (pid == Py.None) { return false; } if (protocol == 0) { if (!Py.isInstance(pid, PyString.TYPE)) { throw new PyException(PicklingError, "persistent id must be string"); } file.write(PERSID); file.write(pid.toString()); file.write("\n"); } else { save(pid, true); file.write(BINPERSID); } return true; }