/** * Read into the given PyObject that implements the Jython buffer API (with write access) or is a * PyArray. * * @param buf a PyObject compatible with the buffer API * @return the amount of data read as an int */ public int readinto(PyObject buf) { // This is an inefficient version of readinto: but readinto is // not recommended for use in Python 2.x anyway if (buf instanceof PyArray) { // PyArray has the buffer interface but it only works for bytes at present PyArray array = (PyArray) buf; String read = read(array.__len__()); for (int i = 0; i < read.length(); i++) { array.set(i, new PyString(read.charAt(i))); } return read.length(); } else if (buf instanceof BufferProtocol) { try (PyBuffer view = ((BufferProtocol) buf).getBuffer(PyBUF.FULL_RO)) { if (view.isReadonly()) { // More helpful than falling through to CPython message throw Py.TypeError("cannot read into read-only " + buf.getType().fastGetName()); } else { // Inefficiently, we have to go via a String String read = read(view.getLen()); int n = read.length(); for (int i = 0; i < n; i++) { view.storeAt((byte) read.charAt(i), i); } return read.length(); } } } // No valid alternative worked throw Py.TypeError("argument 1 must be read-write buffer, not " + buf.getType().fastGetName()); }
public PyFloat __float__() { PyType self_type = getType(); PyObject impl = self_type.lookup("__float__"); if (impl != null) { PyObject res = impl.__get__(this, self_type).__call__(); if (res instanceof PyFloat) return (PyFloat) res; throw Py.TypeError( "__float__" + " returned non-" + "float" + " (type " + res.getType().fastGetName() + ")"); } return super.__float__(); }
public String toString() { PyType self_type = getType(); PyObject impl = self_type.lookup("__repr__"); if (impl != null) { PyObject res = impl.__get__(this, self_type).__call__(); if (!(res instanceof PyString)) throw Py.TypeError( "__repr__ returned non-string (type " + res.getType().fastGetName() + ")"); return ((PyString) res).toString(); } return super.toString(); }
public PyObject __index__() { PyType self_type = getType(); PyObject impl = self_type.lookup("__index__"); if (impl != null) { PyObject res = impl.__get__(this, self_type).__call__(); if (res instanceof PyInteger || res instanceof PyLong) { return res; } throw Py.TypeError( String.format( "__index__ returned non-(int,long) (type %s)", res.getType().fastGetName())); } return super.__index__(); }
@Override public PyString __oct__() { PyType self_type = getType(); PyObject impl = self_type.lookup("__oct__"); if (impl != null) { PyObject res = impl.__get__(this, self_type).__call__(); if (res instanceof PyString) { return (PyString) res; } throw Py.TypeError( "__oct__" + " returned non-" + "string" + " (type " + res.getType().fastGetName() + ")"); } return super.__oct__(); }
public void dispatch__init__(PyType type, PyObject[] args, String[] keywords) { PyType self_type = getType(); if (self_type.isSubType(type)) { PyObject impl = self_type.lookup("__init__"); if (impl != null) { PyObject res = impl.__get__(this, self_type).__call__(args, keywords); if (res != Py.None) { throw Py.TypeError( String.format( "__init__() should return None, not '%.200s'", res.getType().fastGetName())); } proxyInit(); } } }