问题描述
我拼命试图将 std :: vector< bool> 类成员公开给Python类。
I'm desperately trying to expose a std::vector<bool> class member to a Python class.
这是我的C ++类:
class Test { public: std::vector<bool> test_fail; std::vector<double> test_ok; };
访问和转换 test_ok 键入 double (或int,float,..)有效,但不适用于 bool !
While the access and conversion of test_ok of type double (or int, float, ..) works, it does not for bool!
这是我的Cython类:
Here is my Cython class:
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
我得到的错误是:
Error compiling Cython file: ------------------------------------------------------------ ... cdef extern from *: ctypedef bool X 'bool' ^ ------------------------------------------------------------ vector.from_py:37:13: 'bool' is not a type identifier
我正在使用python 2.7.6和Cython 0.20.2(也尝试使用0.20.1)。
I'm using python 2.7.6 and Cython 0.20.2 (also tried 0.20.1).
我也尝试过使用房地产,但是它也不起作用。
I also tried with properties but it does not work either.
附录:我在pyx文件的顶部确实有来自libcpp cimport bool 以及矢量导入。
Addendum: I do have the from libcpp cimport bool at the top of my pyx file, as well as the vector import.
怎么了?我相信这可能是一个错误。有人知道该如何规避吗?谢谢。
What's wrong ?? I believe this might be a bug. Anyone knows how to circumvent this ? Thanks.
推荐答案
我发现了一个有效的解决方法,尽管它可能不是最佳选择。
I have found a valid workaround, although it may not be optimal.
我用python列表替换了 pytest 类的成员类型。
I have replaced the members types of the pytest class with python lists.
现在隐式完成了转换,如文档中所述:
The conversion is now done implicitely, as described in the documentation: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#standard-library
所以现在,我的课看起来像这样:
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
这篇关于cython问题:'bool'不是类型标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!