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

numpy-svn at scipy.org numpy-svn at scipy.org
Tue Jan 19 13:58:19 EST 2010


Author: oliphant
Date: 2010-01-19 12:58:18 -0600 (Tue, 19 Jan 2010)
New Revision: 8074

Modified:
   trunk/numpy/core/src/multiarray/arraytypes.c.src
   trunk/numpy/core/tests/test_multiarray.py
Log:
Fix VOID_compare to handle the case of byteswapped data.  Fixes Ticket #1366.

Modified: trunk/numpy/core/src/multiarray/arraytypes.c.src
===================================================================
--- trunk/numpy/core/src/multiarray/arraytypes.c.src	2010-01-17 20:44:28 UTC (rev 8073)
+++ trunk/numpy/core/src/multiarray/arraytypes.c.src	2010-01-19 18:58:18 UTC (rev 8074)
@@ -2608,6 +2608,7 @@
  * in not_equal.
  *
  * Must align data passed on to sub-comparisons.
+ * Also must swap data based on to sub-comparisons.
  */
 static int
 VOID_compare(char *ip1, char *ip2, PyArrayObject *ap)
@@ -2616,7 +2617,7 @@
     PyObject *names, *key;
     PyObject *tup, *title;
     char *nip1, *nip2;
-    int i, offset, res = 0;
+    int i, offset, res = 0, swap=0;
 
     if (!PyArray_HASFIELDS(ap)) {
         return STRING_compare(ip1, ip2, ap);
@@ -2634,18 +2635,21 @@
             goto finish;
         }
         ap->descr = new;
+        swap = PyArray_ISBYTESWAPPED(ap);
         nip1 = ip1+offset;
         nip2 = ip2+offset;
-        if (new->alignment > 1) {
-            if (((intp)(nip1) % new->alignment) != 0) {
+        if ((swap) || (new->alignment > 1)) {
+            if ((swap) || (((intp)(nip1) % new->alignment) != 0)) {
                 /* create buffer and copy */
                 nip1 = _pya_malloc(new->elsize);
                 if (nip1 == NULL) {
                     goto finish;
                 }
                 memcpy(nip1, ip1+offset, new->elsize);
+                if (swap)
+                    new->f->copyswap(nip1, NULL, swap, ap);
             }
-            if (((intp)(nip2) % new->alignment) != 0) {
+            if ((swap) || (((intp)(nip2) % new->alignment) != 0)) {
                 /* copy data to a buffer */
                 nip2 = _pya_malloc(new->elsize);
                 if (nip2 == NULL) {
@@ -2655,10 +2659,12 @@
                     goto finish;
                 }
                 memcpy(nip2, ip2+offset, new->elsize);
+                if (swap)
+                    new->f->copyswap(nip2, NULL, swap, ap);
             }
         }
         res = new->f->compare(nip1, nip2, ap);
-        if (new->alignment > 1) {
+        if ((swap) || (new->alignment > 1)) {
             if (nip1 != ip1+offset) {
                 _pya_free(nip1);
             }

Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py	2010-01-17 20:44:28 UTC (rev 8073)
+++ trunk/numpy/core/tests/test_multiarray.py	2010-01-19 18:58:18 UTC (rev 8074)
@@ -410,6 +410,16 @@
         assert_equal(r.word, array(['my','first','name']))
         assert_equal(r.number, array([3.1,4.5,6.2]))
 
+        if sys.byteorder == 'little':
+            strtype = '>i2'
+        else:
+            strtype = '<i2'
+        r = np.array([('a', 1),('b', 255), ('c', 3), ('d', 258)],
+                     dtype=[('name', 'S5'),('col2',strtype)])
+        r.sort(order='col2')
+        assert_equal(r['col2'], [1, 3, 255, 258])
+        assert_equal(r, [('a', 1), ('c', 3), ('b', 255), ('d', 258)])
+
     def test_argsort(self):
         # all c scalar argsorts use the same code with different types
         # so it suffices to run a quick check with one type. The number




More information about the Numpy-svn mailing list