问题 ctypes c_char_p的不同行为?


我对不同版本的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

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

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


12240
2018-05-25 05:24


起源

在Python 3中使用 bytes,即 c = b"hello"。该 c_char_p 实例指向的私有缓冲区 bytes 对象,所以只能使用它 const 不会修改字符串的参数。 - eryksun
@eryksun如果您可以添加它作为答案,理由为什么它在python3中发生了变化,我很乐意接受它。 - Sagar Masuti


答案:


c_char_p 是。的子类 _SimpleCData,与 _type_ == 'z'。该 __init__ 方法调用类型 setfunc,对于简单类型 'z' 是 z_set

在Python 2中, z_set 功能 (2.7.7)是为处理两者而编写的 str 和 unicode 字符串。在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'>

在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


16
2018-06-05 13:25



谢谢你的详细解答。 - Sagar Masuti