Skip to content Skip to sidebar Skip to footer

Example To Throw A Buffererror

On reading in the Python 3.3 documentation I noticed the entry about a BufferError exception: 'Raised when a buffer related operation cannot be performed.'. Now I'm wondering in wh

Solution 1:

Here is a list of the functions that might raise a BufferError:

http://docs.python.org/3.4/c-api/buffer.html#buffer-related-functions

Solution 2:

Most exceptions are actually more applicable to C-code than Python code using the buffer protocol; see the C API buffer protocol.

From the 3.3 source code, I see the following error messages being used for BufferError:

memoryview:

  • underlying buffer is not writable for a read-only buffer.
  • writable contiguous buffer requested for a non-contiguous object. for a memory view that is not using the right contiguous format
  • memoryview has %zd exported buffer%s on release; this would, IIRC, indicate that the reference count off.
  • memoryview: underlying buffer is not writable when creating a writable memoryview from a read-only buffer. Should not happen from Python code.
  • memoryview: underlying buffer is not C-contiguous when passing in a non-contiguous buffer object but flags requested one. If strides are used and the buffer is not C-contiguous, this exception is also thrown.
  • memoryview: underlying buffer is not Fortran contiguous, ditto.
  • memoryview: underlying buffer is not contiguous, ditto.
  • memoryview: underlying buffer requires suboffsets: flags indicate that a suboffset buffer is required but the buffer passed is not.
  • memoryview: cannot cast to unsigned bytes if the format flag is present: unsigned bytes and a specified format do not mix.

bytearray:

  • Existing exports of data: object cannot be re-sized: attempting to resize a bytearray that has other views on it.

There is also a undocumented PyObject_CopyData C-API method that'll throw this exception with destination is too small to receive data from source if the destination buffer is too small.

Post a Comment for "Example To Throw A Buffererror"