numpy/numpy — pull request #3907 by mhvk (2013-10-13 06:04:37+00:00)
Title: BUG allow subclasses in MaskedArray ufuncs -- for non-ndarray _data

This is a follow-on to #3900, making two furthe changes to tryto reach the ultimate goal of `MaskedArray` behaving sensibly for subclasses.
1. Ensure that for two-argument ufunc's, the data in the subclass are no longer converted to `ndarray`; rather, a possible subclass is kept. This is particularly relevant for my work on a `MaskedQuantity` class, where `_data` contains a `Quantity` which has not just a value (the ndarray) but also a unit.
2. Avoid the creation of `object` arrays, so that non-array Classes with an `__array_priority__` set and with a reverse method defined properly lead to `NotImplemented` (i.e., making the behaviour match that of regular arrays).

In order for this to work, I
- removed `subok=False` in the `getdata` calls in the two `BinaryOperation` classes
- slightly changed the method used to keep previous values when they are masked, including allowing this to fail (e.g., when I multiply two masked quantities in meters, the result is an area, and hence it is senseless to keep the original values, and quantities raise an appropriate UnitError). (Without this, the matrix subclass tests would not pass).
- slightly changed the order, with result masks only calculated if the primary calculation succeeded.
- slightly changed `getdata` to avoid the creation of `object` arrays, and `getmaskedarray` to deal with the consequences.

I also added a test case, which perhaps best makes clear why this is useful.

CC @charris -- I consider the above a correction of a bug, but realise this is subjective; it could also be ENH, even though the API does not change. It would be wonderful if this could still make it to 1.8.