当包装用CFUNCTYPE
型Python函数,我已经发现,非指针类型被自动转换为虽然他们value
属性被调用。防止自动类型转换回调函数
我怎样才能抑制这种自动转换?
from ctypes import *
funcspec = CFUNCTYPE(c_int, c_int, POINTER(c_int))
@funcspec
def callback(the_int, the_int_p):
print(vars())
return 3
print(callback(c_int(1), byref(c_int(2))))
将会产生(python3
cfunctype_type_conversion.py
):
{'the_int': 1, 'the_int_p': <__main__.LP_c_int object at 0x2671830>}
3
我想:
{'the_int': c_int(1), 'the_int_p': <__main__.LP_c_int object at 0x2671830>}
c_int(3)
这么做并不是完全相同CFUNCTYPE的目的是什么? ctypes的目的是与C-API接口并从C类型和Python类型来回转换。你不应该保留c_int的东西,直接用它们做事。例如,您不能将c_int添加到另一个c_int。 – 2011-06-08 09:47:54