summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2008-08-24 02:26:44 +0000
committerTravis Oliphant <oliphant@enthought.com>2008-08-24 02:26:44 +0000
commita51e8e3a8c11f786706ce64bd46b5e31b99a6ea8 (patch)
tree1434c47772470306fc47d8597ed733659e9ef5f7 /numpy
parentb4d3f491d7dfefe00f18b7d31434d3156c49028c (diff)
downloadnumpy-a51e8e3a8c11f786706ce64bd46b5e31b99a6ea8.tar.gz
BUG: Fix to ticket #816: calling object_() Segfaults.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/scalartypes.inc.src19
-rw-r--r--numpy/core/tests/test_regression.py14
2 files changed, 29 insertions, 4 deletions
diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src
index fd47474af..764b7ba46 100644
--- a/numpy/core/src/scalartypes.inc.src
+++ b/numpy/core/src/scalartypes.inc.src
@@ -1889,7 +1889,7 @@ object_arrtype_dealloc(PyObject *v)
/**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 @@ static PyObject *
#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 @@ static PyObject *
/* 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 @@ finish:
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
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index b104b888c..ff9c7e2e6 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1184,5 +1184,19 @@ class TestRegression(TestCase):
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()