eryksun added the comment:

> 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.

----------

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

Reply via email to