Author: Brian Kearns <bdkea...@gmail.com>
Branch: 
Changeset: r67694:00ce0266c6c3
Date: 2013-10-29 12:32 -0400
http://bitbucket.org/pypy/pypy/changeset/00ce0266c6c3/

Log:    test and fix numpy dtype getitem behavior

diff --git a/pypy/module/micronumpy/interp_dtype.py 
b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -232,14 +232,27 @@
                         raise
                     break
 
-    @unwrap_spec(item=str)
-    def descr_getitem(self, space, item):
+    def descr_getitem(self, space, w_item):
         if self.fields is None:
-            raise OperationError(space.w_KeyError, space.wrap("There are no 
keys in dtypes %s" % self.name))
+            raise OperationError(space.w_KeyError, space.wrap(
+                "There are no fields in dtype %s." % self.name))
+        if space.isinstance_w(w_item, space.w_basestring):
+            item = space.str_w(w_item)
+        elif space.isinstance_w(w_item, space.w_int):
+            indx = space.int_w(w_item)
+            try:
+                item = self.fieldnames[indx]
+            except IndexError:
+                raise OperationError(space.w_IndexError, space.wrap(
+                    "Field index %d out of range." % indx))
+        else:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "Field key must be an integer, string, or unicode."))
         try:
             return self.fields[item][1]
         except KeyError:
-            raise OperationError(space.w_KeyError, space.wrap("Field named %s 
not found" % item))
+            raise OperationError(space.w_KeyError, space.wrap(
+                "Field named '%s' not found." % item))
 
     def descr_reduce(self, space):
         w_class = space.type(self)
diff --git a/pypy/module/micronumpy/test/test_dtypes.py 
b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -34,6 +34,14 @@
 
         assert dtype(None) is dtype(float)
 
+        e = dtype('int8')
+        exc = raises(KeyError, "e[2]")
+        assert exc.value.message == "There are no fields in dtype int8."
+        exc = raises(KeyError, "e['z']")
+        assert exc.value.message == "There are no fields in dtype int8."
+        exc = raises(KeyError, "e[None]")
+        assert exc.value.message == "There are no fields in dtype int8."
+
         exc = raises(TypeError, dtype, (1, 2))
         assert 'data type not understood' in str(exc.value)
         raises(KeyError, 'dtype(int)["asdasd"]')
@@ -828,7 +836,17 @@
         assert d["x"].itemsize == 16
         e = dtype([("x", "float", 2), ("y", "int", 2)])
         assert e.fields.keys() == keys
-        assert e['x'].shape == (2,)
+        for v in ['x', u'x', 0, -2]:
+            assert e[v] == (dtype('float'), (2,))
+        for v in ['y', u'y', 1, -1]:
+            assert e[v] == (dtype('int'), (2,))
+        for v in [-3, 2]:
+            exc = raises(IndexError, "e[%d]" % v)
+            assert exc.value.message == "Field index %d out of range." % v
+        exc = raises(KeyError, "e['z']")
+        assert exc.value.message == "Field named 'z' not found."
+        exc = raises(ValueError, "e[None]")
+        assert exc.value.message == 'Field key must be an integer, string, or 
unicode.'
 
         dt = dtype((float, 10))
         assert dt.shape == (10,)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to