diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2014-03-31 10:57:25 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2014-03-31 10:57:25 -0600 |
commit | 5b265798b516dc7db710aa3942b97cc50df146fc (patch) | |
tree | c91a0c43fc42afc2ee82538a63431e864cfe45f5 /numpy/core | |
parent | fd0d7d204d234865c5a384e8ec1c3a0c9fe66e8f (diff) | |
parent | c53b0e4fb892c7bc6ebdc48e57e23a91da0f542e (diff) | |
download | numpy-5b265798b516dc7db710aa3942b97cc50df146fc.tar.gz |
Merge pull request #3664 from mdboom/structured_array_compare
structured arrays with different byteorders do not compare
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.c | 28 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 44 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 6 |
3 files changed, 50 insertions, 28 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 55b7c8023..1f6c3e3d5 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -1325,16 +1325,16 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) return Py_NotImplemented; } - _res = PyObject_RichCompareBool - ((PyObject *)PyArray_DESCR(self), - (PyObject *)PyArray_DESCR(array_other), - Py_EQ); - if (_res < 0) { + _res = PyArray_CanCastTypeTo(PyArray_DESCR(self), + PyArray_DESCR(array_other), + NPY_EQUIV_CASTING); + if (_res == 0) { Py_DECREF(result); Py_DECREF(array_other); - return NULL; + Py_INCREF(Py_False); + return Py_False; } - if (_res) { + else { Py_DECREF(result); result = _void_compare(self, array_other, cmp_op); } @@ -1386,16 +1386,16 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) return Py_NotImplemented; } - _res = PyObject_RichCompareBool( - (PyObject *)PyArray_DESCR(self), - (PyObject *)PyArray_DESCR(array_other), - Py_EQ); - if (_res < 0) { + _res = PyArray_CanCastTypeTo(PyArray_DESCR(self), + PyArray_DESCR(array_other), + NPY_EQUIV_CASTING); + if (_res == 0) { Py_DECREF(result); Py_DECREF(array_other); - return NULL; + Py_INCREF(Py_True); + return Py_True; } - if (_res) { + else { Py_DECREF(result); result = _void_compare(self, array_other, cmp_op); Py_DECREF(array_other); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 0de7dff9d..c6d3c632f 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1400,7 +1400,10 @@ array_putmask(PyObject *NPY_UNUSED(module), PyObject *args, PyObject *kwds) static int _equivalent_fields(PyObject *field1, PyObject *field2) { - int same, val; + int val; + Py_ssize_t ppos; + PyObject *keys, *key; + PyObject *tuple1, *tuple2; if (field1 == field2) { return 1; @@ -1408,20 +1411,33 @@ _equivalent_fields(PyObject *field1, PyObject *field2) { if (field1 == NULL || field2 == NULL) { return 0; } -#if defined(NPY_PY3K) - val = PyObject_RichCompareBool(field1, field2, Py_EQ); - if (val != 1 || PyErr_Occurred()) { -#else - val = PyObject_Compare(field1, field2); - if (val != 0 || PyErr_Occurred()) { -#endif - same = 0; + + if (PyDict_Size(field1) != PyDict_Size(field2)) { + return 0; } - else { - same = 1; + + /* Iterate over all the fields and compare for equivalency */ + ppos = 0; + while (PyDict_Next(field1, &ppos, &key, &tuple1)) { + if ((tuple2 = PyDict_GetItem(field2, key)) == NULL) { + return 0; + } + /* Compare the dtype of the field for equivalency */ + if (!PyArray_CanCastTypeTo((PyArray_Descr *)PyTuple_GET_ITEM(tuple1, 0), + (PyArray_Descr *)PyTuple_GET_ITEM(tuple2, 0), + NPY_EQUIV_CASTING)) { + return 0; + } + /* Compare the byte position of the field */ + if (PyObject_RichCompareBool(PyTuple_GET_ITEM(tuple1, 1), + PyTuple_GET_ITEM(tuple2, 1), + Py_EQ) != 1) { + PyErr_Clear(); + return 0; + } } - PyErr_Clear(); - return same; + + return 1; } /* @@ -2097,7 +2113,7 @@ array_matrixproduct(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject* kwds) PyObject *v, *a, *o = NULL; char* kwlist[] = {"a", "b", "out", NULL }; PyObject *module; - + if (cached_npy_dot == NULL) { module = PyImport_ImportModule("numpy.core.multiarray"); cached_npy_dot = (PyUFuncObject*)PyDict_GetItemString( diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index cefdb03e5..729295398 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -623,6 +623,12 @@ class TestStructured(TestCase): y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')]) assert_equal(x == y, False) + # Check that structured arrays that are different only in + # byte-order work + a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i8'), ('b', '<f8')]) + b = np.array([(5, 43), (10, 1)], dtype=[('a', '<i8'), ('b', '>f8')]) + assert_equal(a == b, [False, True]) + class TestBool(TestCase): def test_test_interning(self): |