Author: Maciej Fijalkowski <fij...@gmail.com>
Branch: missing-ndarray-attributes
Changeset: r58578:864533f972b6
Date: 2012-10-29 12:13 +0100
http://bitbucket.org/pypy/pypy/changeset/864533f972b6/

Log:    implement byteswap

diff --git a/pypy/module/micronumpy/interp_boxes.py 
b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -35,6 +35,9 @@
     def convert_to(self, dtype):
         return dtype.box(self.value)
 
+    def __repr__(self):
+        return '%s(%s)' % (self.__class__.__name__, self.value)
+
 class ComplexBox(object):
     _mixin_ = True
 
diff --git a/pypy/module/micronumpy/interp_numarray.py 
b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -393,7 +393,6 @@
 
     @unwrap_spec(inplace=bool)
     def descr_byteswap(self, space, inplace=False):
-        raise OperationError(space.w_NotImplementedError, space.wrap("not 
impl"))
         if inplace:
             loop.byteswap(self.implementation, self.implementation)
             return self
@@ -796,6 +795,7 @@
     argsort = interp2app(W_NDimArray.descr_argsort),
     astype = interp2app(W_NDimArray.descr_astype),
     base = GetSetProperty(W_NDimArray.descr_get_base),
+    byteswap = interp2app(W_NDimArray.descr_byteswap),
 
     __array_interface__ = GetSetProperty(W_NDimArray.descr_array_iface),
 )
diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -464,8 +464,8 @@
                                     reds = ['from_iter', 'to_iter'])
 
 def copy_from_to(from_, to, dtype):
-    from_iter = from_.create_iter(from_.get_shape())
-    to_iter = to.create_iter(to.get_shape())
+    from_iter = from_.create_iter()
+    to_iter = to.create_iter()
     while not from_iter.done():
         copy_from_to_driver.jit_merge_point(dtype=dtype, from_iter=from_iter,
                                             to_iter=to_iter)
@@ -473,3 +473,16 @@
         to_iter.next()
         from_iter.next()
 
+byteswap_driver = jit.JitDriver(greens = ['dtype'],
+                                reds = ['from_iter', 'to_iter'])
+
+def byteswap(from_, to):
+    dtype = from_.dtype
+    from_iter = from_.create_iter()
+    to_iter = to.create_iter()
+    while not from_iter.done():
+        byteswap_driver.jit_merge_point(dtype=dtype, from_iter=from_iter,
+                                        to_iter=to_iter)
+        to_iter.setitem(dtype.itemtype.byteswap(from_iter.getitem()))
+        to_iter.next()
+        from_iter.next()
diff --git a/pypy/module/micronumpy/test/test_numarray.py 
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1593,7 +1593,11 @@
 
     def test_byteswap(self):
         from _numpypy import array
-        xxx
+        a = array([1, 256 + 2, 3], dtype='i2')
+        assert (a.byteswap() == [0x0100, 0x0201, 0x0300]).all()
+        assert (a == [1, 256 + 2, 3]).all()
+        assert (a.byteswap(True) == [0x0100, 0x0201, 0x0300]).all()
+        assert (a == [0x0100, 0x0201, 0x0300]).all()
 
 class AppTestMultiDim(BaseNumpyAppTest):
     def test_init(self):
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -204,6 +204,10 @@
     def neg(self, v):
         return -v
 
+    def byteswap(self, w_v):
+        # no for_computation here
+        return self.box(byteswap(self.unbox(w_v)))
+
     @simple_unary_op
     def conj(self, v):
         return v
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to