栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

create_string_buffer抛出错误TypeError:预期的str / bytes而非str实例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

create_string_buffer抛出错误TypeError:预期的str / bytes而非str实例

关于使其正常工作,如果您将其传递给

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中工作)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号