| 163 | if _testbuffer is not None: |
| 164 | |
| 165 | class PicklableNDArray: |
| 166 | # A not-really-zero-copy picklable ndarray, as the ndarray() |
| 167 | # constructor doesn't allow for it |
| 168 | |
| 169 | zero_copy_reconstruct = False |
| 170 | |
| 171 | def __init__(self, *args, **kwargs): |
| 172 | self.array = _testbuffer.ndarray(*args, **kwargs) |
| 173 | |
| 174 | def __getitem__(self, idx): |
| 175 | cls = type(self) |
| 176 | new = cls.__new__(cls) |
| 177 | new.array = self.array[idx] |
| 178 | return new |
| 179 | |
| 180 | @property |
| 181 | def readonly(self): |
| 182 | return self.array.readonly |
| 183 | |
| 184 | @property |
| 185 | def c_contiguous(self): |
| 186 | return self.array.c_contiguous |
| 187 | |
| 188 | @property |
| 189 | def f_contiguous(self): |
| 190 | return self.array.f_contiguous |
| 191 | |
| 192 | def __eq__(self, other): |
| 193 | if not isinstance(other, PicklableNDArray): |
| 194 | return NotImplemented |
| 195 | return (other.array.format == self.array.format and |
| 196 | other.array.shape == self.array.shape and |
| 197 | other.array.strides == self.array.strides and |
| 198 | other.array.readonly == self.array.readonly and |
| 199 | other.array.tobytes() == self.array.tobytes()) |
| 200 | |
| 201 | def __ne__(self, other): |
| 202 | if not isinstance(other, PicklableNDArray): |
| 203 | return NotImplemented |
| 204 | return not (self == other) |
| 205 | |
| 206 | def __repr__(self): |
| 207 | return ("{name}(shape={array.shape}," |
| 208 | "strides={array.strides}, " |
| 209 | "bytes={array.tobytes()})").format( |
| 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: |
no outgoing calls
searching dependent graphs…