Python cython 问题:'bool' 不是类型标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24659723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cython issue: 'bool' is not a type identifier
提问by carmellose
I'm desperately trying to expose a std::vector<bool>
class member to a Python class.
我拼命地试图将一个std::vector<bool>
班级成员暴露给一个 Python 班级。
Here is my C++ class:
这是我的 C++ 类:
class Test
{
public:
std::vector<bool> test_fail;
std::vector<double> test_ok;
};
While the access and conversion of test_ok
of type double
(or int, float, ..) works, it does not for bool
!
虽然test_ok
类型double
(或 int、float、..)的访问和转换有效,但它不适用于bool
!
Here is my Cython class:
这是我的 Cython 课程:
cdef class pyTest:
cdef Test* thisptr
cdef public vector[bool] test_fail
cdef public vector[double] test_ok
cdef __cinit__(self):
self.thisptr = new Test()
self.test_fail = self.thisptr.test_fail # compiles and works if commented
self.test_ok = self.thisptr.test_ok
cdef __dealloc__(self):
del self.thisptr
The error I get is :
我得到的错误是:
Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from *:
ctypedef bool X 'bool'
^
------------------------------------------------------------
vector.from_py:37:13: 'bool' is not a type identifier
I'm using python 2.7.6 and Cython 0.20.2 (also tried 0.20.1).
我正在使用 python 2.7.6 和 Cython 0.20.2(也尝试过 0.20.1)。
I also tried with properties but it does not work either.
我也尝试过使用属性,但它也不起作用。
Addendum:I do have the from libcpp cimport bool
at the top of my pyx file, as well as the vector import.
附录:from libcpp cimport bool
我的 pyx 文件顶部确实有,以及矢量导入。
What's wrong ?? I believe this might be a bug. Anyone knows how to circumvent this ? Thanks.
怎么了 ??我相信这可能是一个错误。任何人都知道如何规避这个?谢谢。
采纳答案by carmellose
I have found a valid workaround, although it may not be optimal.
我找到了一个有效的解决方法,尽管它可能不是最佳的。
I have replaced the members types of the pytest
class with python lists.
我已经pytest
用 python 列表替换了类的成员类型。
The conversion is now done implicitely, as described in the documentation: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#standard-library
转换现在隐式完成,如文档中所述:http: //docs.cython.org/src/userguide/wrapping_CPlusPlus.html#standard-library
All conversions create a new container and copy the data into it. The items in the containers are converted to a corresponding type automatically, which includes recursively converting containers inside of containers, e.g. a C++ vector of maps of strings.
所有转换都会创建一个新容器并将数据复制到其中。容器中的项会自动转换为相应的类型,包括递归转换容器内部的容器,例如字符串映射的C++ 向量。
So now, my class looks like this:
所以现在,我的班级看起来像这样:
cdef class pyTest:
cdef Test* thisptr
cdef public list test_fail #now ok
cdef public list test_ok
cdef __cinit__(self):
self.thisptr = new Test()
self.test_fail = self.thisptr.test_fail # implicit copy & conversion
self.test_ok = self.thisptr.test_ok # implicit copy and conversion
cdef __dealloc__(self):
del self.thisptr
回答by Ben
There's some extra C++ support you need to do. At the top of your .pyx file, add
您需要做一些额外的 C++ 支持。在 .pyx 文件的顶部,添加
from libcpp cimport bool
I'd take a look inside that to find the other things you might need, like std::string and STL containers
我会看看里面,找到你可能需要的其他东西,比如 std::string 和 STL 容器
回答by Dalek
In order to define boolean
objects in cython, they need to be defined as bint
. According to here: The bint of "boolean int" object is compiled to a c int, but get coerced to and from Cython as booleans.
为了boolean
在 cython 中定义对象,它们需要定义为bint
. 根据here:“boolean int”对象的bint被编译为ac int,但作为布尔值被强制传入和传出Cython。
Example:
例子:
cdef bint boolean_variable = True
source: types bint
来源:类型 bint