[issue11427] ctypes from_buffer no longer accepts bytes
STINNER Victor
report at bugs.python.org
Tue Nov 29 18:57:49 CET 2011
STINNER Victor <victor.stinner at haypocalc.com> added the comment:
> I just want to call my C function.
You can use something else than ctypes_array.from_buffer(buffer). ctypes_array.from_buffer() creates a read-write object which can be used to modify the buffer. You cannot pass a read-only object to ctypes_array.from_buffer().
> I'm pretty sure that I verified that this code worked in 3.1.3 before
> opening this bug, but it's been a while.
If it was possible, it was a bug. You cannot modify a read-only object, like bytes, because it would lead to inconsistent object and may lead your program to a crash, even if you don't call C functions.
Example in pure Python:
>>> import ctypes
>>> T=(ctypes.c_uint8*4)
>>> B=bytearray(4)
>>> x=T.from_buffer(B)
>>> B
bytearray(b'\x00\x00\x00\x00')
>>> x[0]=9
>>> x[2]=100
>>> B
bytearray(b'\t\x00d\x00')
>>> list(x)
[9, 0, 100, 0]
B is modified when x[index] is modified.
--
A bug tracker is not the right place to ask help with ctypes.
----------
resolution: -> invalid
status: open -> closed
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11427>
_______________________________________
More information about the Python-bugs-list
mailing list