On Wednesday, August 10, 2016 at 10:13:53 PM UTC+12, eryk sun wrote: > ctypes classes have from_buffer and from_buffer_copy methods that use > the buffer protocol. For example, for a read-only buffer you can use > from_buffer_copy: > > >>> b = bytes(b'1234') > >>> a = (ctypes.c_char * 3).from_buffer_copy(b, 1) > >>> a[:] > b'234'
Can I avoid the copying? For example, in Qahirah <https://github.com/ldo/qahirah> the following method creates and fills in an ImageSurface from a stream of bytes in PNG format: @classmethod def create_from_png_bytes(celf, data) : "creates an ImageSurface from a PNG-format data sequence. This can be" \ " of the bytes or bytearray types, or an array.array with \"B\" type code." offset = 0 def read_data(_, data, length) : nonlocal offset if offset + length <= len(data) : ct.memmove(data, baseadr + offset, length) offset += length status = CAIRO.STATUS_SUCCESS else : status = CAIRO.STATUS_READ_ERROR #end if return \ status #end read_data #begin create_from_png_bytes if isinstance(data, bytes) or isinstance(data, bytearray) : data = array.array("B", data) elif not isinstance(data, array.array) or data.typecode != "B" : raise TypeError("data is not bytes, bytearray or array of bytes") #end if baseadr = data.buffer_info()[0] return \ celf.create_from_png_stream(read_data, None) #end create_from_png_bytes But it has to copy the bytes into an array.array object, then decode that. Is there a way it could access the bytes memory directly? -- https://mail.python.org/mailman/listinfo/python-list