[pypy-commit] pypy numpy-dtype: code clean-up and add one test

justinpeel noreply at buildbot.pypy.org
Wed Aug 3 18:58:09 CEST 2011


Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-dtype
Changeset: r46249:c9320ca675c9
Date: 2011-08-03 10:58 -0600
http://bitbucket.org/pypy/pypy/changeset/c9320ca675c9/

Log:	code clean-up and add one test

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
@@ -180,33 +180,6 @@
     if space.is_true(space.isinstance(scalar, space.w_float)):
         return Float64_dtype
 
-def get_dtype(space, w_type, w_string_or_type):
-    if space.is_true(space.isinstance(w_string_or_type, space.gettypeobject(Dtype.typedef))):
-        return w_string_or_type
-    if space.is_true(space.isinstance(w_string_or_type, space.w_str)):
-        s = space.str_w(w_string_or_type)
-        if len(s) == 1:
-            typenum = _letters_to_nums[ord(s)]
-            dtype = _dtype_list[typenum]
-            if typenum != -1 and dtype is not None:
-                return _dtype_list[typenum]
-        # XXX: can improve this part. will need to for endianness
-        if s in num_dict:
-            return _dtype_list[num_dict[s]]
-        raise OperationError(space.w_ValueError,
-                            space.wrap("type not recognized"))
-    elif space.is_true(space.isinstance(w_string_or_type, space.w_type)):
-        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):
     # this function is for determining the result dtype of bin ops, etc.
     # it is kind of a mess so feel free to improve it
@@ -243,8 +216,35 @@
             return Float64_dtype
     return dtype2
 
+def get_dtype(space, str_or_type):
+    if space.is_true(space.isinstance(str_or_type, space.gettypeobject(Dtype.typedef))):
+        return str_or_type
+    if space.is_true(space.isinstance(str_or_type, space.w_str)):
+        s = space.str_w(str_or_type)
+        if len(s) == 1:
+            typenum = _letters_to_nums[ord(s)]
+            dtype = _dtype_list[typenum]
+            if typenum != -1 and dtype is not None:
+                return _dtype_list[typenum]
+        # XXX: can improve this part. will need to for endianness
+        if s in num_dict:
+            return _dtype_list[num_dict[s]]
+        raise OperationError(space.w_ValueError,
+                            space.wrap("type not recognized"))
+    elif space.is_true(space.isinstance(str_or_type, space.w_type)):
+        if space.is_w(str_or_type, space.gettypeobject(W_IntObject.typedef)):
+            return Long_dtype
+        if space.is_w(str_or_type, space.gettypeobject(W_LongObject.typedef)):
+            return Int64_dtype
+        if space.is_w(str_or_type, space.gettypeobject(W_FloatObject.typedef)):
+            return Float64_dtype
+        if space.is_w(str_or_type, space.gettypeobject(W_BoolObject.typedef)):
+            return Bool_dtype
+    raise OperationError(space.w_TypeError,
+                            space.wrap("data type not understood"))
+
 def descr_new_dtype(space, w_type, w_string_or_type):
-    return space.wrap(get_dtype(space, w_type, w_string_or_type))
+    return space.wrap(get_dtype(space, w_string_or_type))
 
 Dtype.typedef = TypeDef(
     'numpy.dtype',
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -574,7 +574,7 @@
 
 def new_numarray(space, iterable, dtype):
     l = space.listview(iterable)
-    dtype = get_dtype(space, Dtype, dtype)
+    dtype = get_dtype(space, dtype)
     arr = SingleDimArray(len(l), dtype)
     i = 0
     unwrap = dtype.unwrap
@@ -588,14 +588,14 @@
     # this isn't such a great check. We should improve it including exceptions.
     # Also needs to be able to handle keywords better
     iterable = __args__.arguments_w[0]
-    if __args__.keywords:
+    if len(__args__.arguments_w) == 2:
+        dtype = __args__.arguments_w[1]
+    elif __args__.keywords:
         if __args__.keywords[0] == 'dtype':
             dtype = __args__.keywords_w[0]
         else:
             msg = "array() got unexpected keyword argument"
             raise OperationError(space.w_TypeError, space.wrap(msg))
-    elif len(__args__.arguments_w) == 2:
-        dtype = __args__.arguments_w[1]
     else:
         # can just use the dtype for float for now. We need to actually be
         # able to determine the base dtype of an iterable
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -10,6 +10,7 @@
         assert d.kind == 'i'
         assert dtype('int8').num == 1
         assert dtype('i1').num == 1
+        assert dtype(d) is d
 
     def test_dtype_with_types(self):
         from numpy import dtype


More information about the pypy-commit mailing list