Message273750
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. |
|
Date |
User |
Action |
Args |
2016-08-27 02:36:01 | eryksun | set | recipients:
+ eryksun, docs@python, Christian Kothe |
2016-08-27 02:36:01 | eryksun | set | messageid: <1472265361.08.0.22116651592.issue27871@psf.upfronthosting.co.za> |
2016-08-27 02:36:00 | eryksun | link | issue27871 messages |
2016-08-27 02:36:00 | eryksun | create | |
|