Have bytes(buffer) return the content
On Python 3.x, using
bytes() on a
ffi.buffer(…) object returns the content. On 2.x, (where
str), it returns something like
<_cffi_backend.buffer object at 0x272b780>. Is it possible to change it to consistently get the 3.x behavior?
Context: cairocffi accepts file-like objects, and uses
file_obj.write(buffer). This works great when the object is a real file or
io.BytesIO and uses the C-level buffer interface, but not so much for file-like objects implemented in Python (such as Django’s
HttpResponse) that end up calling
bytes() on their argument.
Original bug: https://github.com/Kozea/WeasyPrint/issues/141
I could use
file_obj.write(buffer[:]) in cairocffi, but the additional copy kind of defeats the purpose of having buffers in the first place.