Author: Romain Guillebert <[email protected]>
Branch: numpy-pickle
Changeset: r63595:2a64441f3574
Date: 2013-04-24 18:21 +0200
http://bitbucket.org/pypy/pypy/changeset/2a64441f3574/
Log: Implement ndarray pickling (but no unpickling yet)
diff --git a/pypy/module/micronumpy/__init__.py
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -12,6 +12,7 @@
'zeros': 'interp_numarray.zeros',
'empty': 'interp_numarray.zeros',
'ones': 'interp_numarray.ones',
+ '_reconstruct' : 'interp_numarray._reconstruct',
'dot': 'interp_arrayops.dot',
'fromstring': 'interp_support.fromstring',
'flatiter': 'interp_flatiter.W_FlatIterator',
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py
b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -55,6 +55,9 @@
def get_size(self):
return self.size // self.dtype.itemtype.get_element_size()
+ def get_storage_size(self):
+ return self.size
+
def reshape(self, space, orig_array, new_shape):
# Since we got to here, prod(new_shape) == self.size
new_strides = None
@@ -82,7 +85,7 @@
return SliceArray(self.start, strides, backstrides,
self.get_shape(), self, orig_array)
- def set_real(self, space, orig_array, w_value):
+ def set_real(self, space, orig_array, w_value):
tmp = self.get_real(orig_array)
tmp.setslice(space, convert_to_array(space, w_value))
@@ -102,7 +105,7 @@
impl.fill(self.dtype.box(0))
return impl
- def set_imag(self, space, orig_array, w_value):
+ def set_imag(self, space, orig_array, w_value):
tmp = self.get_imag(orig_array)
tmp.setslice(space, convert_to_array(space, w_value))
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
@@ -775,6 +775,26 @@
return space.int(self.descr_getitem(space, space.wrap(0)))
raise OperationError(space.w_TypeError, space.wrap("only length-1
arrays can be converted to Python scalars"))
+ def descr_reduce(self, space):
+ from rpython.rtyper.lltypesystem import rffi
+ from rpython.rlib.rstring import StringBuilder
+
+ reconstruct =
space.getbuiltinmodule("_numpypy").get("multiarray").get("_reconstruct")
+
+ 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())
+
+ state = space.newtuple([
+ space.wrap(1), # version
+ self.descr_get_shape(space),
+ self.get_dtype(),
+ space.wrap(False), # is_fortran
+ space.wrap(builder.build()),
+ ])
+
+ return space.newtuple([reconstruct, parameters, state])
@unwrap_spec(offset=int)
def descr_new_array(space, w_subtype, w_shape, w_dtype=None, w_buffer=None,
@@ -807,6 +827,7 @@
W_NDimArray.typedef = TypeDef(
"ndarray",
+ __module__ = "numpypy",
__new__ = interp2app(descr_new_array),
__len__ = interp2app(W_NDimArray.descr_len),
@@ -924,6 +945,7 @@
__pypy_data__ = GetSetProperty(W_NDimArray.fget___pypy_data__,
W_NDimArray.fset___pypy_data__,
W_NDimArray.fdel___pypy_data__),
+ __reduce__ = interp2app(W_NDimArray.descr_reduce)
)
@unwrap_spec(ndmin=int, copy=bool, subok=bool)
@@ -1000,6 +1022,9 @@
arr.fill(one)
return space.wrap(arr)
+def _reconstruct(space, w_subtype, w_shape, w_dtype):
+ return descr_new_array(space, w_subtype, w_shape, w_dtype)
+
W_FlatIterator.typedef = TypeDef(
'flatiter',
__iter__ = interp2app(W_FlatIterator.descr_iter),
diff --git a/pypy/module/micronumpy/test/test_base.py
b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -73,8 +73,8 @@
# Coerce to floats, some of these will eventually be float16, or
# whatever our smallest float type is.
- assert find_unaryop_result_dtype(space, bool_dtype,
promote_to_float=True) is float16_dtype
- assert find_unaryop_result_dtype(space, int8_dtype,
promote_to_float=True) is float16_dtype
+ assert find_unaryop_result_dtype(space, bool_dtype,
promote_to_float=True) is float16_dtype
+ assert find_unaryop_result_dtype(space, int8_dtype,
promote_to_float=True) is float16_dtype
assert find_unaryop_result_dtype(space, uint8_dtype,
promote_to_float=True) is float16_dtype
assert find_unaryop_result_dtype(space, int16_dtype,
promote_to_float=True) is float32_dtype
assert find_unaryop_result_dtype(space, uint16_dtype,
promote_to_float=True) is float32_dtype
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
@@ -214,6 +214,7 @@
assert get(1, 1) == 3
class AppTestNumArray(BaseNumpyAppTest):
+ spaceconfig = dict(usemodules=["micronumpy", "struct", "binascii"])
def w_CustomIndexObject(self, index):
class CustomIndexObject(object):
def __init__(self, index):
@@ -1738,6 +1739,17 @@
assert raises(TypeError, "int(array([1, 2]))")
assert int(array([1.5])) == 1
+ def test__reduce__(self):
+ from numpypy import array, dtype
+ from cPickle import loads, dumps
+
+ a = array([1, 2], dtype="int64")
+ data = a.__reduce__()
+
+ assert data[2][4] ==
'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'
+
+ pickled_data = dumps(a)
+ assert loads(pickled_data) == a
class AppTestMultiDim(BaseNumpyAppTest):
def test_init(self):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit