Commits

Philip Jenvey committed 6934ff2 Merge

merge default

  • Participants
  • Parent commits c2eb802, 26c78d4
  • Branches py3k

Comments (0)

Files changed (4)

File pypy/module/micronumpy/boxes.py

         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))
             return space.wrap(dtype.itemtype.to_str(read_val))
         return read_val
 
+    def descr_iter(self, space):
+        return space.newseqiter(self)
+
     def descr_setitem(self, space, w_item, w_value):
         if (space.isinstance_w(w_item, space.w_bytes) or
             space.isinstance_w(w_item, space.w_unicode)):
     __new__ = interp2app(W_GenericBox.descr__new__.im_func),
 
     __getitem__ = interp2app(W_GenericBox.descr_getitem),
-    #__iter__ = interp2app(W_GenericBox.descr_iter),
+    __iter__ = interp2app(W_GenericBox.descr_iter),
     __str__ = interp2app(W_GenericBox.descr_str),
     __repr__ = interp2app(W_GenericBox.descr_str),
     __format__ = interp2app(W_GenericBox.descr_format),
     __new__ = interp2app(W_VoidBox.descr__new__.im_func),
     __getitem__ = interp2app(W_VoidBox.descr_getitem),
     __setitem__ = interp2app(W_VoidBox.descr_setitem),
+    __iter__ = interp2app(W_VoidBox.descr_iter),
 )
 
 W_CharacterBox.typedef = TypeDef("numpy.character", W_FlexibleBox.typedef,

File pypy/module/micronumpy/descriptor.py

 from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
                                       interp_attrproperty, interp_attrproperty_w)
 from rpython.rlib import jit
-from rpython.rlib.objectmodel import specialize
+from rpython.rlib.objectmodel import specialize, compute_hash
 from rpython.rlib.rarithmetic import r_longlong, r_ulonglong
 from pypy.module.micronumpy import types, boxes, base, support, constants as NPY
 from pypy.module.micronumpy.appbridge import get_appbridge_cache
             self.base = subdtype.base
 
     def __repr__(self):
-        if self.fields is not None:
+        if self.fields:
             return '<DType %r>' % self.fields
         return '<DType %r>' % self.itemtype
 
     def descr_ne(self, space, w_other):
         return space.wrap(not self.eq(space, w_other))
 
+    def _compute_hash(self, space, x):
+        from rpython.rlib.rarithmetic import intmask
+        if not self.fields and self.subdtype is None:
+            endian = self.byteorder
+            if endian == NPY.NATIVE:
+                endian = NPY.NATBYTE
+            flags = 0
+            y = 0x345678
+            y = intmask((1000003 * y) ^ ord(self.kind[0]))
+            y = intmask((1000003 * y) ^ ord(endian[0]))
+            y = intmask((1000003 * y) ^ flags)
+            y = intmask((1000003 * y) ^ self.elsize)
+            if self.is_flexible():
+                y = intmask((1000003 * y) ^ self.alignment)
+            return intmask((1000003 * x) ^ y)
+        if self.fields:
+            for name, (offset, subdtype) in self.fields.iteritems():
+                assert isinstance(subdtype, W_Dtype)
+                y = intmask(1000003 * (0x345678 ^ compute_hash(name)))
+                y = intmask(1000003 * (y ^ compute_hash(offset)))
+                y = intmask(1000003 * (y ^ subdtype._compute_hash(space,
+                                                                 0x345678)))
+                x = intmask(x ^ y)
+        if self.subdtype is not None:
+            for s in self.shape:
+                x = intmask((1000003 * x) ^ compute_hash(s))
+            x = self.base._compute_hash(space, x)
+        return x
+
     def descr_hash(self, space):
-        return space.hash(self.descr_reduce(space))
+        return space.wrap(self._compute_hash(space, 0x345678))
+
 
     def descr_str(self, space):
         if self.fields:

File pypy/module/micronumpy/test/test_dtypes.py

         d5 = numpy.dtype([('f0', 'i4'), ('f1', d2)])
         d6 = numpy.dtype([('f0', 'i4'), ('f1', d3)])
         import sys
-        if '__pypy__' not in sys.builtin_module_names:
-            assert hash(d1) == hash(d2)
-            assert hash(d1) != hash(d3)
-            assert hash(d4) == hash(d5)
-            assert hash(d4) != hash(d6)
-        else:
-            for d in [d1, d2, d3, d4, d5, d6]:
-                raises(TypeError, hash, d)
+        assert hash(d1) == hash(d2)
+        assert hash(d1) != hash(d3)
+        assert hash(d4) == hash(d5)
+        assert hash(d4) != hash(d6)
 
+    def test_record_hash(self):
+        from numpy import dtype
+        # make sure the fields hash return different value
+        # for different order of field in a structure
+
+        # swap names
+        t1 = dtype([('x', '<f4'), ('y', '<i4')])
+        t2 = dtype([('y', '<f4'), ('x', '<i4')])
+        assert hash(t1) != hash(t2)
+
+        # swap types
+        t3 = dtype([('x', '<f4'), ('y', '<i4')])
+        t4 = dtype([('x', '<i4'), ('y', '<f4')])
+        assert hash(t3) != hash(t4)
+
+        # swap offsets
+        t5 = dtype([('x', '<f4'), ('y', '<i4')])
+        t6 = dtype([('y', '<i4'), ('x', '<f4')])
+        assert hash(t5) != hash(t6)
     def test_pickle(self):
         import numpy as np
         from numpypy import array, dtype
         for t in [np.int_, np.float_]:
             dt = np.dtype(t)
             dt1 = dt.newbyteorder().newbyteorder()
+            assert dt.isbuiltin
+            assert not dt1.isbuiltin
             dt2 = dt.newbyteorder("<")
             dt3 = dt.newbyteorder(">")
             assert dt.byteorder != dt1.byteorder

File pypy/module/micronumpy/test/test_scalar.py

             assert np.isnan(b/a)
 
     def test_scalar_iter(self):
-        skip('not implemented yet')
         from numpypy import int8, int16, int32, int64, float32, float64
         for t in int8, int16, int32, int64, float32, float64:
             try: