Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: fix-result-types
Changeset: r77459:5c9551b5f9f3
Date: 2015-05-21 20:27 +0100
http://bitbucket.org/pypy/pypy/changeset/5c9551b5f9f3/

Log:    test np.result_type(); fix

diff --git a/pypy/module/micronumpy/casting.py 
b/pypy/module/micronumpy/casting.py
--- a/pypy/module/micronumpy/casting.py
+++ b/pypy/module/micronumpy/casting.py
@@ -37,6 +37,12 @@
             dtypes_w.append(dtype)
     return find_result_type(space, arrays_w, dtypes_w)
 
+simple_kind_ordering = {
+    Bool.kind: 0, ULong.kind: 1, Long.kind: 1,
+    Float64.kind: 2, Complex64.kind: 2,
+    NPY.STRINGLTR: 3, NPY.STRINGLTR2: 3,
+    UnicodeType.kind: 3, VoidType.kind: 3, ObjectType.kind: 3}
+
 
 def find_result_type(space, arrays_w, dtypes_w):
     # equivalent to PyArray_ResultType
@@ -50,17 +56,18 @@
     max_array_kind = 0
     for w_array in arrays_w:
         if w_array.is_scalar():
-            kind = kind_ordering[w_array.get_dtype().kind]
+            kind = simple_kind_ordering[w_array.get_dtype().kind]
             if kind > max_scalar_kind:
                 max_scalar_kind = kind
         else:
             all_scalars = False
-            kind = kind_ordering[w_array.get_dtype().kind]
+            kind = simple_kind_ordering[w_array.get_dtype().kind]
             if kind > max_array_kind:
                 max_array_kind = kind
     if arrays_w:
         for dtype in dtypes_w:
-            kind = kind_ordering[dtype.kind]
+            all_scalars = False
+            kind = simple_kind_ordering[dtype.kind]
             if kind > max_array_kind:
                 max_array_kind = kind
     use_min_scalar = bool(arrays_w) and not all_scalars and max_array_kind >= 
max_scalar_kind
diff --git a/pypy/module/micronumpy/test/test_casting.py 
b/pypy/module/micronumpy/test/test_casting.py
--- a/pypy/module/micronumpy/test/test_casting.py
+++ b/pypy/module/micronumpy/test/test_casting.py
@@ -133,6 +133,13 @@
         assert np.promote_types('i4', 'S8') == np.dtype('S11')
         assert np.promote_types('f4', 'S8') == np.dtype('S32')
 
+    def test_result_type(self):
+        import numpy as np
+        assert np.result_type(np.uint8, np.int8) == np.int16
+        assert np.result_type(np.uint16(1), np.int8(0)) == np.int32
+        assert np.result_type(np.uint16(1), np.int8(0), np.uint8) == np.uint8
+        assert np.result_type(-1, np.uint8, 1) == np.int16
+
 def test_can_cast_same_type(space):
     dt_bool = get_dtype_cache(space).w_booldtype
     assert can_cast_type(space, dt_bool, dt_bool, 'no')
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to