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