This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients BreamoreBoy, amaury.forgeotdarc, belopolsky, benrg, eryksun, martin.panter, meador.inge, theller
Date 2014-07-31.21:46:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1406843201.86.0.187177948868.issue11429@psf.upfronthosting.co.za>
In-reply-to
Content
Extending byref to support bytes and str objects sounds reasonable. 

Here's another workaround to pass a bytes object with an offset:

    from ctypes import *
    from ctypes.util import find_library

    class offset:
        def __init__(self, arg, offset):
            self.arg = arg
            self.offset = offset

    class my_char_p(c_char_p):
        @classmethod
        def from_param(cls, arg):
            if isinstance(arg, offset):
                t = cast(arg.arg, POINTER(c_char * 0))[0]
                carg = byref(t, arg.offset)
            else:
                carg = super().from_param(arg)
            return carg

    atoi = CDLL(find_library('c')).atoi
    atoi.argtypes = [my_char_p]

    >>> atoi(b'12345')
    12345
    >>> atoi(offset(b'12345', 1))
    2345
    >>> atoi(offset(b'12345', 3))
    45

You can also convert bytearray, memoryview, and array.array objects from_param. If the object's buffer is writable you can use a ctypes type's from_buffer method to create the C arg. This takes an optional offset argument. Otherwise use from_buffer_copy or cast.
History
Date User Action Args
2014-07-31 21:46:41eryksunsetrecipients: + eryksun, theller, amaury.forgeotdarc, belopolsky, meador.inge, benrg, BreamoreBoy, martin.panter
2014-07-31 21:46:41eryksunsetmessageid: <1406843201.86.0.187177948868.issue11429@psf.upfronthosting.co.za>
2014-07-31 21:46:41eryksunlinkissue11429 messages
2014-07-31 21:46:41eryksuncreate