Eryk Sun added the comment:

See section 2.7 in the ctypes docs: 

    Fundamental data types, when returned as foreign function call
    results, or, for example, by retrieving structure field members
    or array items, are transparently converted to native Python
    types. In other words, if a foreign function has a restype of
    c_char_p, you will always receive a Python bytes object, not a
    c_char_p instance.

    Subclasses of fundamental data types do not inherit this
    behavior. So, if a foreign functions restype is a subclass of
    c_void_p, you will receive an instance of this subclass from the
    function call. Of course, you can get the value of the pointer
    by accessing the value attribute.

For example:

    class my_char_p(ctypes.c_char_p):
        pass

    >>> locale = ctypes.create_string_buffer(b'en_US.UTF-8')
    >>> setlocale.restype = ctypes.c_char_p
    >>> result = setlocale(0, locale)
    >>> result
    b'en_US.UTF-8'

    >>> setlocale.restype = my_char_p
    >>> result = setlocale(0, locale)
    >>> result
    my_char_p(31391216)
    >>> result.value
    b'en_US.UTF-8'

> when he/she passes that value into the next library function

A function that takes pointer arguments really should have argtypes defined so 
that there's no chance of accidentally getting the default integer conversion 
to a C int.

----------
assignee:  -> docs@python
components: +Documentation
nosy: +docs@python, eryksun
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
versions:  -Python 3.2, Python 3.3, Python 3.4

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

Reply via email to