Author: Romain Guillebert <[email protected]>
Branch: numpy-pickle
Changeset: r62594:3a23062660aa
Date: 2013-03-20 18:50 +0100
http://bitbucket.org/pypy/pypy/changeset/3a23062660aa/

Log:    Implement pickle for dtypes

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
@@ -177,6 +177,36 @@
     def get_size(self):
         return self.itemtype.get_element_size()
 
+    def descr_reduce(self, space):
+        w_class = space.type(self)
+
+        kind = self.kind
+        elemsize = self.itemtype.get_element_size()
+        builder_args = space.newtuple([space.wrap("%s%d" % (kind, elemsize)), 
space.wrap(0), space.wrap(1)])
+
+        version = space.wrap(3)
+        order = space.wrap(byteorder_prefix if self.native else 
nonnative_byteorder_prefix)
+        names = self.descr_get_names(space)
+        values = self.descr_get_fields(space)
+        #TODO: Change this when alignment is implemented :
+        if self.fields:
+            #TODO: Implement this when subarrays are implemented
+            subdescr = space.w_None
+            size = 0
+            for key in self.fields:
+                size += self.fields[key].get_size()
+            w_size = space.wrap(size)
+            alignment = space.wrap(1)
+        else:
+            subdescr = space.w_None
+            w_size = space.wrap(-1)
+            alignment = space.wrap(-1)
+        flags = space.wrap(0)
+
+        data = space.newtuple([version, order, subdescr, names, values, 
w_size, alignment, flags])
+
+        return space.newtuple([w_class, builder_args, data])
+
 class W_ComplexDtype(W_Dtype):
     def __init__(self, itemtype, num, kind, name, char, w_box_type,
                  alternate_constructors=[], aliases=[],
@@ -249,7 +279,7 @@
 
 def dtype_from_spec(space, name):
         raise OperationError(space.w_NotImplementedError, space.wrap(
-            "dtype from spec"))    
+            "dtype from spec"))
 
 def descr__new__(space, w_subtype, w_dtype):
     cache = get_dtype_cache(space)
@@ -291,6 +321,8 @@
     __ne__ = interp2app(W_Dtype.descr_ne),
     __getitem__ = interp2app(W_Dtype.descr_getitem),
 
+    __reduce__ = interp2app(W_Dtype.descr_reduce),
+
     num = interp_attrproperty("num", cls=W_Dtype),
     kind = interp_attrproperty("kind", cls=W_Dtype),
     char = interp_attrproperty("char", cls=W_Dtype),
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
@@ -14,7 +14,7 @@
             from rpython.rtyper.lltypesystem import rffi
             ptr_size = rffi.sizeof(rffi.CCHARP)
         cls.w_ptr_size = cls.space.wrap(ptr_size)
-    
+
 class AppTestDtypes(BaseAppTestDtypes):
     def test_dtype(self):
         from numpypy import dtype
@@ -148,7 +148,7 @@
     def test_bool_binop_types(self):
         from numpypy import array, dtype
         types = [
-            '?', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd', 
+            '?', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd',
             'e'
         ]
         a = array([True], '?')
@@ -269,6 +269,10 @@
         ]:
             assert hash(tp(value)) == hash(value)
 
+    def test_pickle(self):
+        from numpypy import array, dtype
+        assert array([1,2,3]).dtype.__reduce__() == (dtype, ('i8', 0, 1), (3, 
'<', None, None, None, -1, -1, 0))
+
 
 class AppTestTypes(BaseAppTestDtypes):
     def test_abstract_types(self):
@@ -339,7 +343,7 @@
         import numpypy as numpy
 
         assert numpy.int8.mro() == [numpy.int8, numpy.signedinteger,
-                                    numpy.integer, numpy.number, 
+                                    numpy.integer, numpy.number,
                                     numpy.generic, object]
 
         a = numpy.array([1, 2, 3], numpy.int8)
@@ -362,8 +366,8 @@
     def test_uint8(self):
         import numpypy as numpy
 
-        assert numpy.uint8.mro() == [numpy.uint8, numpy.unsignedinteger, 
-                                     numpy.integer, numpy.number, 
+        assert numpy.uint8.mro() == [numpy.uint8, numpy.unsignedinteger,
+                                     numpy.integer, numpy.number,
                                      numpy.generic, object]
 
         a = numpy.array([1, 2, 3], numpy.uint8)
@@ -434,8 +438,8 @@
         import numpypy as numpy
 
         assert numpy.int_ is numpy.dtype(int).type
-        assert numpy.int_.mro() == [numpy.int_, numpy.signedinteger, 
-                                    numpy.integer, numpy.number, 
+        assert numpy.int_.mro() == [numpy.int_, numpy.signedinteger,
+                                    numpy.integer, numpy.number,
                                     numpy.generic, int, object]
 
     def test_int64(self):
@@ -443,12 +447,12 @@
         import numpypy as numpy
 
         if sys.maxint == 2 ** 63 -1:
-            assert numpy.int64.mro() == [numpy.int64, numpy.signedinteger, 
-                                         numpy.integer, numpy.number, 
+            assert numpy.int64.mro() == [numpy.int64, numpy.signedinteger,
+                                         numpy.integer, numpy.number,
                                          numpy.generic, int, object]
         else:
-            assert numpy.int64.mro() == [numpy.int64, numpy.signedinteger, 
-                                         numpy.integer, numpy.number, 
+            assert numpy.int64.mro() == [numpy.int64, numpy.signedinteger,
+                                         numpy.integer, numpy.number,
                                          numpy.generic, object]
 
         assert numpy.dtype(numpy.int64).type is numpy.int64
@@ -464,8 +468,8 @@
         import sys
         import numpypy as numpy
 
-        assert numpy.uint64.mro() == [numpy.uint64, numpy.unsignedinteger, 
-                                      numpy.integer, numpy.number, 
+        assert numpy.uint64.mro() == [numpy.uint64, numpy.unsignedinteger,
+                                      numpy.integer, numpy.number,
                                       numpy.generic, object]
 
         assert numpy.dtype(numpy.uint64).type is numpy.uint64
@@ -480,8 +484,8 @@
 
     def test_float16(self):
         import numpypy as numpy
-        assert numpy.float16.mro() == [numpy.float16, numpy.floating, 
-                                       numpy.inexact, numpy.number, 
+        assert numpy.float16.mro() == [numpy.float16, numpy.floating,
+                                       numpy.inexact, numpy.number,
                                        numpy.generic, object]
 
         assert numpy.float16(12) == numpy.float64(12)
@@ -492,8 +496,8 @@
     def test_float32(self):
         import numpypy as numpy
 
-        assert numpy.float32.mro() == [numpy.float32, numpy.floating, 
-                                       numpy.inexact, numpy.number, 
+        assert numpy.float32.mro() == [numpy.float32, numpy.floating,
+                                       numpy.inexact, numpy.number,
                                        numpy.generic, object]
 
         assert numpy.float32(12) == numpy.float64(12)
@@ -503,8 +507,8 @@
     def test_float64(self):
         import numpypy as numpy
 
-        assert numpy.float64.mro() == [numpy.float64, numpy.floating, 
-                                       numpy.inexact, numpy.number, 
+        assert numpy.float64.mro() == [numpy.float64, numpy.floating,
+                                       numpy.inexact, numpy.number,
                                        numpy.generic, float, object]
 
         a = numpy.array([1, 2, 3], numpy.float64)
@@ -829,7 +833,7 @@
         # it can be float96 or float128
         if numpy.longfloat != numpy.float64:
             assert numpy.longfloat.mro()[1:] == [numpy.floating,
-                                       numpy.inexact, numpy.number, 
+                                       numpy.inexact, numpy.number,
                                        numpy.generic, object]
         a = numpy.array([1, 2, 3], numpy.longdouble)
         assert type(a[1]) is numpy.longdouble
@@ -871,3 +875,4 @@
         a = array([1, 2, 3], dtype=self.non_native_prefix + 'G') # clongdouble
         assert a[0] == 1
         assert (a + a)[1] == 4
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to