astropy/astropy — pull request #1834 by mhvk (2013-11-27 03:40:32+00:00)
Title: Correct behaviour of inplace ufunc's for Quantities holding non-contiguous data

With the current implementation of `__array_prepare__`, for in-place operations we return a copy of `self` with the real `self` hidden inside. Unfortunately, copying does not preserve strides, and as a result, one gets a `TypeError` if one feeds in non-contiguous data:
```
import astropy.units as u
q = u.Quantity([1.,2.,3.,4.], u.m)
q2 = q[::2]
q2 += 1.*u.cm
ERROR: TypeError: __array_prepare__ must return an ndarray or subclass thereof which is otherwise identical to its input [IPython.core.interactiveshell]
```

This PR fixes this by, instead, hiding a copy of `self` inside self, and then setting `self` back to it after the (possibly wrong) straight numpy operation has been applied.

One way in which this is inferior to what was there, is that now `self` *will* contain wrong data for a short instant. But I'm not sure how this can be avoided (and not sure it is a big problem, as in any case the data is changing).

Also added a test case; will update `CHANGES.rst` once this has been reviewed.