numpy.pxd: Added type_t typedefs corresponding to predefined dtypes
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Fri, 15 Aug 2008 22:38:34 +0000 (00:38 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Fri, 15 Aug 2008 22:38:34 +0000 (00:38 +0200)
Cython/Compiler/Buffer.py
Cython/Compiler/Nodes.py
Cython/Includes/numpy.pxd
tests/run/tnumpy.pyx

index c71a0a2cf9f907f867bae2683095a3e34ee0e231..9d322f25f8a978b21063880c1121ae9daa24b607 100644 (file)
@@ -478,7 +478,8 @@ def get_ts_check_item(dtype, writer):
             elif dtype.is_float:
                 types = [('f', 'float'), ('d', 'double'), ('g', 'long double')]
             else:
-                assert False
+                assert dtype.is_error
+                return name
             if dtype.signed == 0:
                 code += "".join(["\n    case '%s': ok = (sizeof(%s) == sizeof(%s) && (%s)-1 > 0); break;" %
                                  (char.upper(), ctype, against, ctype) for char, against in types])
index 7705997bb5b3ed4198578f6423ae563075b323fe..053d7d234849604275083a3e401bc1b68e65d3be 100644 (file)
@@ -601,6 +601,7 @@ class CBufferAccessTypeNode(CBaseTypeNode):
     
     def analyse(self, env):
         base_type = self.base_type_node.analyse(env)
+        if base_type.is_error: return base_type
         import Buffer
 
         options = Buffer.analyse_buffer_options(
index 97bb303751865845c95dd8e75fa25d118bfb3599..db7d4b9c05303f1c800130f02088f72aada26200 100644 (file)
@@ -82,6 +82,22 @@ cdef extern from "numpy/arrayobject.h":
     cdef npy_intp PyArray_DIMS(ndarray arr)
     cdef Py_ssize_t PyArray_ITEMSIZE(ndarray arr)
 
+    ctypedef signed int   npy_byte
+    ctypedef signed int   npy_short
+    ctypedef signed int   npy_int
+    ctypedef signed int   npy_long
+    ctypedef signed int   npy_longlong
+
+    ctypedef unsigned int npy_ubyte
+    ctypedef unsigned int npy_ushort
+    ctypedef unsigned int npy_uint
+    ctypedef unsigned int npy_ulong
+    ctypedef unsigned int npy_ulonglong
+
+    ctypedef float        npy_float
+    ctypedef float        npy_double
+    ctypedef float        npy_longdouble
+
     ctypedef signed int   npy_int8
     ctypedef signed int   npy_int16
     ctypedef signed int   npy_int32
@@ -102,22 +118,40 @@ cdef extern from "numpy/arrayobject.h":
     ctypedef float        npy_float96
     ctypedef float        npy_float128
 
+# Typedefs that matches the runtime dtype objects in
+# the numpy module.
+
+# The ones that are commented out needs an IFDEF function
+# in Cython to enable them only on the right systems.
+
+ctypedef npy_int8       int8_t
+ctypedef npy_int16      int16_t
+ctypedef npy_int32      int32_t
+ctypedef npy_int64      int64_t
+#ctypedef npy_int96      int96_t
+#ctypedef npy_int128     int128_t
+
+ctypedef npy_uint8      uint8_t
+ctypedef npy_uint16     uint16_t
+ctypedef npy_uint32     uint32_t
+ctypedef npy_uint64     uint64_t
+#ctypedef npy_uint96     uint96_t
+#ctypedef npy_uint128    uint128_t
+
+ctypedef npy_float32    float32_t
+ctypedef npy_float64    float64_t
+#ctypedef npy_float80    float80_t
+#ctypedef npy_float128   float128_t
+
+# The int types are mapped a bit surprising --
+# numpy.int corresponds to 'l' and numpy.long to 'q'
+ctypedef npy_long       int_t
+ctypedef npy_longlong   long_t
+
+ctypedef npy_ulong      uint_t
+ctypedef npy_ulonglong  ulong_t
+
+ctypedef npy_double      float_t
+ctypedef npy_double     double_t
+ctypedef npy_longdouble longdouble_t
 
-ctypedef npy_int8   int8_t
-ctypedef npy_int16  int16_t
-ctypedef npy_int32  int32_t
-ctypedef npy_int64  int64_t
-ctypedef npy_int96  int96_t
-ctypedef npy_int128 int128_t
-
-ctypedef npy_uint8   uint8_t
-ctypedef npy_uint16  uint16_t
-ctypedef npy_uint32  uint32_t
-ctypedef npy_uint64  uint64_t
-ctypedef npy_uint96  uint96_t
-ctypedef npy_uint128 uint128_t
-
-ctypedef npy_float32 float32_t
-ctypedef npy_float64 float64_t
-ctypedef npy_float80 float80_t
-ctypedef npy_float128 float128_t
index 074327920341a1c478cf76688355e3ae92771873..418981d43354b5f0c0fbb0cdb4c447da98c97659 100644 (file)
@@ -92,6 +92,15 @@ try:
     >>> test_dtype('g', inc1_longdouble)
     >>> test_dtype('O', inc1_object)
 
+    >>> test_dtype(np.int, inc1_int_t)
+    >>> test_dtype(np.long, inc1_long_t)
+    >>> test_dtype(np.float, inc1_float_t)
+    >>> test_dtype(np.double, inc1_double_t)
+    >>> test_dtype(np.longdouble, inc1_longdouble_t)
+
+    >>> test_dtype(np.int32, inc1_int32_t)
+    >>> test_dtype(np.float64, inc1_float64_t)
+
     Unsupported types:
     >>> test_dtype(np.complex, inc1_byte)
     Traceback (most recent call last):
@@ -103,7 +112,6 @@ try:
     Traceback (most recent call last):
        ...
     ValueError: only objects, int and float dtypes supported for ndarray buffer access so far (dtype is 20)
-
     
 """
 except:
@@ -154,10 +162,24 @@ def inc1_float(np.ndarray[float] arr):                  arr[1] += 1
 def inc1_double(np.ndarray[double] arr):                arr[1] += 1
 def inc1_longdouble(np.ndarray[long double] arr):       arr[1] += 1
 
+
 def inc1_object(np.ndarray[object] arr):
     o = arr[1]
     o += 1
     arr[1] = o # unfortunately, += segfaults for objects
+
+
+def inc1_int_t(np.ndarray[np.int_t] arr):               arr[1] += 1
+def inc1_long_t(np.ndarray[np.long_t] arr):             arr[1] += 1
+def inc1_float_t(np.ndarray[np.float_t] arr):           arr[1] += 1
+def inc1_double_t(np.ndarray[np.double_t] arr):         arr[1] += 1
+def inc1_longdouble_t(np.ndarray[np.longdouble_t] arr): arr[1] += 1
+
+# The tests below only work on platforms that has the given types
+def inc1_int32_t(np.ndarray[np.int32_t] arr):           arr[1] += 1
+def inc1_float64_t(np.ndarray[np.float64_t] arr):       arr[1] += 1
+
+    
 def test_dtype(dtype, inc1):
     a = np.array([0, 10], dtype=dtype)
     inc1(a)