cython issue: 'bool' is not a type identifier

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.

Example:

cdef bint boolean_variable = True

source: types bint


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


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.

The conversion is now done implicitly, as described in the documentation: https://docs.cython.org/en/latest/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.

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

Tags:

Python

C++

Cython