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-08-01.04:13:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1406866387.35.0.859372897774.issue11429@psf.upfronthosting.co.za>
In-reply-to
Content
> Interesting that “cast” accepts a byte string. If this is 
> intended behaviour, it would be good to document that. 
> Currently it says it takes “an object that can be 
> interpreted as a pointer”.

cast makes an FFI call:

    _cast = PYFUNCTYPE(py_object,
                       c_void_p, py_object, py_object)(_cast_addr)
    def cast(obj, typ):
        return _cast(obj, obj, typ)

The first arg is passed as c_void_p, i.e. a void pointer. c_void_p.from_param accepts common objects that can be interpreted as a pointer: None (NULL), integers, bytes, and str. It also accepts c_void_p ('P'), c_char_p ('z'), c_wchar_p ('Z'), Array, _Pointer, _CFuncPtr, and CArgObject (byref). 

If none of the latter apply, c_void_p.from_param checks for the _as_parameter_ hook. For example:

    from ctypes import *
    from ctypes.util import find_library

    libc = CDLL(find_library('c'))
    libc.atoi.argtypes = [c_void_p]
    
    class X: 
        _as_parameter_ = b'123'

    >>> libc.atoi(X())
    123

There's also code in place to support bytearray, but it's incomplete. It uses the z_set setfunc (defined in cfield.c), which doesn't support bytearray yet.
History
Date User Action Args
2014-08-01 04:13:07eryksunsetrecipients: + eryksun, theller, amaury.forgeotdarc, belopolsky, meador.inge, benrg, BreamoreBoy, martin.panter
2014-08-01 04:13:07eryksunsetmessageid: <1406866387.35.0.859372897774.issue11429@psf.upfronthosting.co.za>
2014-08-01 04:13:07eryksunlinkissue11429 messages
2014-08-01 04:13:07eryksuncreate