/** * Convenience method to create (for the caller to throw) a * <code>BufferError("{operation} operation forbidden on released buffer object")</code>. * * @param operation name of operation or null * @return the error as a PyException */ protected static PyException bufferReleased(String operation) { String op = (operation == null) ? "" : operation + " "; return Py.BufferError(op + "operation forbidden on released buffer object"); }
/** * Convenience method to create (for the caller to throw) a * <code>BufferError("underlying buffer is not {property}")</code>. * * @param property * @return the error as a PyException */ protected static PyException bufferIsNot(String property) { return Py.BufferError("underlying buffer is not " + property); }
/** * Convenience method to create (for the caller to throw) a * <code>BufferError("buffer structure requires consumer to use {feature}")</code>. * * @param feature * @return the error as a PyException */ protected static PyException bufferRequires(String feature) { return Py.BufferError("buffer structure requires consumer to use " + feature); }
/** * Check that the number offered is in fact the number of dimensions in this buffer, raising a * Python BufferError if this does not match the number of dimensions. This is a helper for * N-dimensional arrays. * * @param n number of dimensions being assumed by caller * @throws PyException (BufferError) if wrong number of indices */ void checkDimension(int n) throws PyException { int ndim = getNdim(); if (n != ndim) { String fmt = "buffer with %d dimension%s accessed as having %d dimension%s"; String msg = String.format(fmt, ndim, ndim == 1 ? "" : "s", n, n, n == 1 ? "" : "s"); throw Py.BufferError(msg); } }
/** * Test to see if the array may be resized and raise a BufferError if not. This must be called * by the implementation of any operation that changes the number of elements in the array. * * @throws PyException (BufferError) if there are buffer exports preventing a resize */ private void resizeCheck() throws PyException { if (export != null) { // A buffer was exported at some time and we have not explicitly discarded it. PyBuffer pybuf = export.get(); if (pybuf != null && !pybuf.isReleased()) { // A consumer still has the exported buffer throw Py.BufferError("cannot resize an array that is exporting buffers"); } else { /* * Either the reference has expired or all consumers have released it. Either way, * the weak reference is useless now. */ export = null; } } }
/** * Test to see if the byte array may be resized and raise a BufferError if not. This must be * called by the implementation of any append or insert that changes the number of bytes in the * array. * * @throws PyException (BufferError) if there are buffer exports preventing a resize */ protected void resizeCheck() throws PyException { if (export != null) { // A buffer was exported at some time and we have not explicitly discarded it. PyBuffer pybuf = export.get(); if (pybuf != null && !pybuf.isReleased()) { // A consumer still has the exported buffer throw Py.BufferError("Existing exports of data: object cannot be re-sized"); } else { /* * Either the reference has expired or all consumers have released it. Either way, * the weak reference is useless now. */ export = null; } } }