eryksun added the comment:

cast calls ctypes._cast(obj, obj, typ). _cast is a ctypes function pointer 
defined as follows:

    _cast = PYFUNCTYPE(py_object, 
                       c_void_p, py_object, py_object)(_cast_addr)

Since cast makes an FFI call that converts the first arg to c_void_p, you can 
directly cast bytes to a pointer type:

    >>> from ctypes import *
    >>> data = b'123\x00abc'

    >>> ptr = cast(data, c_void_p)

string_at is defined similarly using c_void_p for the first arg:

    _string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
 
    >>> string_at(ptr, 8)
    b'123\x00abc\x00'

Get around the from_buffer mutability requirement by casting to an array (i.e. 
to an array pointer followed by a dereference):

    >>> arr = cast(data, POINTER(c_char * len(data)))[0]
    >>> arr[:]
    b'123\x00abc'

Then use byref to pass an offset into the array:

    >>> from ctypes.util import find_library
    >>> printf = CDLL(find_library('c')).printf

    >>> printf(b'%s\n', byref(arr, 4))
    abc
    4

Since this doesn't copy the buffer, take care to ensure the function call won't 
modify the contents.

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11427>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to