Author: mattip <[email protected]>
Branch: ufuncapi
Changeset: r74766:c2354e730694
Date: 2014-11-30 22:55 +0200
http://bitbucket.org/pypy/pypy/changeset/c2354e730694/

Log:    more tests, start to fix type_resolver and alloc_outargs

diff --git a/pypy/module/micronumpy/test/test_ufuncs.py 
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -1,7 +1,10 @@
 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
 from pypy.module.micronumpy.ufuncs import (find_binop_result_dtype,
-        find_unaryop_result_dtype)
+        find_unaryop_result_dtype, W_UfuncGeneric)
+from pypy.module.micronumpy.support import _parse_signature
 from pypy.module.micronumpy.descriptor import get_dtype_cache
+from pypy.module.micronumpy.base import W_NDimArray
+from pypy.module.micronumpy.concrete import VoidBoxStorage
 from pypy.interpreter.gateway import interp2app
 from pypy.conftest import option
 
@@ -83,6 +86,42 @@
         # promote bools, happens with sign ufunc
         assert find_unaryop_result_dtype(space, bool_dtype, 
promote_bools=True) is int8_dtype
 
+
+class TestGenericUfuncOperation(object):
+    def test_signature_parser(self, space):
+        class Ufunc(object):
+            def __init__(self, nin, nout):
+                self.nin = nin
+                self.nout = nout
+                self.nargs = nin + nout
+                self.core_enabled = True
+                self.core_num_dim_ix = 0 
+                self.core_num_dims = [0] * self.nargs  
+                self.core_offsets = [0] * self.nargs
+                self.core_dim_ixs = [] 
+
+        u = Ufunc(2, 1)
+        _parse_signature(space, u, '(m,n), (n,r)->(m,r)')
+        assert u.core_dim_ixs == [0, 1, 1, 2, 0, 2]
+        assert u.core_num_dims == [2, 2, 2]
+        assert u.core_offsets == [0, 2, 4]
+
+    def test_type_resolver(self, space):
+        c128_dtype = get_dtype_cache(space).w_complex128dtype
+        f64_dtype = get_dtype_cache(space).w_float64dtype
+        u32_dtype = get_dtype_cache(space).w_uint32dtype
+        b_dtype = get_dtype_cache(space).w_booldtype
+
+        ufunc = W_UfuncGeneric(space, [None, None], 'eigenvals', None, 1, 1,
+                     [f64_dtype, c128_dtype, c128_dtype, c128_dtype],
+                     '')
+        f64 = W_NDimArray(VoidBoxStorage(0, f64_dtype))
+        c128 = W_NDimArray(VoidBoxStorage(0, c128_dtype))
+        index, dtypes = ufunc.type_resolver(space, [f64], [c128], 'd->D')
+        assert index == 0
+        assert dtypes == [f64_dtype, c128_dtype]
+
+
 class AppTestUfuncs(BaseNumpyAppTest):
     def test_constants(self):
         import numpy as np
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -571,8 +571,8 @@
                 outargs[i] = out
         if sig is None:
             sig = space.wrap(self.signature)
-        index = self.type_resolver(space, inargs, outargs, sig)
-        outargs = self.alloc_outargs(space, index, inargs, outargs)
+        index, dtypes = self.type_resolver(space, inargs, outargs, sig)
+        outargs = self.alloc_outargs(space, index, inargs, outargs, dtypes)
         for i in range(len(outargs)):
             assert isinstance(outargs[i], W_NDimArray)
         outargs0 = outargs[0]
@@ -805,23 +805,40 @@
             kwargs_w.pop(kw)
         return w_subok, w_out, sig, casting, extobj
 
-    def type_resolver(self, space, inargs, outargs, sig):
+    def type_resolver(self, space, inargs, outargs, type_tup):
         # Find a match for the inargs.dtype in self.dtypes, like
         # linear_search_type_resolver in numpy ufunc_type_resolutions.c
+        # type_tup can be '', a tuple of dtypes, or a string
+        # of the form d,t -> D where the letters are dtype specs
         inargs0 = inargs[0]
         assert isinstance(inargs0, W_NDimArray)
+        nop = len(inargs) + len(outargs)
+        dtypes = []
+        if isinstance(type_tup, str):
+            if len(type_tup) == 1:
+                dtypes = [get_dtype_cache(space).dtypes_by_name[type_tup]] * 
self.nargs
+            elif len(type_tup) == self.nargs + 2:
+                for i in range(len(inargs)):
+                    
dtypes.append(get_dtype_cache(space).dtype_by_name[type_tup[i]])
+                #skip the '->' in the signature
+                for i in range(len(self.nout)):
+                    
dtypes.append(get_dtype_cache(space).dtype_by_name[type_tup[i+2]])
+            else:
+                raise oefmt(space.w_TypeError, "a type-string for %s," \
+                    "requires 1 typecode or %d typecode(s) before and %d" \
+                    " after the -> sign", self.name, self.nin, self.nout)
         for i in range(0, len(self.dtypes), self.nargs):
             if inargs0.get_dtype() == self.dtypes[i]:
                 break
         else:
             if len(self.funcs) < 2:
-                return 0
+                return 0, dtypes
             raise oefmt(space.w_TypeError,
                          'input dtype %s did not match any known dtypes',
                                               str(inargs0.get_dtype()))
-        return i / self.nargs
+        return i / self.nargs, dtypes
 
-    def alloc_outargs(self, space, index, inargs, outargs):
+    def alloc_outargs(self, space, index, inargs, outargs, dtypes):
         # Any None outarg should be allocated here
         inargs0 = inargs[0]
         assert isinstance(inargs0, W_NDimArray)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to