| 210 | name=type(self).__name__, array=self.array.shape) |
| 211 | |
| 212 | def __reduce_ex__(self, protocol): |
| 213 | if not self.array.contiguous: |
| 214 | raise NotImplementedError("Reconstructing a non-contiguous " |
| 215 | "ndarray does not seem possible") |
| 216 | ndarray_kwargs = {"shape": self.array.shape, |
| 217 | "strides": self.array.strides, |
| 218 | "format": self.array.format, |
| 219 | "flags": (0 if self.readonly |
| 220 | else _testbuffer.ND_WRITABLE)} |
| 221 | pb = pickle.PickleBuffer(self.array) |
| 222 | if protocol >= 5: |
| 223 | return (type(self)._reconstruct, |
| 224 | (pb, ndarray_kwargs)) |
| 225 | else: |
| 226 | # Need to serialize the bytes in physical order |
| 227 | with pb.raw() as m: |
| 228 | return (type(self)._reconstruct, |
| 229 | (m.tobytes(), ndarray_kwargs)) |
| 230 | |
| 231 | @classmethod |
| 232 | def _reconstruct(cls, obj, kwargs): |