关于使其正常工作,如果您将其传递给
bytes对象,它将起作用:
>>> import ctypes>>> ctypes.create_string_buffer(b'hello')<ctypes.c_char_Array_6 object at 0x25258c0>
查看以下代码
create_string_buffer:
def create_string_buffer(init, size=None): """create_string_buffer(aBytes) -> character array create_string_buffer(anInteger) -> character array create_string_buffer(aString, anInteger) -> character array """ if isinstance(init, (str, bytes)): if size is None: size = len(init)+1 buftype = c_char * size buf = buftype() buf.value = init return buf elif isinstance(init, int): buftype = c_char * init buf = buftype() return buf raise TypeError(init)
直接做
>>> (ctypes.c_char * 10)().value = b'123456789'
这很好。
>>> (ctypes.c_char * 10)().value = '123456789'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: str/bytes expected instead of str instance
这表明了相同的行为。在我看来,好像您已找到一个错误。
是时候访问http://bugs.python.org了。有一些与相关的bug
c_char,
create_string_buffer它们都在同一领域,但是没有人报告说
str现在给它失败了(但是有明确的例子表明它曾经在Py3K中工作)。



