[Numpy-svn] r5688 - in trunk/numpy/core: src tests

numpy-svn@scip... numpy-svn@scip...
Sat Aug 23 21:26:46 CDT 2008


Author: oliphant
Date: 2008-08-23 21:26:44 -0500 (Sat, 23 Aug 2008)
New Revision: 5688

Modified:
   trunk/numpy/core/src/scalartypes.inc.src
   trunk/numpy/core/tests/test_regression.py
Log:
BUG: Fix to ticket #816: calling object_() Segfaults.

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2008-08-24 01:31:08 UTC (rev 5687)
+++ trunk/numpy/core/src/scalartypes.inc.src	2008-08-24 02:26:44 UTC (rev 5688)
@@ -1889,7 +1889,7 @@
 /**begin repeat1
 #name=byte, short, int, long, longlong, ubyte, ushort, uint, ulong, ulonglong, float, double, longdouble, cfloat, cdouble, clongdouble, string, unicode, object#
 #TYPE=BYTE, SHORT, INT, LONG, LONGLONG, UBYTE, USHORT, UINT, ULONG, ULONGLONG, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE, OBJECT#
-#work=0,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,z,z,1#
+#work=0,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,z,z,0#
 #default=0*16,1*2,2#
 */
 static PyObject *
@@ -1923,13 +1923,18 @@
 #elif @default@ == 1
         robj = PyArray_Scalar(NULL, typecode, NULL);
 #elif @default@ == 2
-        obj = Py_None;
-        robj = PyArray_Scalar(&obj, typecode, NULL);
+	obj = Py_None;
+	robj = type->tp_alloc(type, 0);
+	Py_INCREF(Py_None);
+	((PyObjectScalarObject *)robj)->obval = obj;
 #endif
 	Py_DECREF(typecode);
         goto finish;
     }
 
+    /* It is expected at this point that robj is a PyArrayScalar 
+       (even for Object Data Type)
+    */
     arr = PyArray_FromAny(obj, typecode, 0, 0, FORCECAST, NULL);
     if ((arr == NULL) || (PyArray_NDIM(arr) > 0)) {
         return arr;
@@ -1937,6 +1942,12 @@
     /* 0-d array */
     robj = PyArray_ToScalar(PyArray_DATA(arr), (NPY_AO *)arr);
     Py_DECREF(arr);  
+#if @default@ == 2  /* In OBJECT case, robj is no longer a
+			  PyArrayScalar at this point but the
+			  remaining code assumes it is 
+		       */
+    return robj;
+#endif
 
 finish:
     /* Normal return */
@@ -1972,7 +1983,7 @@
 	itemsize = ((PyUnicodeObject *)robj)->length * sizeof(Py_UNICODE);
     }
     memcpy(dest, src, itemsize);
-#elif @default@ == 2 /* Object arrays */
+#elif @default@ == 2 /* Object arrays */    
     memcpy(dest, src, sizeof(void *));
     Py_INCREF(*((PyObject **)dest));
 #endif

Modified: trunk/numpy/core/tests/test_regression.py
===================================================================
--- trunk/numpy/core/tests/test_regression.py	2008-08-24 01:31:08 UTC (rev 5687)
+++ trunk/numpy/core/tests/test_regression.py	2008-08-24 02:26:44 UTC (rev 5688)
@@ -1184,5 +1184,19 @@
         assert a.dtype.names[0] == "notfoo"
         assert a.dtype.names[1] == "bar"
 
+    def test_for_object_scalar_creation(self, level=rlevel):
+        """Ticket #816"""
+        a = np.object_()
+        b = np.object_(3)
+        b2 = np.object_(3.0)
+        c = np.object_([4,5])
+        d = np.object_([None, {}, []])
+        assert a is None
+        assert type(b) is int
+        assert type(b2) is float
+        assert type(c) is np.ndarray
+        assert c.dtype == object
+        assert d.type == object
+
 if __name__ == "__main__":
     run_module_suite()



More information about the Numpy-svn mailing list