[pypy-commit] pypy default: enhance tests for numpypy string record/array to demonstrate existing bugs

bdkearns noreply at buildbot.pypy.org
Tue Feb 12 06:01:35 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r61115:c6a40e53bb2d
Date: 2013-02-11 23:46 -0500
http://bitbucket.org/pypy/pypy/changeset/c6a40e53bb2d/

Log:	enhance tests for numpypy string record/array to demonstrate
	existing bugs

diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -287,11 +287,11 @@
         for i in range(len(arg)):
             arr.storage[i] = arg[i]
         return W_StringBox(arr, 0, arr.dtype)
-            
+
     def convert_to(self, dtype):
         from pypy.module.micronumpy import types
         assert isinstance(dtype.itemtype, types.StringType)
-        return self        
+        return self
 
 class W_UnicodeBox(W_CharacterBox):
     def descr__new__unicode_box(space, w_subtype, w_arg):
@@ -308,7 +308,7 @@
     def convert_to(self, dtype):
         from pypy.module.micronumpy import types
         assert isinstance(dtype.itemtype, types.UnicodeType)
-        return self        
+        return self
 
 
 class W_ComplexFloatingBox(W_InexactBox):
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -449,7 +449,7 @@
             name="float64",
             char="d",
             w_box_type = space.gettypefor(interp_boxes.W_Float64Box),
-            alternate_constructors=[space.w_float, 
+            alternate_constructors=[space.w_float,
                                     space.gettypefor(interp_boxes.W_NumberBox),
                                    ],
             aliases=["float"],
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -2533,23 +2533,44 @@
 
     def test_string_record(self):
         from _numpypy import dtype, array
+
         d = dtype([('x', str), ('y', 'int32')])
-        assert d.fields['x'] == (dtype(str), 0)
-        assert d.fields['y'] == (dtype('int32'), 1)
+        assert str(d.fields['x'][0]) == '|S0'
+        assert d.fields['x'][1] == 0
+        assert str(d.fields['y'][0]) == 'int32'
+        assert d.fields['y'][1] == 0
+        assert d.name == 'void32'
+
+        a = array([('a', 2), ('cde', 1)], dtype=d)
+        assert a[0]['x'] == '\x02'
+        assert a[0]['y'] == 2
+        assert a[1]['x'] == '\x01'
+        assert a[1]['y'] == 1
+
         d = dtype([('x', 'S1'), ('y', 'int32')])
-        assert d.fields['x'] == (dtype(str), 0)
-        assert d.fields['y'] == (dtype('int32'), 1)
-        a = array([('a', 2), ('c', 1)], dtype=d)
+        assert str(d.fields['x'][0]) == '|S1'
+        assert d.fields['x'][1] == 0
+        assert str(d.fields['y'][0]) == 'int32'
+        assert d.fields['y'][1] == 1
+        assert d.name == 'void40'
+
+        a = array([('a', 2), ('cde', 1)], dtype=d)
+        assert a[0]['x'] == 'a'
+        assert a[0]['y'] == 2
+        assert a[1]['x'] == 'c'
         assert a[1]['y'] == 1
-        assert a[0]['x'] == 'a'
 
-    def test_stringarray(self):
+    def test_string_array(self):
         from _numpypy import array
-        a = array(['abc'],'S3')
-        assert str(a.dtype) == '|S3'
         a = array(['abc'])
         assert str(a.dtype) == '|S3'
-        a = array(['abc','defg','ab'])
+        a = array(['abc'], 'S')
+        assert str(a.dtype) == '|S3'
+        a = array(['abc'], 'S3')
+        assert str(a.dtype) == '|S3'
+        a = array(['abcde'], 'S3')
+        assert str(a.dtype) == '|S3'
+        a = array(['abc', 'defg', 'ab'])
         assert str(a.dtype) == '|S4'
         assert a[0] == 'abc'
         assert a[1] == 'defg'
@@ -2561,6 +2582,8 @@
         from _numpypy import array
         a = array('ffff')
         assert a.shape == ()
+        a = array([], dtype='S')
+        assert str(a.dtype) == '|S1'
         a = array('x', dtype='>S')
         assert str(a.dtype) == '|S1'
         a = array('x', dtype='c')
@@ -2581,14 +2604,14 @@
         s = repr(a)
         assert s.replace('\n', '') == \
                       "array(['abc', 'defg', 'ab'],       dtype='|S4')"
-        
-       
+
+
 class AppTestPyPy(BaseNumpyAppTest):
     def setup_class(cls):
         if option.runappdirect and '__pypy__' not in sys.builtin_module_names:
             py.test.skip("pypy only test")
         BaseNumpyAppTest.setup_class.im_func(cls)
-    
+
     def test_init_2(self):
         # this test is pypy only since in numpy it becomes an object dtype
         import _numpypy
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1019,17 +1019,17 @@
     def str_format(self, box):
         real, imag = self.for_computation(self.unbox(box))
         imag_str = str_format(imag) + 'j'
-        
+
         # (0+2j) => 2j
         if real == 0:
-            return imag_str        
+            return imag_str
 
         real_str = str_format(real)
         op = '+' if imag >= 0 else ''
         return ''.join(['(', real_str, op, imag_str, ')'])
 
     @staticmethod
-    def for_computation(v):   
+    def for_computation(v):
         return float(v[0]), float(v[1])
 
     @raw_unary_op
@@ -1101,7 +1101,7 @@
     @complex_binary_op
     def mul(self, v1, v2):
         return rcomplex.c_mul(v1, v2)
-    
+
     @complex_binary_op
     def div(self, v1, v2):
         try:


More information about the pypy-commit mailing list