[pypy-commit] pypy default: make numpy scalar non-iterable

yuyichao noreply at buildbot.pypy.org
Tue Jul 8 05:53:42 CEST 2014


Author: Yichao Yu <yyc1992 at gmail.com>
Branch: 
Changeset: r72381:2aabeb712f61
Date: 2014-07-04 22:11 +0800
http://bitbucket.org/pypy/pypy/changeset/2aabeb712f61/

Log:	make numpy scalar non-iterable

diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -153,6 +153,11 @@
         raise OperationError(space.w_IndexError, space.wrap(
             "invalid index to scalar variable"))
 
+    def descr_iter(self, space):
+        # Making numpy scalar non-iterable with a valid __getitem__ method
+        raise oefmt(space.w_TypeError,
+                    "'%T' object is not iterable", self)
+
     def descr_str(self, space):
         return space.wrap(self.get_dtype(space).itemtype.str_format(self))
 
@@ -555,6 +560,7 @@
     __new__ = interp2app(W_GenericBox.descr__new__.im_func),
 
     __getitem__ = interp2app(W_GenericBox.descr_getitem),
+    __iter__ = interp2app(W_GenericBox.descr_iter),
     __str__ = interp2app(W_GenericBox.descr_str),
     __repr__ = interp2app(W_GenericBox.descr_str),
     __format__ = interp2app(W_GenericBox.descr_format),
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
@@ -249,4 +249,12 @@
         assert d.dtype == dtype('int32')
         assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
 
-
+    def test_scalar_iter(self):
+        from numpypy import int8, int16, int32, int64, float32, float64
+        for t in int8, int16, int32, int64, float32, float64:
+            try:
+                iter(t(17))
+            except TypeError:
+                pass
+            else:
+                assert False, "%s object should not be iterable." % t


More information about the pypy-commit mailing list