AKAIK it is not possible to give ctypes a bytearray object and persuade
it to give you a pointer to the actual array data, suitable for passing
to a DLL.
On 6/30/22, Rob Cliffe via Python-list <pytho...@python.org> wrote:
AKAIK it is not possible to give ctypes a bytearray object and persuadeYou're overlooking the from_buffer() method. For example:
it to give you a pointer to the actual array data, suitable for passing
to a DLL.
bytearray(b'spam&eggs\x00')ba = bytearray(10)
ca = (ctypes.c_char * len(ba)).from_buffer(ba)
ca.value = b'spam&eggs'
ba
Note that the bytearray can't be resized while a view of the data is exported. For example:
Traceback (most recent call last):ba.append(97)
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
Traceback (most recent call last):del ba[-1]
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
On 6/30/22, Rob Cliffe via Python-list <python-list@python.org> wrote:
AKAIK it is not possible to give ctypes a bytearray object and persuadeYou're overlooking the from_buffer() method. For example:
it to give you a pointer to the actual array data, suitable for passing
to a DLL.
>>> ba = bytearray(10)
>>> ca = (ctypes.c_char * len(ba)).from_buffer(ba)
>>> ca.value = b'spam&eggs'
>>> ba
bytearray(b'spam&eggs\x00')
Note that the bytearray can't be resized while a view of the data is exported. For example:
>>> ba.append(97)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
>>> del ba[-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 226:51:04 |
Calls: | 6,624 |
Calls today: | 6 |
Files: | 12,171 |
Messages: | 5,318,701 |