Message224420
| Author |
eryksun |
| Recipients |
benrg, eryksun, martin.panter, meador.inge, santoso.wijaya, theller, vstinner |
| Date |
2014年07月31日.15:41:14 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1406821274.71.0.275113747451.issue11427@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
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. |
|