Author: Sergey Matyunin <[email protected]>
Branch: numpy_broadcast
Changeset: r83711:7f688202899a
Date: 2016-04-08 13:07 +0200
http://bitbucket.org/pypy/pypy/changeset/7f688202899a/

Log:    In W_Broadcast (micronumpy) added check for number of arguments,
        added numiter property

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -19,9 +19,14 @@
     def descr_new_broadcast(space, w_subtype, __args__):
         return W_Broadcast(space, __args__.arguments_w)
 
-    def __init__(self, space, w_args):
+    def __init__(self, space, args):
+        num_args = len(args)
+        if not (2 <= num_args <= NPY.MAXARGS):
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("Need at least two and fewer than 
(%d) array objects." % NPY.MAXARGS))
+
         self.seq = [convert_to_array(space, w_elem)
-                    for w_elem in w_args]
+                    for w_elem in args]
 
         self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
                                      len(self.seq), parse_op_flag)
@@ -72,6 +77,9 @@
     def descr_get_index(self, space):
         return space.wrap(self.index)
 
+    def descr_get_numiter(self, space):
+        return space.wrap(len(self.iters))
+
     @jit.unroll_safe
     def descr_next(self, space):
         if self.index >= self.size:
@@ -97,4 +105,5 @@
                               
shape=GetSetProperty(W_Broadcast.descr_get_shape),
                               size=GetSetProperty(W_Broadcast.descr_get_size),
                               
index=GetSetProperty(W_Broadcast.descr_get_index),
+                              
numiter=GetSetProperty(W_Broadcast.descr_get_numiter),
                               )
diff --git a/pypy/module/micronumpy/constants.py 
b/pypy/module/micronumpy/constants.py
--- a/pypy/module/micronumpy/constants.py
+++ b/pypy/module/micronumpy/constants.py
@@ -77,6 +77,8 @@
 WRAP = 1
 RAISE = 2
 
+MAXARGS = 32
+
 # These can be requested in constructor functions and tested for
 ARRAY_C_CONTIGUOUS = 0x0001
 ARRAY_F_CONTIGUOUS = 0x0002
diff --git a/pypy/module/micronumpy/test/test_broadcast.py 
b/pypy/module/micronumpy/test/test_broadcast.py
--- a/pypy/module/micronumpy/test/test_broadcast.py
+++ b/pypy/module/micronumpy/test/test_broadcast.py
@@ -73,3 +73,16 @@
                      (2, 40, 700), (2, 40, 800), (2, 50, 700), (2, 50, 800),
                      (3, 40, 700), (3, 40, 800), (3, 50, 700), (3, 50, 800)]
 
+    def test_number_of_arguments(self):
+        """
+        Test from numpy unit tests.
+        """
+        import numpy as np
+        arr = np.empty((5,))
+        for j in range(35):
+            arrs = [arr] * j
+            if j < 2 or j > 32:
+                raises(ValueError, np.broadcast, *arrs)
+            else:
+                mit = np.broadcast(*arrs)
+                assert mit.numiter == j
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to