[New-bugs-announce] [issue11429] ctypes is highly eclectic in its raw-memory support

benrg report at bugs.python.org
Mon Mar 7 08:49:01 CET 2011

New submission from benrg <benrudiak at gmail.com>:

ctypes accepts bytes objects as arguments to C functions, but not bytearray objects. It has its own array types but seems to be unaware of array.array. It doesn't even understand memoryview objects. I think that all of these types should be passable to C code.

Additionally, while passing a pointer to a bytes value to a C function is easy, it's remarkably difficult to pass that same pointer with an offset added to it. I first tried byref(buf, offset), but byref wouldn't accept bytes. Then I tried addressof(buf), but that didn't work either, even though ctypes is clearly able to obtain this address when it has to. After banging my head against the wall for longer than I care to think about, I finally came up with something like byref((c_char*length).from_buffer(buf), offset). But that broke in 3.2. After wasting even more time, I came up with addressof(cast(buf, POINTER(c_char)).contents) + offset. This is nuts. There should be a simple and documented way to do this. My first preference would be for the byref method, since it was the first thing I tried, and would have saved me the most time. Ideally both byref and addressof should work for bytes objects as they do for ctypes arrays (and also for bytearray, memoryview, etc.)

assignee: theller
components: ctypes
messages: 130236
nosy: benrg, theller
priority: normal
severity: normal
status: open
title: ctypes is highly eclectic in its raw-memory support
type: feature request
versions: Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list