homepage

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 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.
History
Date User Action Args
2014年07月31日 15:41:14eryksunsetrecipients: + eryksun, theller, vstinner, meador.inge, santoso.wijaya, benrg, martin.panter
2014年07月31日 15:41:14eryksunsetmessageid: <1406821274.71.0.275113747451.issue11427@psf.upfronthosting.co.za>
2014年07月31日 15:41:14eryksunlinkissue11427 messages
2014年07月31日 15:41:14eryksuncreate

AltStyle によって変換されたページ (->オリジナル) /