[pypy-commit] pypy fix-result-types: Start reimplementing np.result_type() to make it compatible with cnumpy

rlamy noreply at buildbot.pypy.org
Sat May 9 17:42:00 CEST 2015


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: fix-result-types
Changeset: r77250:6a670ec95449
Date: 2015-05-09 16:40 +0100
http://bitbucket.org/pypy/pypy/changeset/6a670ec95449/

Log:	Start reimplementing np.result_type() to make it compatible with
	cnumpy and create find_result_type() as an equivalent of
	PyArray_ResultType

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
@@ -20,12 +20,31 @@
     if not args_w:
         raise oefmt(space.w_ValueError,
             "at least one array or dtype is required")
+    arrays_w = []
+    dtypes_w = []
+    for w_arg in args_w:
+        if isinstance(w_arg, W_NDimArray):
+            arrays_w.append(w_arg)
+        elif is_scalar_w(space, w_arg):
+            w_scalar = as_scalar(space, w_arg)
+            w_arr = W_NDimArray.from_scalar(space, w_scalar)
+            arrays_w.append(w_arr)
+        else:
+            dtype = as_dtype(space, w_arg)
+            dtypes_w.append(dtype)
+    return find_result_type(space, arrays_w, dtypes_w)
+
+
+def find_result_type(space, arrays_w, dtypes_w):
+    # equivalent to PyArray_ResultType
     result = None
-    for w_arg in args_w:
-        dtype = as_dtype(space, w_arg)
+    for w_array in arrays_w:
+        result = find_binop_result_dtype(space, result, w_array.get_dtype())
+    for dtype in dtypes_w:
         result = find_binop_result_dtype(space, result, dtype)
     return result
 
+
 @unwrap_spec(casting=str)
 def can_cast(space, w_from, w_totype, casting='safe'):
     try:


More information about the pypy-commit mailing list