ctypes c_char_p的不同行为?

19

我对不同版本的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相同的行为?我想从Python将char指针传递给C。


5
在Python 3中使用bytes,例如c = b"hello"c_char_p实例指向bytes对象的私有缓冲区,因此仅在不修改字符串的情况下使用这个实例来处理const参数。 - Eryk Sun
@eryksun 如果您能将其作为答案添加,并说明在Python3中更改的原因,我将很高兴接受它。 - Sagar Masuti
1个回答

25

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 null-terminated字符串(即由\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

感谢您的详细回答。 - Sagar Masuti
9
简而言之,在Python 3.x中,调用ctypes.c_char_p(my_string.encode('utf-8'))即可使其正常工作。 - phyatt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接