Question on bool: Python vs. C vs. C++ bool; memory view of bool array

546 views
Skip to first unread message

Kolen Cheung

unread,
May 14, 2017, 4:56:02 AM5/14/17
to cython-users
Hi, I bet question like this has been asked dozen of times. A quick search of this topic in this forum doesn't seem to have the info I was looking for though. So I pardon asking a very similar question in a slightly different way.

First, from the FAQ, it mentioned I can use either the Python bool or C++ bool. But why not the C bool from the stdbool? I tried but it wasn't available for import. The reason for this question is that I basically need to use C++ language for Cythonization just because I have bool arrays. I would want to use the simpler C language output instead.

Second, memoryview seem not to be allowed for bool array, and a quick search seem to suggest np.ndarray[np.uint8_t, cast=True, ndim=2] mask is the best way to do it. But it will then have a Python interaction. e.g. I want to inline a function that has this argument, but Cython said the inline failed because of the Python interaction. So for inline function I need to use pointer instead, which is not ideal in terms of indexing into a 2D array. Are there any better way to do this?

Stefan Behnel

unread,
May 14, 2017, 5:06:33 AM5/14/17
to cython...@googlegroups.com
Kolen Cheung schrieb am 14.05.2017 um 09:53:
> First, from the FAQ, it mentioned I can use either the Python bool or C++
> bool. But why not the C bool from the stdbool? I tried but it wasn't
> available for import. The reason for this question is that I basically need
> to use C++ language for Cythonization just because I have bool arrays. I
> would want to use the simpler C language output instead.

You could try this (entirely untested):

cdef extern from "stdbool.h":
ctypedef bint bool

That should let any pythonesque usages of the "bool" type coerce to Python
True/False. Not sure how well the other direction behaves, but it might
"just work" in many cases.

Would be nice to get feedback if you try this out.


> Second, memoryview seem not to be allowed for bool array, and a quick
> search seem to suggest np.ndarray[np.uint8_t, cast=True, ndim=2] mask is
> the best way to do it. But it will then have a Python interaction. e.g. I
> want to inline a function that has this argument, but Cython said the
> inline failed because of the Python interaction. So for inline function I
> need to use pointer instead, which is not ideal in terms of indexing into a
> 2D array. Are there any better way to do this?

Could you show us the code and the exact Cython error that you got?
Inlining is ok with Python interaction, only "nogil" is not.

Stefan

Robert Bradshaw

unread,
May 15, 2017, 1:56:56 PM5/15/17
to cython...@googlegroups.com
On Sun, May 14, 2017 at 2:06 AM, Stefan Behnel <stef...@behnel.de> wrote:
Kolen Cheung schrieb am 14.05.2017 um 09:53:
> First, from the FAQ, it mentioned I can use either the Python bool or C++
> bool. But why not the C bool from the stdbool? I tried but it wasn't
> available for import. The reason for this question is that I basically need
> to use C++ language for Cythonization just because I have bool arrays. I
> would want to use the simpler C language output instead.

You could try this (entirely untested):

    cdef extern from "stdbool.h":
        ctypedef bint bool

That should let any pythonesque usages of the "bool" type coerce to Python
True/False. Not sure how well the other direction behaves, but it might
"just work" in many cases.

Would be nice to get feedback if you try this out.

Yep, you can also do "from libcpp cimport bool" which will pull this definition from https://github.com/cython/cython/blob/master/Cython/Includes/libcpp/__init__.pxd

However, note that many C++ structures using bool (such as vector<bool>) have different specializations that can interact strangely with how indexing works (due to using a bit-per-element rather than anything that can be addressed). 

> Second, memoryview seem not to be allowed for bool array, and a quick
> search seem to suggest np.ndarray[np.uint8_t, cast=True, ndim=2] mask is
> the best way to do it. But it will then have a Python interaction. e.g. I
> want to inline a function that has this argument, but Cython said the
> inline failed because of the Python interaction. So for inline function I
> need to use pointer instead, which is not ideal in terms of indexing into a
> 2D array. Are there any better way to do this?

Could you show us the code and the exact Cython error that you got?
Inlining is ok with Python interaction, only "nogil" is not.

Stefan

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kolen Cheung

unread,
May 25, 2017, 8:08:07 AM5/25/17
to cython-users, stef...@behnel.de

On Sunday, May 14, 2017 at 5:06:33 PM UTC+8, Stefan Behnel wrote:

Kolen Cheung schrieb am 14.05.2017 um 09:53:
> First, from the FAQ, it mentioned I can use either the Python bool or C++
> bool. But why not the C bool from the stdbool? I tried but it wasn't
> available for import. The reason for this question is that I basically need
> to use C++ language for Cythonization just because I have bool arrays. I
> would want to use the simpler C language output instead.

You could try this (entirely untested):

    cdef extern from "stdbool.h":
        ctypedef bint bool

That should let any pythonesque usages of the "bool" type coerce to Python
True/False. Not sure how well the other direction behaves, but it might
"just work" in many cases.

Would be nice to get feedback if you try this out.

I just tried this and it works! Thanks. Should this be added in the documentation and/or Wiki?



> Second, memoryview seem not to be allowed for bool array, and a quick
> search seem to suggest np.ndarray[np.uint8_t, cast=True, ndim=2] mask is
> the best way to do it. But it will then have a Python interaction. e.g. I
> want to inline a function that has this argument, but Cython said the
> inline failed because of the Python interaction. So for inline function I
> need to use pointer instead, which is not ideal in terms of indexing into a
> 2D array. Are there any better way to do this?

Could you show us the code and the exact Cython error that you got?
Inlining is ok with Python interaction, only "nogil" is not.

The error is like this:


>   def boundary_distance(bool[:, :] mask, int num_threads=4, bool debug=False):



E   ValueError: Does not understand character buffer dtype format string ('?')

I think I read somewhere that for bool array memoryviews won’t work and np.ndarray[np.uint8_t, cast=True, ndim=2] mask is needed.

I also tried

def boundary_distance(np.ndarray[np.uint8_t, cast=True, ndim=2] mask_raw, int num_threads=4, bool debug=False):

    cdef bool[:, :] mask = mask_raw

But the same error occur.

Stefan

Reply all
Reply to author
Forward
0 new messages