ctypes c_char_p的不同行为?

Sag*_*uti 15 python ctypes python-2.7 python-3.x

我对不同版本的python的这种行为感到困惑,不明白为什么?

Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> c="hello"
>>> a=ctypes.c_char_p(c)
>>> print(a.value) 
hello

Python 3.3.5 (default, Mar 11 2014, 15:08:59) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> c="hello" 
>>> a=ctypes.c_char_p(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bytes or integer address expected instead of str instance
Run Code Online (Sandbox Code Playgroud)

一个工作,而另一个给我一个错误.哪一个是正确的?

如果它们都是正确的,我怎样才能实现与3.3.5中的2.7相同的行为?我想将char指针从python传递给C.

Ery*_*Sun 19

c_char_p是一个子类_SimpleCData,有_type_ == 'z'.该__init__方法调用类型setfunc,对于简单类型'z'z_set.

在Python 2中,编写z_set函数(2.7.7)来处理两者strunicode字符串.在Python 3之前,str是一个8位字符串.CPython 2.x在str内部使用C以空字符结尾的字符串(即终止的字节数组\0),z_set可以调用它PyString_AS_STRING(即获取指向str对象内部缓冲区的指针).甲unicode字符串需要首先被编码为字节串.z_set自动处理此编码并保持对_objects 属性中编码字符串的引用.

>>> c = u'spam'
>>> a = c_char_p(c)
>>> a._objects
'spam'
>>> type(a._objects)
<type 'str'>
Run Code Online (Sandbox Code Playgroud)

在Windows上,默认的ctypes字符串编码是'mbcs',错误处理设置为'ignore'.在所有其他平台的默认编码是'ascii',与'strict'错误处理.要修改默认值,请致电ctypes.set_conversion_mode.例如,set_conversion_mode('utf-8', 'strict').

在Python 3中,z_set函数(3.4.1)不会自动转换str(现在为Unicode)bytes.范例在Python 3中转移到严格划分二进制数据中的字符串.删除了ctypes默认转换,功能也是如此set_conversion_mode.你必须传递c_char_p一个bytes对象(例如b'spam''spam'.encode('utf-8')).在CPython 3.x中,z_set调用C-API函数PyBytes_AsString来获取指向bytes对象内部缓冲区的指针.

请注意,如果C函数修改了字符串,则需要使用它create_string_buffer来创建c_char数组.查找要键入的参数,以const确保使用它是安全的c_char_p.

  • 总之,调用`ctypes.c_char_p(my_string.encode('utf-8'))`使其在Python 3.x中工作。 (3认同)