ctypes initializer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • castironpi

    ctypes initializer

    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.
  • marek.rocki@wp.pl

    #2
    Re: ctypes initializer

    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_b uffer('\xEE\xFF \x78\x56\x34\x1 2\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(a ddressof(rawdat a)+2), POINTER(S)).con tents

    print hex(s.x), s.y # Should be 12345678h and -1

    Comment

    • castironpi

      #3
      Re: ctypes initializer

      On Aug 23, 6:43 pm, marek.ro...@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_b uffer('\xEE\xFF \x78\x56\x34\x1 2\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(a ddressof(rawdat a)+2), POINTER(S)).con tents
      >
      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(rawda ta)+ 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 pleasemmap.

      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.

      Comment

      • castironpi

        #4
        Re: ctypes initializer

        On Aug 23, 7:11 pm, castironpi <castiro...@gma il.comwrote:
        On Aug 23, 6:43 pm, marek.ro...@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_b uffer('\xEE\xFF \x78\x56\x34\x1 2\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(a ddressof(rawdat a)+2), POINTER(S)).con tents
        >
        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(rawda ta)+ 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 pleasemmap.
        >
        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.
        The solution could be as simple as returning a ctypes pointer from an
        mmap method. Does this require a PEP, or ought a patch to do it?

        Comment

        Working...