Commits

Justin Peel committed cc5e673

add ability to get dtypes with types (e.g. dtype(bool) or array([1],bool))

  • Participants
  • Parent commits 959ca3a
  • Branches numpy-dtype

Comments (0)

Files changed (2)

pypy/module/micronumpy/interp_dtype.py

 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.objspace.std.boolobject import W_BoolObject
+from pypy.objspace.std.floatobject import W_FloatObject
+from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.longobject import W_LongObject
 from pypy.rlib.rarithmetic import r_int, r_uint, LONG_BIT, LONGLONG_BIT
 from pypy.rpython.lltypesystem import lltype, rffi
 
         raise OperationError(space.w_ValueError,
                             space.wrap("type not recognized"))
     elif space.is_true(space.isinstance(w_string_or_type, space.w_type)):
-        # XXX: need to implement this
-        return Float64_dtype
-    else:
-        raise OperationError(space.w_TypeError,
+        if space.is_w(w_string_or_type, space.gettypeobject(W_IntObject.typedef)):
+            return Long_dtype
+        if space.is_w(w_string_or_type, space.gettypeobject(W_LongObject.typedef)):
+            return Int64_dtype
+        if space.is_w(w_string_or_type, space.gettypeobject(W_FloatObject.typedef)):
+            return Float64_dtype
+        if space.is_w(w_string_or_type, space.gettypeobject(W_BoolObject.typedef)):
+            return Bool_dtype
+    raise OperationError(space.w_TypeError,
                             space.wrap("data type not understood"))
 
 def find_result_dtype(d1, d2):

pypy/module/micronumpy/test/test_dtypes.py

         d = dtype('l')
         assert d.num == 7
         assert d.kind == 'i'
+
+    def test_dtype_with_types(self):
+        from numpy import dtype
+        assert dtype(bool).num == 0
+        assert dtype(int).num == 7
+        assert dtype(long).num == 9
+        assert dtype(float).num == 12
     
     def test_bool_array(self):
         from numpy import array
         types = ('?','b','B','h','H','i','I','l','L','q','Q','f','d','g')
         dtypes = [dtype(t) for t in types]
         N = len(types)
-        a = array([True],'?')
+        a = array([True], '?')
         for i in xrange(N):
             assert (a + array([0], types[i])).dtype is dtypes[i]
 # need more tests for binop result types