Author: Yichao Yu <yyc1...@gmail.com>
Branch: numpy-generic-item
Changeset: r74050:cfef40b67090
Date: 2014-09-23 08:25 -0400
http://bitbucket.org/pypy/pypy/changeset/cfef40b67090/

Log:    Implement numpy.generic.item. Do not let numpy.ndarray.item accept
        random keyword arguments.

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
@@ -16,6 +16,7 @@
 from pypy.module.micronumpy.base import W_NDimArray, W_NumpyObject
 from pypy.module.micronumpy.concrete import VoidBoxStorage
 from pypy.module.micronumpy.flagsobj import W_FlagsObject
+from pypy.module.micronumpy import support
 
 MIXIN_32 = (W_IntObject.typedef,) if LONG_BIT == 32 else ()
 MIXIN_64 = (W_IntObject.typedef,) if LONG_BIT == 64 else ()
@@ -144,6 +145,22 @@
     def item(self, space):
         return self.get_dtype(space).itemtype.to_builtin_type(space, self)
 
+    def descr_item(self, space, args_w):
+        if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
+            args_w = space.fixedview(args_w[0])
+        if len(args_w) > 1:
+            raise oefmt(space.w_ValueError,
+                        "incorrect number of indices for array")
+        elif len(args_w) == 1:
+            try:
+                idx = support.index_w(space, args_w[0])
+            except OperationError:
+                raise oefmt(space.w_TypeError, "an integer is required")
+            if idx != 0:
+                raise oefmt(space.w_IndexError,
+                            "index %d is out of bounds for size 1", idx)
+        return self.item(space)
+
     def descr_getitem(self, space, w_item):
         from pypy.module.micronumpy.base import convert_to_array
         if space.is_w(w_item, space.w_Ellipsis) or \
@@ -619,6 +636,7 @@
     __hash__ = interp2app(W_GenericBox.descr_hash),
 
     tolist = interp2app(W_GenericBox.item),
+    item = interp2app(W_GenericBox.descr_item),
     min = interp2app(W_GenericBox.descr_self),
     max = interp2app(W_GenericBox.descr_self),
     argmin = interp2app(W_GenericBox.descr_zero),
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -470,8 +470,7 @@
     def descr_get_flatiter(self, space):
         return space.wrap(W_FlatIterator(self))
 
-    def descr_item(self, space, __args__):
-        args_w, kw_w = __args__.unpack()
+    def descr_item(self, space, args_w):
         if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
             args_w = space.fixedview(args_w[0])
         shape = self.get_shape()
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to