From f7ca8b0d1341816ffe26d29bace1201bfe981588 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Sat, 16 Aug 2008 00:38:34 +0200 Subject: [PATCH] numpy.pxd: Added type_t typedefs corresponding to predefined dtypes --- Cython/Compiler/Buffer.py | 3 +- Cython/Compiler/Nodes.py | 1 + Cython/Includes/numpy.pxd | 70 +++++++++++++++++++++++++++++---------- tests/run/tnumpy.pyx | 24 +++++++++++++- 4 files changed, 78 insertions(+), 20 deletions(-) diff --git a/Cython/Compiler/Buffer.py b/Cython/Compiler/Buffer.py index c71a0a2c..9d322f25 100644 --- a/Cython/Compiler/Buffer.py +++ b/Cython/Compiler/Buffer.py @@ -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]) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 7705997b..053d7d23 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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( diff --git a/Cython/Includes/numpy.pxd b/Cython/Includes/numpy.pxd index 97bb3037..db7d4b9c 100644 --- a/Cython/Includes/numpy.pxd +++ b/Cython/Includes/numpy.pxd @@ -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 diff --git a/tests/run/tnumpy.pyx b/tests/run/tnumpy.pyx index 07432792..418981d4 100644 --- a/tests/run/tnumpy.pyx +++ b/tests/run/tnumpy.pyx @@ -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) -- 2.26.2