[pypy-svn] r71715 - in pypy/branch/micronumpy/pypy/module/micronumpy: . test

dan at codespeak.net dan at codespeak.net
Wed Mar 3 22:02:18 CET 2010


Author: dan
Date: Wed Mar  3 22:02:08 2010
New Revision: 71715

Modified:
   pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py
   pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
Log:
Added a few tests for interplevel code.

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py	Wed Mar  3 22:02:08 2010
@@ -19,49 +19,6 @@
 def coerce_float32(space, w_x):
     return unwrap_float32(space, space.float(w_x))
 
-def typecode(space, w_type):
-    try:
-        assert isinstance(w_type, DynamicType)
-        return w_type.code
-    except AssertionError, e: pass
-
-    try:
-        return space.str_w(w_type)
-    except OperationError, e:
-        typecode_mapping = {
-                            space.w_int: 'i',
-                            space.w_float: 'd',
-                           }
-        try:
-            return typecode_mapping[w_type]
-        except IndexError, e:
-            raise OperationError(space.w_TypeError,
-                    space.wrap("Can't understand type."))
-
-result_types = {
-                ('i', 'i'): 'i',
-                ('i', 'd'): 'd',
-                ('d', 'i'): 'd',
-                ('d', 'd'): 'd',
-               }
-
-def result_mapping(space, types):
-    types = (typecode(space, types[0]),
-             typecode(space, types[1]))
-    return result_types[types]
-
-def iterable_type(space, w_xs):
-    xs = space.fixedview(w_xs)
-    result_type = 'i'
-    for i in range(len(xs)):
-        try:
-            atype = iterable_type(space, xs[i])
-        except OperationError, e:
-            if not e.match(space, space.w_TypeError):
-                raise
-            atype = typecode(space, space.type(xs[i]))
-        result_type = result_types[(result_type, atype)]
-    return result_type
 
 def create_factory(result_factory):
     def factory(t):
@@ -81,23 +38,68 @@
             try:
                 code = space.str_w(w_x)
                 if self.code == code:
-                    return space.wrap(True)
+                    return space.w_True
                 elif self.name == code:
-                    return space.wrap(True)
+                    return space.w_True
                 else:
-                    return space.wrap(False)
+                    return space.w_False
             except OperationError, e:
-                return space.wrap(False)
+                return space.w_False
             except TypeError, e:
-                return space.wrap(False) #FIXME: need to throw applevel type error
+                return space.w_False #FIXME: need to throw applevel type error
     descr_eq.unwrap_spec = ['self', ObjSpace, W_Root]
 DynamicType.typedef = TypeDef('dtype',
                               __eq__ = interp2app(DynamicType.descr_eq),
                              )
 
 class DynamicTypes(object):
+    result_types = {
+                    ('i', 'i'): 'i',
+                    ('i', 'd'): 'd',
+                    ('d', 'i'): 'd',
+                    ('d', 'd'): 'd',
+                   }
+
     def __init__(self):
         self.dtypes = {}
+
+    def typecode(self, space, w_type):
+        try:
+            assert isinstance(w_type, DynamicType)
+            return w_type.code
+        except AssertionError, e: pass
+
+        try:
+            return space.str_w(w_type)
+        except OperationError, e:
+            typecode_mapping = {
+                                space.w_int: 'i',
+                                space.w_float: 'd',
+                               }
+            try:
+                return typecode_mapping[w_type]
+            except IndexError, e:
+                raise OperationError(space.w_TypeError,
+                        space.wrap("Can't understand type."))
+
+    def result_mapping(self, space, types):
+        types = (self.typecode(space, types[0]),
+                 self.typecode(space, types[1]))
+        return self.result_types[types]
+
+    def iterable_type(self, space, w_xs):
+        xs = space.fixedview(w_xs)
+        result_type = 'i'
+        for i in range(len(xs)):
+            try:
+                atype = self.iterable_type(space, xs[i])
+            except OperationError, e:
+                if not e.match(space, space.w_TypeError):
+                    raise
+                atype = self.typecode(space, space.type(xs[i]))
+            result_type = self.result_types[(result_type, atype)]
+        return result_type
+
     def verify_dtype_dict(self, space):
         if not self.dtypes:
             self.dtypes.update(
@@ -116,11 +118,22 @@
             t = space.str_w(w_type)
         except OperationError, e:
             if e.match(space, space.w_TypeError):
-                t = typecode(space, w_type)
+                t = self.typecode(space, w_type)
             else:
                 raise
         return self.retrieve_dtype(space, t)
 
+    def sdarray(self, space, length, w_dtype):
+        from pypy.module.micronumpy.sdarray import sdresult
+        return sdresult(self.typecode(space))(space, length, w_dtype)
+
+    def mdarray(self, space, shape, w_dtype):
+        from pypy.module.micronumpy.mdarray import mdresult
+        return mdresult(self.typecode(space))(space, shape, w_dtype)
+
 dtypes = DynamicTypes()
+iterable_type = dtypes.iterable_type
+typecode = dtypes.typecode
+result_mapping = dtypes.result_mapping
 get_dtype = dtypes.get_dtype
 retrieve_dtype = dtypes.retrieve_dtype

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	Wed Mar  3 22:02:08 2010
@@ -244,5 +244,40 @@
         assert compare(ar[0], ar[2])
         assert compare(ar[..., 0], [0, 3, 0])
 
+class TestDType(object):
+    def test_lookups(self, space):
+        from pypy.module.micronumpy.dtype import retrieve_dtype
+        from pypy.module.micronumpy.dtype import get_dtype
+        a = get_dtype(space, space.wrap('i'))
+        b = get_dtype(space, space.wrap('d'))
 
-            
+        assert a == retrieve_dtype(space, 'i')
+        assert b == retrieve_dtype(space, 'd')
+
+    def test_result_types(self, space):
+        from pypy.module.micronumpy.dtype import get_dtype
+        from pypy.module.micronumpy.dtype import result_mapping
+        w_typecode_a = space.wrap('i')
+        w_typecode_b = space.wrap('d')
+        a = get_dtype(space, w_typecode_a)
+        b = get_dtype(space, w_typecode_b)
+
+        assert 'i' == result_mapping(space, (w_typecode_a, w_typecode_a))
+        assert 'd' == result_mapping(space, (w_typecode_b, w_typecode_a))
+        assert 'd' == result_mapping(space, (w_typecode_a, w_typecode_b))
+        assert 'd' == result_mapping(space, (w_typecode_b, w_typecode_b))
+
+    def test_iterable_type(self, space):
+        from pypy.module.micronumpy.dtype import iterable_type
+        w_int = space.wrap(1)
+        w_float = space.wrap(2.0)
+
+        data = [(space.wrap([1, 2, 3, 4, 5]), 'i'),
+                (space.wrap([1, 2, 3.0, 4, 5]), 'd'),
+                (space.wrap([1, 2.0, 3.0, 4, 5]), 'd'),
+                (space.wrap([1.0, 2, 3, 4, 5]), 'd'),
+                (space.wrap([1, 2, 3, 4, 5.0]), 'd'),
+                (space.wrap([1.0, 2, 3, 4, 5.0]), 'd')]
+
+        for w_xs, typecode in data:
+            assert typecode == iterable_type(space, w_xs)



More information about the Pypy-commit mailing list