[pypy-commit] pypy numpypy-frompyfunc: tests, translate passes

mattip noreply at buildbot.pypy.org
Wed Dec 14 21:48:39 CET 2011


Author: mattip
Branch: numpypy-frompyfunc
Changeset: r50520:66a6917800a8
Date: 2011-12-14 22:47 +0200
http://bitbucket.org/pypy/pypy/changeset/66a6917800a8/

Log:	tests, translate passes

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
@@ -1552,15 +1552,18 @@
 
 
 class W_FromPyFunc(Wrappable):
+    _attrs_ = ['nIn', 'nOut', 'signature']
+    _immutable_fields_ = ['nIn', 'nOut', 'signature']
+
     def __init__(self, space, w_func, w_nIn, w_nOut):
         self.nIn = space.int_w(w_nIn)
         self.nOut = space.int_w(w_nOut)
         if self.nOut != 1:
             raise OperationError(space.w_NotImplementedError, space.wrap(''))
-        self.signature = signature.CallPyFunc(w_func)
+        self.signature = signature.CallPyFunc(space, w_func)
+        # TODO: once we have lazy eval kill next line
         # should check that the nIn and nOut match the function signature,
         # but how?
-        self.w_func = w_func
 
     def descr__new__(space, w_subtype, w_func, w_nIn, w_nOut):
         return space.wrap(W_FromPyFunc(space, w_func, w_nIn, w_nOut))
@@ -1570,9 +1573,9 @@
             raise OperationError(space.w_ValueError, space.wrap(
                 'invalid number of arguments'))
         if self.nIn == 0:
-            return space.wrap(space.call_function(self.w_func))
+            return space.call(self.signature.w_func, space.newlist([]))
         arr_s = [convert_to_array(space, a) for a in args_w]
-        result = W_NDimArray(arr_s[0].find_size(), arr_s[0].shape[:], 
+        result = W_NDimArray(arr_s[0].find_size(), arr_s[0].shape[:],
                 dtype=arr_s[0].find_dtype(), order=arr_s[0].order)
         i_s = [a.start_iter() for a in arr_s]
         ri = result.start_iter()
@@ -1584,11 +1587,14 @@
         while not ri.done():
             if len(arr_s) == 1:
                 result.dtype.setitem(result.storage, ri.offset,
-                      space.call_function(self.w_func, arr_s[0].eval(i_s[0])))
+                       space.call_function(signature.w_func,
+                                           arr_s[0].eval(i_s[0])))
             else:
-                result.dtype.setitem(result.storage, ri.offset, 
-                    space.call_function(self.w_func, 
-                                        *[a.eval(i) for a,i in zip(arr_s, i_s)]))
+                w_fargs = space.newlist([arr_s[0].eval(i_s[0])])
+                for j in range(1, len(arr_s)):
+                    space.call_method(w_fargs, "append", arr_s[j].eval(i_s[j]))
+                result.dtype.setitem(result.storage, ri.offset, space.call(
+                        signature.w_func, w_fargs))
             i_s = [i.next(shapelen) for i in i_s]
             ri = ri.next(shapelen)
         return space.wrap(result)
diff --git a/pypy/module/micronumpy/signature.py b/pypy/module/micronumpy/signature.py
--- a/pypy/module/micronumpy/signature.py
+++ b/pypy/module/micronumpy/signature.py
@@ -56,6 +56,6 @@
 class CallPyFunc(BaseSignature):
     _immutable_fields_ = ["func", "name"]
 
-    def __init__(self, func):
-        self.func = func
-        self.name = func.name
+    def __init__(self, space, w_func):
+        self.w_func = w_func
+        self.name = w_func.getname(space)
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
@@ -378,11 +378,20 @@
         from numpypy import frompyfunc
         ufunc = frompyfunc(abs, 1, 1)
         assert (ufunc([-1, 0, 3, 15]) == [1, 0, 3, 15]).all()
+    
+    def test_frompyfunc_nullfunc(self):
+        def foo():
+            return 165.0
+        from numpypy import frompyfunc
+        ufunc = frompyfunc(foo, 0, 1)
+        assert ufunc() == 165.0
 
     def test_frompyfunc_foo(self):
         from numpypy import frompyfunc, array
+
         def foo(x):
             return x * x + 1
+
         def bar(x):
             return x + 1
         ufunc = frompyfunc(foo, 1, 1)
@@ -392,19 +401,22 @@
         assert (ufunc(range(10)) == array(range(10)) * range(10) + 1).all()
         #but messing with the func_code WILL change it: numpy is sensitive
         #to this in the same way
+
         def foo(x):
             return x * x + 1
+
         def bar(x):
             return x + 1
-        from numpypy import frompyfunc, array
         ufunc = frompyfunc(foo, 1, 1)
         assert (ufunc(range(10)) == array(range(10)) * range(10) + 1).all()
         foo.func_code = bar.func_code
         assert not (ufunc(range(10)) == array(range(10)) * range(10) + 1).all()
+
     def test_frompyfunc_broadcast(self):
         from numpypy import frompyfunc, array
+
         def foo(x, y):
             return x * y + 1
         ufunc = frompyfunc(foo, 2, 1)
-        assert (ufunc(range(10),range(10)) == array(range(10)) * range(10) + 1).all()
-       
+        assert (ufunc(range(10), range(10)) == \
+                            array(range(10)) * range(10) + 1).all()


More information about the pypy-commit mailing list