Eric Firing wrote:
In writing a cython extension to work with numpy masked arrays, I needed to work with the mask, which is dtype('bool'). Therefore I was expecting to be able to use np.bool_t, in analogy to np.float_t. Instead I had to use np.int8_t, and cast the input to np.int8 in a python wrapper. Can bool_t be added to numpy.pxd?

Thanks.

Eric


Attached is a diff that I think takes care of adding bool support.

Eric



diff -r 51fa7e425dc8 Cython/Includes/numpy.pxd
--- a/Cython/Includes/numpy.pxd	Wed Jun 03 03:17:42 2009 -0700
+++ b/Cython/Includes/numpy.pxd	Fri Jun 05 21:07:15 2009 -1000
@@ -123,7 +123,8 @@
                 if ((descr.byteorder == '>' and little_endian) or
                     (descr.byteorder == '<' and not little_endian)):
                     raise ValueError("Non-native byte order not supported")
-                if   t == NPY_BYTE:        f = "b"
+                if   t == NPY_BOOL:        f = "?"
+                elif t == NPY_BYTE:        f = "b"
                 elif t == NPY_UBYTE:       f = "B"
                 elif t == NPY_SHORT:       f = "h"
                 elif t == NPY_USHORT:      f = "H"
@@ -173,6 +174,8 @@
 
     cdef int PyDataType_HASFIELDS(dtype obj)
 
+    ctypedef unsigned char    npy_bool
+
     ctypedef signed char      npy_byte
     ctypedef signed short     npy_short
     ctypedef signed int       npy_int
@@ -233,6 +236,7 @@
 # The ones that are commented out needs an IFDEF function
 # in Cython to enable them only on the right systems.
 
+ctypedef npy_bool       bool_t
 ctypedef npy_int8       int8_t
 ctypedef npy_int16      int16_t
 ctypedef npy_int32      int32_t
@@ -316,7 +320,8 @@
                 raise RuntimeError("Format string allocated too short.")
 
             # Until ticket #99 is fixed, use integers to avoid warnings
-            if   t == NPY_BYTE:        f[0] =  98 #"b"
+            if   t == NPY_BOOL:        f[0] =  63 #"?"
+            elif t == NPY_BYTE:        f[0] =  98 #"b"
             elif t == NPY_UBYTE:       f[0] =  66 #"B"
             elif t == NPY_SHORT:       f[0] = 104 #"h"
             elif t == NPY_USHORT:      f[0] =  72 #"H"
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to