Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cython issue: 'bool' is not a type identifier

Tags:

c++

python

cython

I'm desperately trying to expose a std::vector<bool> class member to a Python class.

Here is my C++ class:

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!

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 

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).

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.

What's wrong ?? I believe this might be a bug. Anyone knows how to circumvent this ? Thanks.

like image 616
Carmellose Avatar asked Jul 09 '14 17:07

Carmellose


1 Answers

There's some extra C++ support you need to do. At the top of your .pyx file, add

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

like image 120
Ben Avatar answered Oct 01 '22 11:10

Ben