[Numpy-svn] r3022 - in trunk/numpy: . core/include/numpy core/src lib/tests numarray oldnumeric testing tests

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Aug 14 18:15:52 CDT 2006


Author: oliphant
Date: 2006-08-14 18:15:21 -0500 (Mon, 14 Aug 2006)
New Revision: 3022

Added:
   trunk/numpy/tests/
   trunk/numpy/tests/test_ctypeslib.py
Removed:
   trunk/numpy/lib/tests/test_utils.py
Modified:
   trunk/numpy/core/include/numpy/arrayobject.h
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/multiarraymodule.c
   trunk/numpy/core/src/scalarmathmodule.c.src
   trunk/numpy/core/src/scalartypes.inc.src
   trunk/numpy/ctypeslib.py
   trunk/numpy/numarray/alter_code1.py
   trunk/numpy/numarray/alter_code2.py
   trunk/numpy/oldnumeric/alter_code1.py
   trunk/numpy/oldnumeric/alter_code2.py
   trunk/numpy/setup.py
   trunk/numpy/testing/numpytest.py
Log:
Fix Python 2.5 compatibility to work with new b3 release

Modified: trunk/numpy/core/include/numpy/arrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/arrayobject.h	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/core/include/numpy/arrayobject.h	2006-08-14 23:15:21 UTC (rev 3022)
@@ -644,8 +644,10 @@
 #define PY_SSIZE_T_MAX INT_MAX
 #define PY_SSIZE_T_MIN INT_MIN
 #define NPY_SSIZE_T_PYFMT "i"
+#define constchar const char
 #else
 #define NPY_SSIZE_T_PYFMT "n"
+#define constchar char
 #endif
 
 #if SIZEOF_PY_INTPTR_T == SIZEOF_INT

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/core/src/arrayobject.c	2006-08-14 23:15:21 UTC (rev 3022)
@@ -3036,7 +3036,7 @@
 }
 
 static Py_ssize_t
-array_getcharbuf(PyArrayObject *self, Py_ssize_t segment, const char **ptrptr)
+array_getcharbuf(PyArrayObject *self, Py_ssize_t segment, constchar **ptrptr)
 {
         if (self->descr->type_num == PyArray_STRING || \
             self->descr->type_num == PyArray_UNICODE)
@@ -3790,6 +3790,20 @@
                               PyArray_Copy(self));
 }
 
+#if PY_VERSION_HEX >= 0x02050000
+static PyObject *
+array_index(PyArrayObject *v)
+{
+        if (PyArray_SIZE(v) != 1 || !PyArray_ISINTEGER(v)) {
+                PyErr_SetString(PyExc_TypeError, "only length-1 integer " \
+                                "arrays can be converted to an index");
+                return NULL;
+        }
+        return v->descr->f->getitem(v->data, v);
+}
+#endif
+
+
 static PyNumberMethods array_as_number = {
         (binaryfunc)array_add,              /*nb_add*/
         (binaryfunc)array_subtract,                 /*nb_subtract*/
@@ -3834,6 +3848,10 @@
         (binaryfunc)array_inplace_floor_divide,  /*nb_inplace_floor_divide*/
         (binaryfunc)array_inplace_true_divide,   /*nb_inplace_true_divide*/
 
+#if PY_VERSION_HEX >= 0x02050000
+        (unaryfunc)array_index,                /* nb_index */
+#endif         
+
 };
 
 /****************** End of Buffer Protocol *******************************/

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-08-14 23:15:21 UTC (rev 3022)
@@ -6118,7 +6118,7 @@
 static unsigned int
 PyArray_GetNDArrayCVersion(void)
 {
-	return (unsigned int)NDARRAY_VERSION;
+	return (unsigned int)NPY_VERSION;
 }
 
 static PyObject *

Modified: trunk/numpy/core/src/scalarmathmodule.c.src
===================================================================
--- trunk/numpy/core/src/scalarmathmodule.c.src	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/core/src/scalarmathmodule.c.src	2006-08-14 23:15:21 UTC (rev 3022)
@@ -712,8 +712,8 @@
 /**end repeat**/
 
 /**begin repeat
-   #name=(float,double,longdouble,cfloat,cdouble,clongdouble)*6#
-   #oper=lshift*6, rshift*6, and*6, or*6, xor*6, index*6#
+   #name=(float,double,longdouble,cfloat,cdouble,clongdouble)*5#
+   #oper=lshift*6, rshift*6, and*6, or*6, xor*6#
 **/
 #define @name at _@oper@ NULL
 /**end repeat**/
@@ -819,21 +819,7 @@
 /**end repeat**/
 
 
-#if PY_VERSION_HEX >= 0x02050000
 /**begin repeat
-   #name=byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong#
-   #Name=Byte, UByte, Short, UShort, Int, UInt, Long, ULong, LongLong, ULongLong#
-**/
-static Py_ssize_t
- at name@_index(PyObject *a)
-{
-    return PyArrayScalar_VAL(a, @Name@);    
-}
-/**end repeat**/
-#endif
-
-
-/**begin repeat
    #oper=le,ge,lt,gt,eq,ne#
    #op=<=,>=,<,>,==,!=#
 **/
@@ -937,7 +923,7 @@
     0,                                         /*nb_inplace_floor_divide*/
     0,                                         /*nb_inplace_true_divide*/
 #if PY_VERSION_HEX >= 0x02050000
-    (lenfunc)@name at _index,                     /*nb_index*/
+    (unaryfunc)NULL,                      /*nb_index*/
 #endif
 };
 /**end repeat**/
@@ -951,6 +937,9 @@
        #name=byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble#
        #NAME=Byte, UByte, Short, UShort, Int, UInt, Long, ULong, LongLong, ULongLong, Float, Double, LongDouble, CFloat, CDouble, CLongDouble#
     **/
+#if PY_VERSION_HEX >= 0x02050000
+    @name at _as_number.nb_index = Py at NAME@ArrType_Type.tp_as_number->nb_index;
+#endif
     Py at NAME@ArrType_Type.tp_as_number = &(@name at _as_number);
     Py at NAME@ArrType_Type.tp_richcompare = @name at _richcompare;
     /**end repeat**/

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/core/src/scalartypes.inc.src	2006-08-14 23:15:21 UTC (rev 3022)
@@ -593,24 +593,7 @@
 }
 /**end repeat**/
 
-#if PY_VERSION_HEX >= 0x02050000
-/* This needs a better implementation */
-static Py_ssize_t
-gentype_index(PyObject *self)
-{
-        PyObject *obj;
-        if (!(PyArray_IsScalar(self, Integer))) {
-                PyErr_SetString(PyExc_TypeError,
-                                "not an integer type.");
-                return -1;
-        }
-        obj = gentype_int(self);
-        if (obj == NULL) return -1;
-        return PyInt_AsSsize_t(obj);
-}
-#endif
 
-
 static PyNumberMethods gentype_as_number = {
         (binaryfunc)gentype_add,                    /*nb_add*/
         (binaryfunc)gentype_subtract,               /*nb_subtract*/
@@ -651,7 +634,7 @@
         0,                                         /*nb_inplace_floor_divide*/
         0,                                         /*nb_inplace_true_divide*/
 #if PY_VERSION_HEX >= 0x02050000
-        (lenfunc)gentype_index,                 /* nb_index */
+        (unaryfunc)NULL,                 /* nb_index */
 #endif
 };
 
@@ -1641,7 +1624,7 @@
 }
 
 static int
-gentype_getcharbuf(PyObject *self, int segment, const char **ptrptr)
+gentype_getcharbuf(PyObject *self, int segment, constchar **ptrptr)
 {
         if (PyArray_IsScalar(self, String) ||   \
             PyArray_IsScalar(self, Unicode))
@@ -1795,6 +1778,26 @@
         return a == PyArrayScalar_True;
 }
 
+#if PY_VERSION_HEX >= 0x02050000
+/**begin repeat
+#name=byte, short, int, long, ubyte, ushort, longlong, uint, ulong, ulonglong#
+#Name=Byte, Short, Int, Long, UByte, UShort, LongLong, UInt, ULong, ULongLong#
+#type=PyInt_FromLong*6, PyLong_FromLongLong*1, PyLong_FromUnsignedLong*2, PyLong_FromUnsignedLongLong# 
+*/
+static PyNumberMethods @name at _arrtype_as_number;
+static PyObject *
+ at name@_index(PyObject *self)
+{
+        return @type@(PyArrayScalar_VAL(self, @Name@));
+}
+/**end repeat**/
+static PyObject *
+bool_index(PyObject *a)
+{
+        return PyInt_FromLong(PyArrayScalar_VAL(a, Bool));
+}
+#endif
+
 /* Arithmetic methods -- only so we can override &, |, ^. */
 static PyNumberMethods bool_arrtype_as_number = {
         0,                                      /* nb_add */
@@ -2154,7 +2157,7 @@
 
 static Py_ssize_t
 object_arrtype_getcharbuf(PyObjectScalarObject *self, Py_ssize_t segment,
-                          const char **ptrptr)
+                          constchar **ptrptr)
 {
         PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer;
 
@@ -2336,7 +2339,21 @@
         PyGenericArrType_Type.tp_richcompare = gentype_richcompare;
 
         PyBoolArrType_Type.tp_as_number = &bool_arrtype_as_number;
+#if PY_VERSION_HEX >= 0x02050000
+        /* need to add dummy versions with filled-in nb_index
+           in-order for PyType_Ready to fill in .__index__() method
+        */
+        /**begin repeat
+#name=byte, short, int, long, longlong, ubyte, ushort, uint, ulong, ulonglong#
+#NAME=Byte, Short, Int, Long, LongLong, UByte, UShort, UInt, ULong, ULongLong#
+        */
+        Py at NAME@ArrType_Type.tp_as_number = &@name at _arrtype_as_number;
+        Py at NAME@ArrType_Type.tp_as_number->nb_index = (unaryfunc)@name at _index;
 
+        /**end repeat**/
+        PyBoolArrType_Type.tp_as_number->nb_index = (unaryfunc)bool_index;
+#endif
+
         PyStringArrType_Type.tp_alloc = NULL;
         PyStringArrType_Type.tp_free = NULL;
 

Modified: trunk/numpy/ctypeslib.py
===================================================================
--- trunk/numpy/ctypeslib.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/ctypeslib.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -1,5 +1,8 @@
 __all__ = ['ctypes_load_library', 'ndpointer']
 
+import sys, os
+from numpy import integer, product, ndarray, dtype as _dtype
+from numpy.core.multiarray import _flagdict, flagsobj
 
 # Adapted from Albert Strasheim
 def ctypes_load_library(libname, loader_path):

Deleted: trunk/numpy/lib/tests/test_utils.py
===================================================================
--- trunk/numpy/lib/tests/test_utils.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/lib/tests/test_utils.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -1,62 +0,0 @@
-from numpy.testing import * 
-set_package_path() 
-import numpy as N 
-restore_path() 
- 
-class test_ndpointer(NumpyTestCase): 
-    def check_dtype(self): 
-        dt = N.intc 
-        p = N.ndpointer(dtype=dt) 
-        self.assert_(p.from_param(N.array([1], dt))) 
-        dt = '<i4' 
-        p = N.ndpointer(dtype=dt) 
-        self.assert_(p.from_param(N.array([1], dt))) 
-        dt = N.dtype('>i4') 
-        p = N.ndpointer(dtype=dt) 
-        p.from_param(N.array([1], dt)) 
-        self.assertRaises(TypeError, p.from_param, 
-                          N.array([1], dt.newbyteorder('swap'))) 
-        dtnames = ['x', 'y'] 
-        dtformats = [N.intc, N.float64] 
-        dtdescr = {'names' : dtnames, 'formats' : dtformats} 
-        dt = N.dtype(dtdescr) 
-        p = N.ndpointer(dtype=dt) 
-        self.assert_(p.from_param(N.zeros((10,), dt))) 
-        samedt = N.dtype(dtdescr) 
-        p = N.ndpointer(dtype=samedt) 
-        self.assert_(p.from_param(N.zeros((10,), dt))) 
-        dt2 = N.dtype(dtdescr, align=True)
-        if dt.itemsize != dt2.itemsize:
-            self.assertRaises(TypeError, p.from_param, N.zeros((10,), dt2))
-        else:
-            self.assert_(p.from_param(N.zeros((10,), dt2)))
- 
-    def check_ndim(self): 
-        p = N.ndpointer(ndim=0) 
-        self.assert_(p.from_param(N.array(1))) 
-        self.assertRaises(TypeError, p.from_param, N.array([1])) 
-        p = N.ndpointer(ndim=1) 
-        self.assertRaises(TypeError, p.from_param, N.array(1)) 
-        self.assert_(p.from_param(N.array([1]))) 
-        p = N.ndpointer(ndim=2) 
-        self.assert_(p.from_param(N.array([[1]]))) 
-         
-    def check_shape(self): 
-        p = N.ndpointer(shape=(1,2)) 
-        self.assert_(p.from_param(N.array([[1,2]]))) 
-        self.assertRaises(TypeError, p.from_param, N.array([[1],[2]])) 
-        p = N.ndpointer(shape=()) 
-        self.assert_(p.from_param(N.array(1))) 
- 
-    def check_flags(self): 
-        x = N.array([[1,2,3]], order='F') 
-        p = N.ndpointer(flags='FORTRAN') 
-        self.assert_(p.from_param(x)) 
-        p = N.ndpointer(flags='CONTIGUOUS') 
-        self.assertRaises(TypeError, p.from_param, x) 
-        p = N.ndpointer(flags=x.flags.num) 
-        self.assert_(p.from_param(x)) 
-        self.assertRaises(TypeError, p.from_param, N.array([[1,2,3]])) 
- 
-if __name__ == "__main__": 
-    NumpyTest().run() 

Modified: trunk/numpy/numarray/alter_code1.py
===================================================================
--- trunk/numpy/numarray/alter_code1.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/numarray/alter_code1.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -52,7 +52,7 @@
    - .setimaginary() --> .imag
    
 """
-__all__ = ['fromfile', 'fromstr', 'convertfile', 'convertall']
+__all__ = ['convertfile', 'convertall']
 
 import sys
 import os

Modified: trunk/numpy/numarray/alter_code2.py
===================================================================
--- trunk/numpy/numarray/alter_code2.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/numarray/alter_code2.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -3,7 +3,7 @@
 with numpy
 
 """
-#__all__ = ['fromfile', 'fromstr', 'convertfile', 'convertall']
+#__all__ = ['convertfile', 'convertall']
 __all__ = []
 
 import warnings

Modified: trunk/numpy/oldnumeric/alter_code1.py
===================================================================
--- trunk/numpy/oldnumeric/alter_code1.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/oldnumeric/alter_code1.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -28,7 +28,7 @@
  * Converts uses of type(...) is <type>
    isinstance(..., <type>)
 """
-__all__ = ['fromfile', 'fromstr', 'convertfile', 'convertall']
+__all__ = ['convertfile', 'convertall']
 
 import sys
 import os

Modified: trunk/numpy/oldnumeric/alter_code2.py
===================================================================
--- trunk/numpy/oldnumeric/alter_code2.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/oldnumeric/alter_code2.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -17,7 +17,7 @@
    oldnumeric.random_array, and oldnumeric.fft
 
 """
-#__all__ = ['fromfile', 'fromstr', 'convertfile', 'convertall']
+#__all__ = ['convertfile', 'convertall']
 __all__ = []
 
 import warnings

Modified: trunk/numpy/setup.py
===================================================================
--- trunk/numpy/setup.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/setup.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -15,6 +15,7 @@
     config.add_subpackage('linalg')
     config.add_subpackage('random')
     config.add_data_dir('doc')
+    config.add_data_dir('tests')
     config.make_config_py() # installs __config__.py
     return config
 

Modified: trunk/numpy/testing/numpytest.py
===================================================================
--- trunk/numpy/testing/numpytest.py	2006-08-14 21:30:52 UTC (rev 3021)
+++ trunk/numpy/testing/numpytest.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -452,6 +452,8 @@
                 continue
             modules.append(module)
 
+        
+
         self.test_files = []
         suites = []
         for module in modules:

Copied: trunk/numpy/tests/test_ctypeslib.py (from rev 3019, trunk/numpy/lib/tests/test_utils.py)
===================================================================
--- trunk/numpy/lib/tests/test_utils.py	2006-08-14 20:27:39 UTC (rev 3019)
+++ trunk/numpy/tests/test_ctypeslib.py	2006-08-14 23:15:21 UTC (rev 3022)
@@ -0,0 +1,63 @@
+from numpy.testing import * 
+set_package_path() 
+import numpy as N 
+from numpy.ctypeslib import ndpointer
+restore_path() 
+ 
+class test_ndpointer(NumpyTestCase): 
+    def check_dtype(self): 
+        dt = N.intc 
+        p = ndpointer(dtype=dt) 
+        self.assert_(p.from_param(N.array([1], dt))) 
+        dt = '<i4' 
+        p = ndpointer(dtype=dt) 
+        self.assert_(p.from_param(N.array([1], dt))) 
+        dt = N.dtype('>i4') 
+        p = ndpointer(dtype=dt) 
+        p.from_param(N.array([1], dt)) 
+        self.assertRaises(TypeError, p.from_param, 
+                          N.array([1], dt.newbyteorder('swap'))) 
+        dtnames = ['x', 'y'] 
+        dtformats = [N.intc, N.float64] 
+        dtdescr = {'names' : dtnames, 'formats' : dtformats} 
+        dt = N.dtype(dtdescr) 
+        p = ndpointer(dtype=dt) 
+        self.assert_(p.from_param(N.zeros((10,), dt))) 
+        samedt = N.dtype(dtdescr) 
+        p = ndpointer(dtype=samedt) 
+        self.assert_(p.from_param(N.zeros((10,), dt))) 
+        dt2 = N.dtype(dtdescr, align=True)
+        if dt.itemsize != dt2.itemsize:
+            self.assertRaises(TypeError, p.from_param, N.zeros((10,), dt2))
+        else:
+            self.assert_(p.from_param(N.zeros((10,), dt2)))
+ 
+    def check_ndim(self): 
+        p = ndpointer(ndim=0) 
+        self.assert_(p.from_param(N.array(1))) 
+        self.assertRaises(TypeError, p.from_param, N.array([1])) 
+        p = ndpointer(ndim=1) 
+        self.assertRaises(TypeError, p.from_param, N.array(1)) 
+        self.assert_(p.from_param(N.array([1]))) 
+        p = ndpointer(ndim=2) 
+        self.assert_(p.from_param(N.array([[1]]))) 
+         
+    def check_shape(self): 
+        p = ndpointer(shape=(1,2)) 
+        self.assert_(p.from_param(N.array([[1,2]]))) 
+        self.assertRaises(TypeError, p.from_param, N.array([[1],[2]])) 
+        p = ndpointer(shape=()) 
+        self.assert_(p.from_param(N.array(1))) 
+ 
+    def check_flags(self): 
+        x = N.array([[1,2,3]], order='F') 
+        p = ndpointer(flags='FORTRAN') 
+        self.assert_(p.from_param(x)) 
+        p = ndpointer(flags='CONTIGUOUS') 
+        self.assertRaises(TypeError, p.from_param, x) 
+        p = ndpointer(flags=x.flags.num) 
+        self.assert_(p.from_param(x)) 
+        self.assertRaises(TypeError, p.from_param, N.array([[1,2,3]])) 
+ 
+if __name__ == "__main__": 
+    NumpyTest().run() 



More information about the Numpy-svn mailing list