Direct use of bytearray buffers with ctypes ?

P

Pakal

Hello

I'm having big trouble wrapping the win32 ReadFile() function with
ctypes.

I wanted to allow the user to give a bytearray to this function, so
that the writable buffer of this bytearray is directly used to receive
the data from the file ; thus, no temporary copy in a separate ctype
buffer would be required.

However, I've found no way to provide ReadFile with a pointer to the
inner buffer of the bytearray object. I get misc. TypeErrors and
ValueErrors all the time, by trying to use the from_buffer() method or
other ctypes functions.

Is that actually posisble to expose with ctypes the internals of a
python object to the system, or is that unsupported at the moment ?
 
P

Pakal

Oki, it seems I've found.
To directly use a bytearray buffer from ctypes, you must first create
a compatible ctypes type (I.E, a char array of same size), and only
then instanciate this new type with newtype.from_buffer
(bytearray_object).

The little danger is : you must NOT change the size of bytearray as
long as the ctypes array targets its inner buffer, else of cousre
memory reallocations occurs, and your ctypes array points to invalid
memory (awaiting a pretty segfault). But modifying chars or characters
ranges from the buffer, as well via the bytearray object than via the
ctypes array, is fine.

I don't think problems can occur if both sides are accessed
simultaneously, even though ctypes operations may release the GIL
while they execute... the only race condition is the access of a
memory segment, isn't it ? No crash shall occur with this...

IDLE 2.6.4
import ctypes
a = bytearray(3)
mytype = ctypes.c_char * 3
h = mytype.from_buffer(a)
h
h[:] = "abc"
a bytearray(b'abc')
h[0] = "k"
a
bytearray(b'kbc')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top