summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2008-05-15 02:06:28 +0000
committerTravis Oliphant <oliphant@enthought.com>2008-05-15 02:06:28 +0000
commit04fb428606f1af32f2de28c1c2afe2dadf9e7747 (patch)
tree2e324be092b53b1dc6b93543aa1262aff4d2c365 /numpy
parent9bd719a3ecf017e8fd64211597d9a0559502bc2b (diff)
downloadnumpy-04fb428606f1af32f2de28c1c2afe2dadf9e7747.tar.gz
Fix the unit tests and eliminate the dimensionality reduction assumption for non base-class arrays.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/arrayobject.c30
-rw-r--r--numpy/core/tests/test_defmatrix.py2
2 files changed, 30 insertions, 2 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index 51abf8e02..d83f7bda8 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -7082,6 +7082,11 @@ discover_itemsize(PyObject *s, int nd, int *itemsize)
int n, r, i;
PyObject *e;
+ if (PyArray_Check(s)) {
+ *itemsize = MAX(*itemsize, PyArray_ITEMSIZE(s));
+ return 0;
+ }
+
n = PyObject_Length(s);
if ((nd == 0) || PyString_Check(s) ||
@@ -7112,6 +7117,14 @@ discover_dimensions(PyObject *s, int nd, intp *d, int check_it)
PyObject *e;
int r, n, i, n_lower;
+
+ if (PyArray_Check(s)) {
+ for (i=0; i<nd; i++) {
+ d[i] = PyArray_DIM(s,i);
+ }
+ return 0;
+ }
+
n=PyObject_Length(s);
*d = n;
if (*d < 0) {
@@ -7430,15 +7443,30 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max)
static int
setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, intp offset)
{
- Py_ssize_t i, slen = PySequence_Length(s);
+ Py_ssize_t i, slen;
int res = 0;
+ /* This code is to ensure that the sequence access below will
+ return a lower-dimensional sequence.
+ */
+ if (PyArray_Check(s) && !(PyArray_CheckExact(s))) {
+ /* FIXME: This could probably copy the entire subarray
+ at once here using a faster algorithm.
+ Right now, just make sure a base-class array
+ is used so that the dimensionality reduction assumption
+ is correct.
+ */
+ s = PyArray_EnsureArray(s);
+ }
+
if (dim > a->nd) {
PyErr_Format(PyExc_ValueError,
"setArrayFromSequence: sequence/array dimensions mismatch.");
return -1;
}
+ slen = PySequence_Length(s);
+
if (slen != a->dimensions[dim]) {
PyErr_Format(PyExc_ValueError,
"setArrayFromSequence: sequence/array shape mismatch.");
diff --git a/numpy/core/tests/test_defmatrix.py b/numpy/core/tests/test_defmatrix.py
index d8af2938b..04b2dd6bb 100644
--- a/numpy/core/tests/test_defmatrix.py
+++ b/numpy/core/tests/test_defmatrix.py
@@ -187,7 +187,7 @@ class TestNewScalarIndexing(NumpyTestCase):
def check_dimesions(self):
a = self.a
x = a[0]
- assert_equal(x.ndim, 1)
+ assert_equal(x.ndim, 2)
def check_array_from_matrix_list(self):
a = self.a