【发布时间】:2021-05-25 17:32:29
【问题描述】:
我有一个 C 库,其中包含一个函数,其中包括一个 (const unsigned char *) 类型参数,我想为此编写一个基于 ctypes 的 Python 绑定。
在 Python 版本中,我希望能够传递“字节”值。
直接传递“字节”值是行不通的。我得到一个 ctypes.ArgumentError:
ctypes.ArgumentError: argument 6: <class 'TypeError'>: expected LP_c_ubyte instance instead of bytes
我可以通过使用 ctypes.cast() 方法将 'bytes' 值转换为 unsigned char * 来使其工作。这似乎是显而易见的事情,效果很好,但是当我在我的代码上运行 mypy 时,我收到以下错误:
source/pydwf/core/api/DigitalCanAPI.py:65: error: Argument 1 to "cast" has incompatible type "bytes"; expected "Union[_CData, _CArgObject, int]"
Found 1 error in 1 file (checked 52 source files)
那么,有没有一种方法可以同时满足 Python(所以它可以工作)和 mypy(所以它对演员阵容感到满意?)
这可能是 mypy 中的错误吗?
【问题讨论】:
标签: python casting ctypes mypy