[Numpy-svn] r8141 - in trunk/numpy: compat core/tests

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 12:06:35 CST 2010


Author: ptvirtan
Date: 2010-02-20 12:06:35 -0600 (Sat, 20 Feb 2010)
New Revision: 8141

Modified:
   trunk/numpy/compat/py3k.py
   trunk/numpy/core/tests/test_multiarray.py
   trunk/numpy/core/tests/test_numerictypes.py
   trunk/numpy/core/tests/test_records.py
   trunk/numpy/core/tests/test_regression.py
   trunk/numpy/core/tests/test_unicode.py
Log:
3K: core: adjust some tests vs. str/bytes and int inheritance issues

Modified: trunk/numpy/compat/py3k.py
===================================================================
--- trunk/numpy/compat/py3k.py	2010-02-20 18:06:15 UTC (rev 8140)
+++ trunk/numpy/compat/py3k.py	2010-02-20 18:06:35 UTC (rev 8141)
@@ -3,7 +3,8 @@
 
 """
 
-__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar']
+__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
+           'asunicode']
 
 import sys
 
@@ -14,6 +15,7 @@
         if isinstance(s, bytes):
             return s
         return s.encode('iso-8859-1')
+    asunicode = str
     def isfileobj(f):
         return isinstance(f, io.IOBase)
     strchar = 'U'
@@ -23,6 +25,10 @@
     strchar = 'S'
     def isfileobj(f):
         return isinstance(f, file)
+    def asunicode(s):
+        if isinstance(s, unicode):
+            return s
+        return s.decode('iso-8859-1')
 
 def getexception():
     return sys.exc_info()[1]

Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py	2010-02-20 18:06:15 UTC (rev 8140)
+++ trunk/numpy/core/tests/test_multiarray.py	2010-02-20 18:06:35 UTC (rev 8141)
@@ -545,7 +545,9 @@
 class TestSubscripting(TestCase):
     def test_test_zero_rank(self):
         x = array([1,2,3])
-        self.failUnless(isinstance(x[0], int))
+        self.failUnless(isinstance(x[0], np.int_))
+        if sys.version_info[0] < 3:
+            self.failUnless(isinstance(x[0], int))
         self.failUnless(type(x[0, ...]) is ndarray)
 
 
@@ -1087,8 +1089,8 @@
         def test_unicode_field_names(self):
             # Unicode field names are not allowed on Py2
             title = unicode('b')
-            assert_raises(ValueError, np.dtype, [(title, int)])
-            assert_raises(ValueError, np.dtype, [(('a', title), int)])
+            assert_raises(TypeError, np.dtype, [(title, int)])
+            assert_raises(TypeError, np.dtype, [(('a', title), int)])
 
 class TestView(TestCase):
     def test_basic(self):

Modified: trunk/numpy/core/tests/test_numerictypes.py
===================================================================
--- trunk/numpy/core/tests/test_numerictypes.py	2010-02-20 18:06:15 UTC (rev 8140)
+++ trunk/numpy/core/tests/test_numerictypes.py	2010-02-20 18:06:35 UTC (rev 8141)
@@ -1,5 +1,6 @@
 import sys
 from numpy.testing import *
+from numpy.compat import asbytes, asunicode
 import numpy as np
 
 # This is the structure of the table used for plain objects:
@@ -57,8 +58,8 @@
     # x     Info                                                color info        y                  z
     #       value y2 Info2                            name z2         Name Value
     #                name   value    y3       z3
-    ([3,2], (6j, 6., ('nn', [6j,4j], [6.,4.], [1,2]), 'NN', True), 'cc', ('NN', 6j), [[6.,4.],[6.,4.]], 8),
-    ([4,3], (7j, 7., ('oo', [7j,5j], [7.,5.], [2,1]), 'OO', False), 'dd', ('OO', 7j), [[7.,5.],[7.,5.]], 9),
+    ([3,2], (6j, 6., (asbytes('nn'), [6j,4j], [6.,4.], [1,2]), asbytes('NN'), True), asbytes('cc'), (asunicode('NN'), 6j), [[6.,4.],[6.,4.]], 8),
+    ([4,3], (7j, 7., (asbytes('oo'), [7j,5j], [7.,5.], [2,1]), asbytes('OO'), False), asbytes('dd'), (asunicode('OO'), 7j), [[7.,5.],[7.,5.]], 9),
     ]
 
 

Modified: trunk/numpy/core/tests/test_records.py
===================================================================
--- trunk/numpy/core/tests/test_records.py	2010-02-20 18:06:15 UTC (rev 8140)
+++ trunk/numpy/core/tests/test_records.py	2010-02-20 18:06:35 UTC (rev 8141)
@@ -1,6 +1,7 @@
 from os import path
 import numpy as np
 from numpy.testing import *
+from numpy.compat import asbytes, asunicode
 
 class TestFromrecords(TestCase):
     def test_fromrecords(self):
@@ -9,7 +10,7 @@
         assert_equal(r[0].item(), (456, 'dbe', 1.2))
 
     def test_method_array(self):
-        r = np.rec.array('abcdefg' * 100, formats='i2,a3,i4', shape=3, byteorder='big')
+        r = np.rec.array(asbytes('abcdefg') * 100, formats='i2,a3,i4', shape=3, byteorder='big')
         assert_equal(r[1].item(), (25444, 'efg', 1633837924))
 
     def test_method_array2(self):
@@ -34,7 +35,7 @@
     def test_recarray_fromfile(self):
         data_dir = path.join(path.dirname(__file__), 'data')
         filename = path.join(data_dir, 'recarray_from_file.fits')
-        fd = open(filename)
+        fd = open(filename, 'rb')
         fd.seek(2880 * 2)
         r = np.rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big')
 

Modified: trunk/numpy/core/tests/test_regression.py
===================================================================
--- trunk/numpy/core/tests/test_regression.py	2010-02-20 18:06:15 UTC (rev 8140)
+++ trunk/numpy/core/tests/test_regression.py	2010-02-20 18:06:35 UTC (rev 8141)
@@ -6,6 +6,7 @@
 from os import path
 from numpy.testing import *
 from numpy.testing.utils import _assert_valid_refcount
+from numpy.compat import asbytes, asunicode
 import numpy as np
 
 rlevel = 1
@@ -793,7 +794,7 @@
         """Ticket 702"""
         class MyFloat:
             def __float__(self):
-                return 1
+                return 1.0
 
         tmp = np.atleast_1d([MyFloat()])
         tmp2 = tmp.astype(float)
@@ -991,7 +992,7 @@
         for i in range(1,9) :
             msg = 'unicode offset: %d chars'%i
             t = np.dtype([('a','S%d'%i),('b','U2')])
-            x = np.array([('a',u'b')], dtype=t)
+            x = np.array([(asbytes('a'),u'b')], dtype=t)
             assert_equal(str(x), "[('a', u'b')]", err_msg=msg)
 
     def test_sign_for_complex_nan(self, level=rlevel):

Modified: trunk/numpy/core/tests/test_unicode.py
===================================================================
--- trunk/numpy/core/tests/test_unicode.py	2010-02-20 18:06:15 UTC (rev 8140)
+++ trunk/numpy/core/tests/test_unicode.py	2010-02-20 18:06:35 UTC (rev 8141)
@@ -2,17 +2,29 @@
 
 from numpy.testing import *
 from numpy.core import *
+from numpy.compat import asbytes
 
 # Guess the UCS length for this python interpreter
 if sys.version_info[0] >= 3:
-    import warnings
-    warnings.warn('XXX: how to detect UCS size on Py3?')
-    ucs4 = True
+    import array as _array
+    ucs4 = (_array.array('u').itemsize == 4)
+    def buffer_length(arr):
+        if isinstance(arr, unicode):
+            return _array.array('u').itemsize * len(arr)
+        v = memoryview(arr)
+        if v.shape is None:
+            return len(v) * v.itemsize
+        else:
+            return prod(v.shape) * v.itemsize
 else:
     if len(buffer(u'u')) == 4:
         ucs4 = True
     else:
         ucs4 = False
+    def buffer_length(arr):
+        if isinstance(arr, ndarray):
+            return len(arr.data)
+        return len(buffer(arr))
 
 # Value that can be represented in UCS2 interpreters
 ucs2_value = u'\uFFFF'
@@ -32,16 +44,16 @@
         # Check the length of the unicode base type
         self.assert_(int(ua.dtype.str[2:]) == self.ulen)
         # Check the length of the data buffer
-        self.assert_(len(ua.data) == nbytes)
+        self.assert_(buffer_length(ua) == nbytes)
         # Small check that data in array element is ok
         self.assert_(ua_scalar == u'')
         # Encode to ascii and double check
-        self.assert_(ua_scalar.encode('ascii') == '')
+        self.assert_(ua_scalar.encode('ascii') == asbytes(''))
         # Check buffer lengths for scalars
         if ucs4:
-            self.assert_(len(buffer(ua_scalar)) == 0)
+            self.assert_(buffer_length(ua_scalar) == 0)
         else:
-            self.assert_(len(buffer(ua_scalar)) == 0)
+            self.assert_(buffer_length(ua_scalar) == 0)
 
     def test_zeros0D(self):
         """Check creation of 0-dimensional objects"""
@@ -84,7 +96,7 @@
         # Check the length of the unicode base type
         self.assert_(int(ua.dtype.str[2:]) == self.ulen)
         # Check the length of the data buffer
-        self.assert_(len(ua.data) == nbytes)
+        self.assert_(buffer_length(ua) == nbytes)
         # Small check that data in array element is ok
         self.assert_(ua_scalar == self.ucs_value*self.ulen)
         # Encode to UTF-8 and double check
@@ -92,16 +104,16 @@
                      (self.ucs_value*self.ulen).encode('utf-8'))
         # Check buffer lengths for scalars
         if ucs4:
-            self.assert_(len(buffer(ua_scalar)) == 4*self.ulen)
+            self.assert_(buffer_length(ua_scalar) == 4*self.ulen)
         else:
             if self.ucs_value == ucs4_value:
                 # In UCS2, the \U0010FFFF will be represented using a
                 # surrogate *pair*
-                self.assert_(len(buffer(ua_scalar)) == 2*2*self.ulen)
+                self.assert_(buffer_length(ua_scalar) == 2*2*self.ulen)
             else:
                 # In UCS2, the \uFFFF will be represented using a
                 # regular 2-byte word
-                self.assert_(len(buffer(ua_scalar)) == 2*self.ulen)
+                self.assert_(buffer_length(ua_scalar) == 2*self.ulen)
 
     def test_values0D(self):
         """Check creation of 0-dimensional objects with values"""
@@ -169,7 +181,7 @@
         # Check the length of the unicode base type
         self.assert_(int(ua.dtype.str[2:]) == self.ulen)
         # Check the length of the data buffer
-        self.assert_(len(ua.data) == nbytes)
+        self.assert_(buffer_length(ua) == nbytes)
         # Small check that data in array element is ok
         self.assert_(ua_scalar == self.ucs_value*self.ulen)
         # Encode to UTF-8 and double check
@@ -177,16 +189,16 @@
                      (self.ucs_value*self.ulen).encode('utf-8'))
         # Check buffer lengths for scalars
         if ucs4:
-            self.assert_(len(buffer(ua_scalar)) == 4*self.ulen)
+            self.assert_(buffer_length(ua_scalar) == 4*self.ulen)
         else:
             if self.ucs_value == ucs4_value:
                 # In UCS2, the \U0010FFFF will be represented using a
                 # surrogate *pair*
-                self.assert_(len(buffer(ua_scalar)) == 2*2*self.ulen)
+                self.assert_(buffer_length(ua_scalar) == 2*2*self.ulen)
             else:
                 # In UCS2, the \uFFFF will be represented using a
                 # regular 2-byte word
-                self.assert_(len(buffer(ua_scalar)) == 2*self.ulen)
+                self.assert_(buffer_length(ua_scalar) == 2*self.ulen)
 
     def test_values0D(self):
         """Check assignment of 0-dimensional objects with values"""



More information about the Numpy-svn mailing list