Message224420
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. |
|
Date |
User |
Action |
Args |
2014-07-31 15:41:14 | eryksun | set | recipients:
+ eryksun, theller, vstinner, meador.inge, santoso.wijaya, benrg, martin.panter |
2014-07-31 15:41:14 | eryksun | set | messageid: <1406821274.71.0.275113747451.issue11427@psf.upfronthosting.co.za> |
2014-07-31 15:41:14 | eryksun | link | issue11427 messages |
2014-07-31 15:41:14 | eryksun | create | |
|