ctypes initializer

castironpi castironpi at gmail.com
Sun Aug 24 02:11:14 CEST 2008

On Aug 23, 6:43 pm, marek.ro... at wp.pl wrote:
> castironpi napisa³(a):
> > Is there a way to initialize a ctypes Structure to point to an offset
> > into a buffer? I don't know if the way I'm doing it is supported.
> There is a high probability you're abusing ctypes too much, but it's
> possible. The following seems to work:
> from ctypes import *
> class S(Structure):
>         _fields_ = [('x', c_uint), ('y', c_int)]
> rawdata = create_string_buffer('\xEE\xFF\x78\x56\x34\x12\xFF\xFF\xFF
> \xFF\xAA')
> # Try to make a structure s of type S which takes its data from
> rawdata
> # buffer, starting at index 2
> s = cast(c_void_p(addressof(rawdata)+2), POINTER(S)).contents
> print hex(s.x), s.y # Should be 12345678h and -1

Output is 0x12345678L -1, as you state.  I understand that '\xEE\xFF'
is skipped with addressof(rawdata)+ 2, which makes +2 an offset into
the buffer.

At this point, I'd say the use of 'cast' is dubious, but possible to
support.  My problem comes in, in that the buffer I have comes from a
non-ctypes source.  It is a, <drumroll please> mmap.

My goals in exploring this are persistence and IPC, which are
certainly not abusing Python too much.  'ctypes' may not be right for
the job though.  The solution I looked at got even worse than 'from
_ctypes import _cast_addr'.  I want a supported way to do it.

More information about the Python-list mailing list