1. Pypy
  2. Untitled project
  3. pypy

Commits

Romain Guillebert  committed d8b79f0

Implement pickling for slices

  • Participants
  • Parent commits b11b29c
  • Branches default

Comments (0)

Files changed (2)

File pypy/module/micronumpy/interp_boxes.py

View file
  • Ignore whitespace
 from pypy.module.micronumpy.arrayimpl.voidbox import VoidBoxStorage
 from rpython.rlib.objectmodel import specialize
 from pypy.interpreter.mixedmodule import MixedModule
+from rpython.rtyper.lltypesystem import lltype
+from rpython.rlib.rstring import StringBuilder
+
 
 MIXIN_32 = (int_typedef,) if LONG_BIT == 32 else ()
 MIXIN_64 = (int_typedef,) if LONG_BIT == 64 else ()
     return func_with_new_name(new, name + "_box_new"), staticmethod(_get_dtype), func_with_new_name(descr_reduce, "descr_reduce")
 
 
-class PrimitiveBox(object):
+class Box(object):
+    _mixin_ = True
+
+    def reduce(self, space):
+        from rpython.rlib.rstring import StringBuilder
+        from rpython.rtyper.lltypesystem import rffi, lltype
+
+        numpypy = space.getbuiltinmodule("_numpypy")
+        assert isinstance(numpypy, MixedModule)
+        multiarray = numpypy.get("multiarray")
+        assert isinstance(multiarray, MixedModule)
+        scalar = multiarray.get("scalar")
+
+        ret = space.newtuple([scalar, space.newtuple([space.wrap(self._get_dtype(space)), space.wrap(self.raw_str())])])
+        return ret
+
+class PrimitiveBox(Box):
     _mixin_ = True
 
     def __init__(self, value):
     def __repr__(self):
         return '%s(%s)' % (self.__class__.__name__, self.value)
 
-    def reduce(self, space):
-        from rpython.rlib.rstring import StringBuilder
-        from rpython.rtyper.lltypesystem import rffi, lltype
-
-        numpypy = space.getbuiltinmodule("_numpypy")
-        assert isinstance(numpypy, MixedModule)
-        multiarray = numpypy.get("multiarray")
-        assert isinstance(multiarray, MixedModule)
-        scalar = multiarray.get("scalar")
-
+    def raw_str(self):
         value = lltype.malloc(rffi.CArray(lltype.typeOf(self.value)), 1, flavor="raw")
         value[0] = self.value
 
         builder = StringBuilder()
         builder.append_charpsize(rffi.cast(rffi.CCHARP, value), rffi.sizeof(lltype.typeOf(self.value)))
+        ret = builder.build()
 
-        ret = space.newtuple([scalar, space.newtuple([space.wrap(self._get_dtype(space)), space.wrap(builder.build())])])
         lltype.free(value, flavor="raw")
+
         return ret
 
-class ComplexBox(object):
+class ComplexBox(Box):
     _mixin_ = True
 
     def __init__(self, real, imag=0.):
     def convert_imag_to(self, dtype):
         return dtype.box(self.imag)
 
-    def reduce(self, space):
-        from rpython.rlib.rstring import StringBuilder
-        from rpython.rtyper.lltypesystem import rffi, lltype
-
-        numpypy = space.getbuiltinmodule("_numpypy")
-        assert isinstance(numpypy, MixedModule)
-        multiarray = numpypy.get("multiarray")
-        assert isinstance(multiarray, MixedModule)
-        scalar = multiarray.get("scalar")
-
+    def raw_str(self):
         value = lltype.malloc(rffi.CArray(lltype.typeOf(self.real)), 2, flavor="raw")
         value[0] = self.real
         value[1] = self.imag
 
         builder = StringBuilder()
         builder.append_charpsize(rffi.cast(rffi.CCHARP, value), rffi.sizeof(lltype.typeOf(self.real)) * 2)
+        ret = builder.build()
 
-        ret = space.newtuple([scalar, space.newtuple([space.wrap(self._get_dtype(space)), space.wrap(builder.build())])])
         lltype.free(value, flavor="raw")
+
         return ret
 
 class W_GenericBox(W_Root):

File pypy/module/micronumpy/interp_numarray.py

View file
  • Ignore whitespace
         from rpython.rtyper.lltypesystem import rffi
         from rpython.rlib.rstring import StringBuilder
         from pypy.interpreter.mixedmodule import MixedModule
+        from pypy.module.micronumpy.arrayimpl.concrete import SliceArray
 
         numpypy = space.getbuiltinmodule("_numpypy")
         assert isinstance(numpypy, MixedModule)
         parameters = space.newtuple([space.gettypefor(W_NDimArray), space.newtuple([space.wrap(0)]), space.wrap("b")])
 
         builder = StringBuilder()
-        builder.append_charpsize(self.implementation.get_storage(), self.implementation.get_storage_size())
+        if isinstance(self.implementation, SliceArray):
+            iter = self.implementation.create_iter()
+            while not iter.done():
+                box = iter.getitem()
+                builder.append(box.raw_str())
+                iter.next()
+        else:
+            builder.append_charpsize(self.implementation.get_storage(), self.implementation.get_storage_size())
 
         state = space.newtuple([
                 space.wrap(1),      # version